Questions this answers
- What is agent-first API design?
- How is agent-first different from developer-first API design?
- Does agent-first design break existing clients?
Most APIs are built for human developers who read docs, browse examples, and debug with stack traces. In 2026, a growing share of API traffic comes from AI agents instead, and they don't interact with APIs the same way humans do.
This is how we redesigned TokenLab's unified AI API around one principle: don't try to be clever, be informative. We call the result agent-first API design, and it cut wasted tokens for our users by more than 60%.
Key Takeaways
- Agent-first API design adds structured, machine-readable hints to error responses so AI agents can self-correct without web searches or human help.
- Suggest alternatives, don't auto-correct. Fields like
did_you_mean,suggestions, andretryablelet agents make informed decisions instead of having a decision made for them. - Every suggestion is grounded in production data, so offline or deprecated models never show up in the candidate list.
- Hint fields are additive and backward compatible, so existing OpenAI-compatible clients keep working unchanged.
What Is Agent-First API Design?
Agent-first API design means structuring your responses, especially error responses, so an AI agent can understand what went wrong and fix it without leaving the conversation.
Traditional API error:
{"error": {"message": "Model not found"}}
Agent-first API error:
{
"error": {
"code": "model_not_found",
"message": "Model 'gpt5.5' not found",
"did_you_mean": "gpt-5.5",
"suggestions": [{"id": "gpt-5.5"}, {"id": "gemini-3.5-flash"}],
"hint": "Use GET /v1/models to list all available models."
}
}
With a traditional API, the agent has to search the web, find documentation, parse HTML, and guess. With an agent-first API, it self-corrects in one step.
Why Traditional APIs Fail AI Agents
Watch what happens when an agent hits a typical API aggregator for the first time:
Agent: POST /v1/chat/completions {"model": "gpt5.5"}
API: 400 {"error": {"message": "Model not found"}}
Agent: (searches the web for "tokenlab models list")
Agent: (fetches a docs page, maybe the wrong one)
Agent: (parses HTML, finds a model name)
Agent: POST /v1/chat/completions {"model": "gpt-5.5"}
API: 200 ✓
Six steps, multiple network requests, hundreds of wasted tokens. And that's the happy path, where the agent happened to guess the right docs URL.
With agent-first design:
Agent: POST /v1/chat/completions {"model": "gpt5.5"}
API: 400 {"did_you_mean": "gpt-5.5", "hint": "Use GET /v1/models..."}
Agent: POST /v1/chat/completions {"model": "gpt-5.5"}
API: 200 ✓
Two steps, zero web searches. The agent self-corrected from the error response alone.
The Core Principle: Intelligence Stays on the Model Side
The temptation is to build "smart" APIs that auto-correct the model name, silently reroute to something similar, or bolt on a recommendation engine. We rejected all of that.
When an agent sends model: "gpt5.5", you don't actually know its intent. Maybe it's checking whether a newer GPT release exists. Maybe it has a hard budget constraint. Maybe it needs a specific capability that only one model supports. Auto-routing to gpt-5.5 would silently change cost, quality, and capabilities, and the agent would never know it happened.
The better move is to fail fast and fail informatively. Give the agent all the data and let it decide.
Four Agent-First API Design Patterns
Pattern 1: Model Not Found → Fuzzy Suggestions
{
"error": {
"code": "model_not_found",
"did_you_mean": "gpt-5.5",
"suggestions": [
{"id": "gpt-5.5"},
{"id": "gemini-3.5-flash"},
{"id": "claude-sonnet-5"}
],
"hint": "Did you mean 'gpt-5.5'? Use GET /v1/models to list all available models."
}
}
did_you_mean uses a three-layer resolution: static alias mapping from production data, normalized string matching, and bounded edit distance. Every candidate is checked against the live model list, so we never suggest a model that's currently offline.
Pattern 2: Insufficient Balance → Budget-Aware Alternatives
{
"error": {
"code": "insufficient_balance",
"balance_usd": 0.12,
"estimated_cost_usd": 0.35,
"suggestions": [
{"id": "gemini-3.5-flash", "estimated_cost_usd": 0.02},
{"id": "deepseek-v4-flash", "estimated_cost_usd": 0.01}
],
"hint": "Insufficient balance. Try a cheaper model or top up."
}
}
Instead of just saying "not enough money," we tell the agent exactly how much it has, how much it needs, and which models it can afford right now. The agent can autonomously downgrade to a cheaper AI model without any human in the loop. Verify current per-model pricing on the TokenLab model directory before hardcoding cost thresholds.
Pattern 3: All Channels Failed → Live Alternatives
{
"error": {
"code": "all_channels_failed",
"retryable": true,
"retry_after": 30,
"alternatives": [
{"id": "claude-sonnet-5", "status": "available"},
{"id": "gpt-5.5", "status": "available"}
],
"hint": "All channels for 'claude-opus-4-8' temporarily unavailable. Retry in 30s or try an alternative."
}
}
The alternatives list isn't static. It's a live query against our channel health data, so the agent gets real-time information about what's actually working right now, not a hardcoded fallback list that may be stale.
Pattern 4: Rate Limited → Exact Retry Timing
{
"error": {
"code": "rate_limit_exceeded",
"retryable": true,
"retry_after": 8,
"limit": "1000/min",
"remaining": 0,
"hint": "Rate limited. Retry after 8s."
}
}
No guessing, no exponential backoff starting from an arbitrary value. The agent knows the exact wait time. For more on handling rate limits well, see our AI API rate limiting guide.
Success Responses Carry Hints Too
When an agent calls /v1/chat/completions with a Claude model, the response includes:
X-TokenLab-Hint: This model supports native Anthropic format. Use POST /v1/messages for better performance.
X-TokenLab-Native-Endpoint: /v1/messages
We're telling the agent, this worked, but there's a better way. It can switch to the native endpoint on the next call and pick up features like extended thinking and prompt caching that aren't exposed through the OpenAI-compatible format.
These hints live in headers, not the response body, because the body has to follow the OpenAI or Anthropic spec exactly. Headers are the safe extension point that won't break any existing parsing logic.
The /v1/models Response as an Agent Cheat Sheet
We added three fields to every model entry in the /v1/models response:
category: chat model, image generator, video model, or audio. No more guessing from the name.pricing_unit: per token, per image, per second, or per request. Needed for any real cost estimation.cache_pricing: upstream prompt cache prices plus the platform's semantic cache discount.
Combined with existing fields (pricing, capabilities, aliases, max tokens), an agent can make a fully informed model selection from a single API call. You can see the full live catalog in the TokenLab model directory (observed 2026-07-07), which currently lists 300+ models across chat, image, video, and audio categories including current frontier options like Claude Sonnet 5, GPT-5.5, and Gemini 3.5 Flash. Verify current pricing and availability on that page rather than assuming figures in this article are current.
llms.txt: The Agent's First Read
We serve a dynamic llms.txt at api.tokenlab.sh/llms.txt, a machine-readable overview of the whole API. It includes:
- A first-call template with working code
- Common model names, auto-generated from usage data rather than hardcoded
- All 12 endpoints with parameters
- Filter parameters for model discovery
An agent that reads this file before its first API call is far more likely to get the request right on the first try.
Data-Driven, Not Knowledge-Driven
Every suggestion in the system comes from production data. The did_you_mean alias map was seeded from 30 days of actual model_not_found errors in our request logs. Model suggestions are sorted by real usage. The "common model names" list in llms.txt is generated from our database, not maintained by hand.
We track every model miss in a Redis sorted set. Once a misspelling accumulates enough hits, it gets promoted into the alias map. When a model goes offline, it drops out of every suggestion list automatically. The system tunes itself over time instead of drifting out of date, which matters when new model releases like GPT-5.5, Claude Sonnet 5, and Gemini 3.5 Flash ship on overlapping timelines.
The Design Constraint That Made It Work
We set one rule: no new endpoints, no new SDKs, no breaking changes. Everything had to fit inside the existing OpenAI-compatible error format. New fields are optional, so any client that ignores them gets exactly the same experience as before.
That constraint forced precision about what actually helps an agent self-correct, instead of building elaborate new APIs that nobody would bother to adopt.
How to Apply Agent-First Design to Your Own API
If you're building APIs that AI agents will consume:
- Make every error actionable. State what went wrong, why, and what to do next.
- Suggest alternatives instead of auto-correcting. Let the agent make the informed decision.
- Use structured fields, not prose.
did_you_meanis parseable; "did you mean..." buried in a sentence is not. - Ground suggestions in real data. Production usage patterns beat a hardcoded list that goes stale.
- Serve machine-readable discovery through
llms.txt, an OpenAPI spec, or a structured model list. - Keep it backward compatible. New hint fields should be additive, never breaking.
Where to Start Without Rewriting Everything
Most teams don't need to redesign their whole API in one week. A smaller starting point works fine:
- Add one or two machine-readable hint fields to your highest-volume errors.
- Make
/v1/modelsor your equivalent discovery endpoint richer and more explicit. - Publish one machine-readable overview, such as
llms.txt. - Test the full loop with an actual agent client, not just curl.
If you're already operating through a gateway layer, the unified AI gateway guide explains why that control plane matters. If you're still on a direct OpenAI-compatible integration, the migration guide is the easiest place to start before layering in agent-friendly behavior.
FAQ
What is agent-first API design?
It's an approach where error responses include structured, machine-readable hints (fields like did_you_mean, suggestions, and hint) so AI agents can self-correct without human intervention or a documentation lookup.
How is agent-first different from developer-first API design?
Developer-first APIs optimize for human readability: clear messages, good docs, helpful examples. Agent-first APIs add structured fields on top of that so machines can parse the error and act on it programmatically, without reading anything.
Does agent-first design break existing clients?
No. The fields are additive. Existing clients that don't look for did_you_mean or suggestions simply ignore them and keep working exactly as before.
TokenLab provides unified access to 300+ AI models, including current frontier models like GPT-5.5, Claude Sonnet 5, and Gemini 3.5 Flash, through a single API listed in the model directory. Get Started free to test the agent-first API with $1 in starter credits.
Sources
Price observed 2026-07-07
- TokenLab model directoryObserved 2026-07-07



