Designing a CLI for AI agents, not humans
What an agent should see, what it shouldn't, and how to fail in a way it can recover from.
We ran agents through a set of real tasks and did a full QA pass on the CLI. The CLI worked. The agents using it kept getting stuck. Some examples:
- An agent stopped to ask the user for an API token it didn't need, because the schema listed one. Another agent made a token up and sent it.
- Every Google call carried about a dozen extra parameters, like
prettyPrintandquotaUser. The agent never needed them, but it paid for them in tokens on every call. - An agent got a 503 on a POST, saw
"retryable": true, and sent it again. The first request had already gone through. - When a policy blocked a call, the message told the agent to "edit or remove the guarding policy". We never want an agent to do that.
- A provider with a broken address failed with "must use HTTPS". The agent kept changing its input, which was fine all along.
- One missing type in Stripe's spec stopped every refund method from installing.
Nothing here crashed. Each time, the CLI did its job and then printed something that sent the agent the wrong way.
We had written the output for a developer reading the terminal. But most of our calls come from agents. An agent reads the output as instructions and acts on it straight away, many times in a row. So we went through the CLI again with that reader in mind. Most fixes came down to three things: show the agent less, tell it more when something fails, and never suggest the wrong next step.
How an agent uses the CLI
An agent works in three steps: find a capability, read its schema, run it.
swytchcode discover "refund a stripe charge" --json
swytchcode info refunds.refund.create --json
swytchcode exec refunds.refund.create --json --body '{"charge":"ch_123","amount":5000}'Over MCP these are the swytchcode_discover, swytchcode_info and swytchcode_exec tools. The rest of this post is about getting those three outputs right.
Show only what the agent can fill in
info returns the schema the agent fills in. This is the real output for Stripe's refund method, with the description cut short:
{
"canonical_id": "refunds.refund.create",
"type": "method",
"integration": "[email protected]",
"summary": "Create customer balance refund",
"http_method": "POST",
"endpoint": "/v1/refunds",
"inputs": [
{ "amount": { "LOCATION": "body", "REQUIRED": false, "TYPE": "INT" } },
{ "charge": { "LOCATION": "body", "REQUIRED": false, "TYPE": "STRING" } },
{ "payment_intent": { "LOCATION": "body", "REQUIRED": false, "TYPE": "STRING" } },
{ "reason": { "LOCATION": "body", "REQUIRED": false, "TYPE": "STRING" } },
{ "metadata": { "LOCATION": "body", "REQUIRED": false, "TYPE": "ANY" } }
]
}We made three changes to get here.
We removed credentials. A spec lists access_token, api_key or an X-API-Key header as inputs, because the raw API needs them. But Swytchcode adds those itself when the call runs, from the connected account. If the agent sees them, it either asks the user for a token or invents one.
Some names are always credentials. Others, like token, are only credentials when they're sent in a header or the query string. A body field called token is real data, so we keep it:
// Always a credential, wherever it's sent. Compared after lowercasing and
// removing '-' and '_', so api_key, api-key and apiKey all match.
var strongCredentialParams = map[string]struct{}{
"accesstoken": {}, "oauthtoken": {}, "refreshtoken": {}, "idtoken": {},
"clientsecret": {}, "apikey": {}, "xapikey": {},
}
// A credential only in a header or query string. A body field called
// "token" is real payload and stays.
var gatedCredentialParams = map[string]struct{}{
"key": {}, "token": {}, "bearer": {}, "authorization": {},
}
func isInjectedCredentialParam(name, location string) bool {
n := normalizeParamName(name)
if _, ok := strongCredentialParams[n]; ok {
return true
}
if _, ok := gatedCredentialParams[n]; ok {
switch strings.ToLower(strings.TrimSpace(location)) {
case "", "header", "query":
return true
}
}
return false
}We removed Google's extra parameters. Specs built from Google's APIs add alt, callback, fields, prettyPrint, quotaUser, uploadType, upload_protocol and $.xgafv to almost every method. No tool call needs them. We drop them only when they're query parameters, since a body field called fields or callback can be real data.
We also stopped sending the raw spec entry along with the cleaned schema. It doubled the size of the response and brought back the fields we had just removed.
We added the endpoint. Two methods can differ only by endpoint, like gmail.user.send.create and gmail.user.send.create1. Before http_method and endpoint were in the output, the agent needed a dry run to tell them apart.
Every error has the same shape
Every error the CLI prints looks like this:
{
"error": "what went wrong",
"category": "one of a fixed set",
"retryable": false,
"suggested_action": "one sentence the agent can follow",
"docs_url": "https://docs.swytchcode.com/...",
"reference_id": "SWY-ERR-C3408C"
}category is one of auth, validation, not_found, network, rate_limit, internal, permission_denied, policy_denied, policy_error, base_url_not_set, tenant_not_connected, plan_limit or setup_required. An agent should check the category, not the message text.
The exit codes are fixed too:
| Code | Meaning |
|---|---|
| 0 | OK, including a provider's own 4xx/5xx (see below) |
| 1 | Invalid input |
| 2 | Tool not found |
| 3 | Credential problem (not connected, plan, setup) |
| 4 | Request failed after retries |
| 5 | Internal error |
| 6 | Permission denied (including a policy block) |
| 7 | Waiting for human approval |
| 130 | Interrupted |
Provider errors get the same fields
If the provider sends back an error, the call itself still worked, so the CLI exits with 0 and shows the status. Now it also adds the same fields an agent needs to decide what to do. A real 404:
{
"status_code": 404,
"error_category": "not_found",
"retryable": false,
"suggested_action": "the target resource does not exist; check the ids and the endpoint",
"request": { "method": "GET", "url": "https://api.stripe.com/v1/customers" },
"data": { "error": { "message": "No such customer", "type": "invalid_request_error" } }
}Scripts that read status_code keep working.
Retryable must be safe
Our first version set retryable: true for every 429 and 5xx. That turned out to be dangerous.
A provider can return a 500 after it has already created the customer or sent the email. Our own retry code knows this and never repeats a plain POST. But if we tell an agent "retryable": true, the agent repeats it.
So now retryable is true only when the error is temporary and the request is safe to send again:
func requestIsRetrySafe(req *http.Request) bool {
if isIdempotentMethod(req.Method) {
return true
}
for k := range req.Header {
if strings.Contains(strings.ToLower(k), "idempotency") {
return true
}
}
return false
}The real output for a POST that got a 503:
{
"status_code": 503,
"error_category": "server_error",
"retryable": false,
"suggested_action": "the provider signalled a transient error, but this call may have already taken effect; do not blindly retry - only re-issue with an idempotency key so the provider can dedupe it",
"request": { "method": "POST", "url": "https://api.stripe.com/v1/customers" }
}With idempotency turned on, the same POST is retried for you. A GET that gets a 503 comes back with "retryable": true.
Don't suggest the wrong next step
Some messages were fine for a person and wrong for an agent. The policy block was the worst one. This is what it says now:
{
"error": "blocked by policy \"no-personal-email\": Customers with a gmail.com address are not allowed",
"category": "policy_denied",
"source": "policy",
"hint": "Use the customer's business email address",
"suggested_action": "this call was blocked by a configured guard and was not executed - do not retry it or try to work around the guard; report to the user which guard blocked it. A human can review guards with: swytchcode policy list",
"docs_url": "https://docs.swytchcode.com/policies/policy-rules/"
}- The policy id is at the start of the error, because the rule's own message might just say "blocked".
source: "policy"tells our rules apart from the provider's. A policy rate limit and a Stripe 429 both havecategory: "rate_limit", and the agent needs to know which one it hit.hintcomes from whoever wrote the policy. Here it tells the agent how to fix its own request.suggested_actiontells the agent not to work around the rule, and to tell the user.
We fixed other messages the same way:
$ swytchcode plan customers.customer.list
× customers.customer.list is a method, not a workflow
hint: run 'swytchcode exec customers.customer.list' to execute it{
"error": "base URL for [email protected] (sandbox) is not set: \"https://{{site}}.example.com\" (it has a placeholder to fill in)",
"category": "base_url_not_set",
"suggested_action": "this provider runs on your own instance - set sandbox_endpoint for \"[email protected]\" in .swytchcode/integrations/manifest.json, or set SWYTCHCODE_BASE_URL_STRIPE_STRIPE_SANDBOX"
}The second one used to say “must use HTTPS”. That blamed the agent's input when the real problem was in the provider's spec.
When a call is waiting for human approval, the CLI exits with 7, not an error code. The agent knows the call is waiting and can get on with other work:
$ swytchcode exec refunds.refund.create --dry-run --body '{"charge":"ch_123","amount":250000}'
This command matches an approval policy ("large-refunds") and would require human approval before running.
$ echo $?
7Give the agent fewer tools
The MCP server has two profiles:
swytchcode mcp serve --profile agent # default
swytchcode mcp serve --profile fullThe real tool list for agent:
["swytchcode_add", "swytchcode_discover", "swytchcode_exec", "swytchcode_info",
"swytchcode_list", "swytchcode_policy", "swytchcode_search"]swytchcode_policy can only read policies. Adding, checking and removing policies is only in full, so an agent can't relax the rules that limit it. A test checks this, so nobody adds a write tool to agent by mistake.
A typo in the profile name is an error. It doesn't fall back to full:
$ swytchcode mcp serve --profile agnet
Error: invalid --profile "agnet" (valid: "agent" or "full")Warn instead of failing
An agent can't fix a broken spec, so one bad part of a spec shouldn't block everything else. Stripe's spec uses a type it never defines, deep in the refund response. That used to make add fail for every refund method. Now:
$ swytchcode add method refunds.refund.create
⚠ could not build the response shape for "refunds.refund.create": struct "refund" field "destination_details": struct "refund_destination_details" field "affirm": struct "destination_details_unimplemented" not found in STRUCTS. The integration bundle refers to a data type it does not define, so the method was added without a response schema.
Added method "refunds.refund.create" to tooling.json (provider: stripe.stripe)The inputs are complete, so the method works. The warning goes to stderr, so the JSON on stdout stays clean.
Keep each call fast
Agents make lots of small calls. A big provider spec can be several megabytes with hundreds of methods, and we were reading and parsing it on every call. Now we keep the parsed spec in memory and only parse again when the file's size or modified time changes. For large providers, parsing had been most of the time each call took.
Looking back
What changed most for us was the reader we think about when we write output. A developer can work out a vague error. An agent can't. It does exactly what the text says. So now we read every message we add and ask: if a model does exactly this, what happens?
A lot of this work was removing things. We expected to help agents by giving them more context. Most of the real wins came from showing them less.
If you're building a tool that agents will use, run real agents against it early and watch what they do. Most of what we found never showed up in our own logs.
At Swytchcode, agents are the main users of the CLI, so we now treat its output like an API: it has a fixed shape, it's versioned with care, and it's tested. If you're building agents with Swytchcode, you get all of this by default. Your agent sees clean schemas, clear errors and a safe answer to "can I retry?", without any extra work on your side.
Related reading
More from the blog
We asked our Hermes agent if it was leaking API keys. It printed one into the chat.
Your Hermes agent keeps API keys in more places than you think, and some go to your AI provider with every message. Swytchcode's free API Key Leak Check finds them in seconds and explains the fix in plain words.
What did your Hermes agent install last week? Now you can check.
Your Hermes AI agent installs packages, pulls containers and clones repos on your behalf, and keeps no record. Swytchcode's free Agent Install Monitor keeps one, privately, on your own machine.
We connected Instinct to a flight API through Swytchcode. Here's how it went
Instinct has no way to plug in custom APIs, so we tried connecting it to Duffel's flight API through a Google Sheet, with Swytchcode holding the keys and the rules. How it works, and an honest look at how Swytchcode did.
