|
Hi Tip-Sheeters, This week has an everything-old-is-new-again flavor: we're looking at building command line interfaces (CLIs) for APIs. You remember those old things: the somewhat cryptic tools you run in a linux prompt or from your Mac's terminal. Well they're having a moment in the AI and API space because they're a useful way for agents to call your APIs. And we're going to build one for the Air Travel API. Read to the end to see an LLM use the CLI in an agent harness. Back from Summer BreakIt's been several weeks since I sent the last Tip Sheet. I had a much-needed summer vacation driving down California's State Route 1 on the U.S. west coast seeing beautiful places like Mavericks Beach and Big Sur.
I normally spend my days in Middle America, but that West Coast is something special if you haven't had a chance to visit. On the trip I also spent a short time in San Francisco and Silicon Valley (not on Highway 1 but nearby). The Bay Area is home to a lot of the commercial companies at the front of the AI race, such as Anthropic, OpenAI, Google, Nvidia, Meta, Cohere and schools like Stanford University. I didn't have time for any office visits, but you could almost feel the tokens flying through the air. (The joke goes that in Los Angeles your waiter or waitress is probably an actor -- in the Bay Area they're probably a founder/CTO.) Tip Sheet #66 went globalTip Sheet #66 Global Edition was one of the most popular newsletters I've sent. Thanks to everyone who read that one and sent me a note. I've already got some requests for regions to include in the next Global issue. One of my most loyal readers -- you know who you are -- requested Africa π, so the research has begun... Shoot me an email with any events, resources, job postings from your region and I'll try to include them in the next Global Edition. (Replying directly also helps the newsletter stay out of spam filters so the Tip Sheet can fulfill its purpose in life π.) Airline Anchor Project: Building a CLI for your APICLIs have become more popular recently as a method for providing APIs to LLM agents in code harnesses like Claude Code and GitHub Copilot. Here's a summary of CLIs I gave in Tip Sheet #62: Understanding the debate over MCPs, Skills and CLIs:
Command line interface (CLI) tools - These are the commands you run from a (usually linux) command prompt. They can be local commands like `grep` or API wrappers like `aws` and `az`. These weren't created for agents, but agents can use them, especially if they're well-known CLI tools that are referenced in a lot of training data (blog posts, documentation pages, tech books). Here's the big picture view of my Air Travel anchor project, with the part we're covering this week highlighted: As you see in the diagram, the CLI will use a software development kit (SDK) with reusable code for interacting with the API. This approach gives us two major benefits:
Getting the Code: Normally I code examples for the Tip Sheet in a dedicated repo. This week instead of the code, I added a Tip Sheet #67 readme.md with links to parts of the overall anchor project repo where the CLI code is found. (Since the CLI and SDK are working together, it's easier to point to the main repo this time.) Getting started with a CLI with TyperI decided to build my CLI using the Typer library created by SebastiΓ‘n RamΓrez MontaΓ±o, creator of FastAPI. He used Typer to build the `fastapi` CLI, which has provided major improvements to the usability in FastAPI in the last couple of years. Since this was my first CLI, some of the first questions I had were:
I've used CLIs from the command line, but I never understood how some of the features I took for granted had to be supported by the CLI I was calling. Here are some features that CLIs typically provide: What core features do users need?
Here's what you'll see if you execute 'air-travel --help' when my CLI is installed: Human users sometimes use this to figure out the right options for interacting with the CLI. You'll see that Agents use it a lot. That feature is automatically performed by Typer. 2. CLIs need to process a lot of options and provide intelligent responses. Here are all the options available in my CLI's 'flights' command: Your CLI is going to be performing a lot of logic to check for legal options, validate input types, and explain to the user when they're wrong. Typer handles option processing for you. 3. Prompt completion Some command line tools give type-ahead functionality rather than forcing the user to type the full command (and spell all the options correctly). A CLI can provide this functionality, and Typer provides that option. Let's Jump into the CodeThe structure of the air travel CLI is quite simple, as you can see by the tree listing of the repo's cli folder: βββ README.md βββ pyproject.toml - includes project settings and command setup βββ src β βββ air_travel_cli β βββ main.py - single Python file for CLI βββ tests β βββ test_air_travel_cli.py - Pytest βββ uv.lock - uv lock file to make sure build is reproducible Most of the action is in the main.py file. First we get the import handled and create some global objects: Next we setup global settings for the CLI using the `@app.callback()` which is a Typer decorator Then we define each command using the `@app.command` decorator. Our two commands will be: Installing and running the CLII haven't published the CLI or SDK on PyPi, so to use them you clone the github repo, and use uv to build and install the CLI as a tool. Full instructions to build and install are in the readme here. Once the CLI is installed, you can first run the health check to make sure the API is up and running: `air-travel health` API is healthy {'message': 'API health check is successful'} To retrieve results from the API, flights command: `air-travel flights --carrier AA --limit 1` Flight ID: 97 Date: 2025-11-12 Carrier: AA 4810 Route: SGU (St. George, UT) to PHX (Phoenix, AZ) Scheduled Departure: 0730 Actual Departure: 0804 Scheduled Arrival: 0851 Using the CLI with an Agent HarnessOK if you made it this far, I'll give up the goods: using the CLI with GitHub Copilot. I have Copilot running in VS Code and have installed the CLI in that same environment. Goal: get the LLM to correctly use the CLI and retrieve data from my API using it. Results: Pretty successful in the end -- after one big change to the CLI to limit the default flight records to 20. When I defaulted to 100 records, the agent harness got an error of some kind and the LLM kept trying different things to figure out how to get it to work. Here are some screenshots of the steps the agent went through. I have my agent harness set so that before it performs a command it has to get permission from me. Step one is to prompt and get it to use the air-travel CLI. In this environment there are a few other options such as an MCP server and agent skill, so getting this first step to work is key. The next step is for the agent to use the help function of the CLI to figure out the proper syntax. This is a big selling point of CLIs for agents, that they can take steps to gather the information they need. This is really important for non-famous CLIs, because they won't have any training data on it and can make bad choices. With the information about syntax, it calls the air-travel CLI. One minor point is that it correctly used the actual date for Thanksgiving Day 2025. (This is a different day every year, so a little bit of logic needed there.) Additional features for the air-travel CLILightweight results - To save the number of tokens used by agents I can add some summary views or table views. Publishing the CLI and SDK on PyPi - This will help Python users go get them without the repo. Adding binary installations - In addition to the Python versions, I can use GitHub Actions to compile the CLI into binaries so it can be installed in other environments. Whew, honestly this newsletter kept getting longer and longer...but I learned so much in researching and writing this! Hope you got something out of it that you can use, Keep coding, Ryan Day π https://tips.handsonapibook.com/ -- no spam, just a short email every week or so. |
This is my weekly newsletter where I share some useful tips that I've learned while researching and writing the book Hands-on APIs for AI and Data Science, a #1 New Release from O'Reilly Publishing
Hi Tip-Sheeters, This week I'm sharing highlights of virtual chat I had with Ricardo Heredia Joya, a Madrid data scientist with a background as a medical doctor. He is active in the Football analytics community, and shares his thoughts and tips in the Underdoc substack. (If you're enjoying the Tip Sheet I'm sure you'll get a lot from Ricardo's newsletter.) You are your best asset, so never stop learning. - Ricardo Heredia Joya Q1: Thanks for having a virtual chat with the Tip Sheet...
Hi Tip-Sheeters, I've been looking forward to this week's newsletter for a while -- The One Where I Deploy to FastAPI Cloud (fans of TV's Friends may appreciate the title). Since I began using FastAPI, some of the best improvements I have seen have been in the "fastapi" command line interface program. CLIs have become pretty important in their own right lately so if you're interested in the code for the FastAPI CLI you can see it here: https://github.com/fastapilabs. The CLI is built with...
Hi Tip-Sheeters, Thanks for everyone who joined the NordicAPIs LiveCast: Asynchronous APIs for AI and Data Science recently. If you're interested in skimming to catch some of the interesting bits, here are a few: 12:45 - How asynch patterns used by agents have an impact on your API design 39:14 - How learning enterprise patterns is beneficial for API developers 49:24 - What skills should data scientists and software developers learn right now Thanks again to NordicAPIs -- I encourage you to...