Short answer
A coding agent re-sends its system prompt, tool definitions, project instructions, conversation history, file reads and tool output to the model on every turn, then pays for the model's reply on top. Most tokens are input, and input grows as the session's history grows. Prompt caching discounts the repeated prefix; retries, loops and parallel agents multiply the total.
What goes into a single agent turn
A coding agent such as Claude Code, Codex CLI or AirCode CLI doesn't send just your prompt to the model. Every turn it assembles a request from several parts, and the model is billed for all of them. The parts are much the same in every agent.
- System prompt and tool definitions. These are the agent's own instructions plus a description of every tool it can call: file edits, shell commands, search, and any MCP servers you have connected. They are sent with every request.
- Project instructions. Files such as
CLAUDE.mdorAGENTS.mdload at the start of a session and stay in context. A long instructions file costs tokens on every turn, not once. - Conversation history. The model has no memory between requests. To continue a session, the agent re-sends the earlier messages, tool calls and results.
- File reads and search results. When the agent opens a file or greps the repository, the content becomes input tokens, and it stays in the history afterwards.
- Tool output. Test runs, build logs, stack traces and command output are often the bulkiest part of a turn. A failing test suite can produce far more text than the code being fixed.
- Model output and reasoning. The reply, the edits and any extended thinking are output tokens, which providers usually price higher than input.
The prompt you typed is often the smallest of these parts. That's why a one-line request in a large repository can still be an expensive turn.
Input, output and cached tokens
Providers bill these three kinds of tokens differently, so it helps to know which one is growing.
Input tokens
Input covers everything sent to the model: instructions, tools, history, files and tool output. In a long agent session, input usually makes up most of the token count, because the context is re-sent on every turn.
Output tokens
Output covers what the model writes back, including code, explanations and reasoning. A turn produces far fewer output tokens than input tokens, but each one usually costs more.
Cached tokens
Prompt caching lets a provider reuse the unchanged beginning of a request, such as the system prompt, tool definitions and early history. Cached input is usually billed at a reduced rate, so an agent that keeps its prefix stable pays less for the context it repeats.
Caches don't last forever. In Claude Code, Anthropic documents a cache lifetime of five minutes by default on API keys and cloud providers, and one hour on a subscription within plan usage. Switching models, changing the effort level on most models, or connecting an MCP server forces a fresh cache. Claude Code's gateway documentation also notes that if a gateway strips the cache_control fields, the conversation bills as uncached input on every turn.
Note. Cache pricing and lifetimes differ by provider and model. Check your provider's pricing page for the rates that apply to you.
Why usage grows as a session goes on
Because the history is re-sent each turn, a session gets more expensive per turn the longer it runs. Early turns carry instructions and a few file reads. Later turns carry those plus every earlier message, tool call and test log that is still in context.
The table below is a hypothetical session. The numbers are invented to show the shape of the growth; they aren't measurements from any tool.
| Part of the request | Turn 1 | Turn 20 |
|---|---|---|
| Instructions and tool definitions | Sent in full | Same content, likely served from cache |
| Conversation history | Empty | 19 earlier turns, with their tool calls |
| File reads and search results | A few files | Every file read so far, some more than once |
| Tool output | None yet | Several test runs and build logs |
| Your new message | One line | One line |
Agents manage this growth by summarizing. Claude Code, for example, compacts the conversation automatically as it approaches the context limit, and you can trigger it yourself with /compact. Compaction keeps the session going but discards detail, so it is a trade-off rather than free savings.
Loops, retries and parallel agents multiply usage
A few patterns can multiply token usage well beyond what the task needs.
- Retries. When a request fails or a tool call errors, the agent tries again and sends the full context again.
- Unproductive loops. An agent can repeat the same search, edit or test run without making progress. Each pass is a full-priced turn.
- Subagents. In Claude Code, each subagent runs in its own context window. That keeps verbose work out of your main conversation, but the subagent's tokens still count.
- Agent teams. Anthropic's cost guide says agent teams use approximately 7x more tokens than a standard session. Parallel agents trade tokens for speed.
None of these patterns is wrong, but they are where costs tend to spike without anyone noticing until the bill arrives.
How to see what your agent is using
You can't reduce what you can't see. Each tool exposes usage a little differently.
- Claude Code.
/costis an alias for/usage. For API users it shows an estimated session cost at list price and prompt-cache statistics; subscribers see plan usage bars. In headless mode,claude -p --output-format jsonincludestotal_cost_usdin the result. - Codex CLI.
/statusshows the current session configuration and token usage, and/usageshows account usage.codex exec --jsonreports per-turn counts such asinput_tokens,cached_input_tokensandoutput_tokens. - Provider and gateway dashboards. If your requests go through a router, its dashboard shows usage across every tool that shares the key. Requests sent through Air Router appear in your AirLLM dashboard.
Practical ways to keep token usage down
Most savings come from sending less context and matching the model to the step. The habits that matter most:
- Start a fresh context between unrelated tasks instead of carrying an old history forward.
- Keep project instruction files short and move rarely used procedures out of them.
- Disconnect tools and MCP servers you aren't using, since their definitions are sent every turn.
- Filter noisy tool output, such as full test logs, down to what the agent needs.
- Use a strong model for planning and hard reasoning, and an efficient one for routine steps.
- Avoid switching models mid-session when you can, so the prompt cache keeps working.
For tool-specific advice, see how to reduce Claude Code costs and the broader guide to reducing LLM API costs.
Where routing and context optimization fit
Manual habits help, but they depend on remembering them on every task. An AI coding agent router applies some of the same ideas automatically, at the point where the agent's requests leave your machine.
Air Router sits between your coding agent and the model providers. You point the agent at it by changing the base URL, the API key and the model name. From there, it works on the request rather than on your workflow:
- The right model for each step. Routine steps can go to efficient models and harder reasoning to stronger ones, instead of every step using the same premium model. You choose a routing model, such as
airllm-autofor cross-provider routing orairllm-anthropicto stay within the Claude family. - Less repeated context. The Air Router page describes context deduplication, tool history compaction and language-aware code compression, which reduce repeated files, completed tool history and unnecessary prompt overhead before the request reaches the model.
- Prompt caches kept intact. Air Router is designed to preserve prompt caches, so routing doesn't undo the caching your agent relies on.
- Loop protection. It is designed to detect repeated searches, edits and tool calls and interrupt unproductive loops before they consume more tokens.
How much this changes your bill depends on your workload: some requests have little to remove, and some steps need the strongest model anyway. To set it up, follow the per-tool setup steps in the Developer Guide.
Frequently asked questions
Why does a coding agent use so many tokens for a small change?
Because the request carries much more than your change. The agent sends its instructions, tool definitions, the conversation so far and any files or logs it has read, and in a long session that context can be many times larger than the edit itself.
Are cached tokens free?
Usually not. Providers generally bill cached input at a reduced rate rather than nothing, and the exact rate and cache lifetime depend on the provider and model.
Does a bigger context window mean a bigger bill?
Only if you fill it. You pay for the tokens you send, not for the size of the window, but a large window makes it easy to keep carrying old history and files forward.
Do subagents save tokens?
They keep verbose work out of your main conversation, but each subagent runs in its own context window, so its tokens still count. Anthropic's cost guide says agent teams use approximately 7x more tokens than a standard session.
How can I see token usage in Claude Code or Codex CLI?
In Claude Code, run /cost, which is an alias for /usage. In Codex CLI, /status shows the session's token usage, and codex exec --json reports per-turn input, cached and output tokens.
Related
How to reduce Claude Code costs
Anthropic's own advice on context, models and limits, plus where an LLM router fits.
CategoryWhat is an AI coding agent router?
Why coding agents benefit from routing each step to a suitable model.
ProductAir Router
Intelligent LLM routing for coding agents: model selection, context optimization and loop protection.
GuideWhat is an LLM router?
How model routing works and when it's worth adding to your stack.
Sources
- Claude Code docs — Manage costs effectively
- Claude Code docs — Prompt caching
- Claude Code docs — Subagents
- Claude Code docs — LLM gateway protocol
- Codex docs — Developer commands
- Codex docs — Non-interactive mode
See where your agent's tokens go
Route Claude Code, Codex CLI, Cursor or AirCode through Air Router and follow usage for every tool in one AirLLM dashboard.