Why your coding agent's API bill is so high (and how to cut it)
Updated 13 November 2026
A coding agent doesn't send one question and get one answer. It loops: read some files, call a tool, read the result, decide what to do next, repeat. Every pass is a separate API request, and each request carries the whole conversation so far. That's how one task ends up using millions of tokens when the code it produces fits on a page.
Where the tokens go
The model has no memory between requests, so the agent sends the system prompt, the tool list, your messages and every earlier tool result again on each turn. A conversation that has grown to 60,000 tokens sends about 60,000 input tokens on turn 20, even if you typed one line.
Tool output is usually the biggest part. Reading a large file, dumping a test log or listing a directory puts all of it in the context, and it stays there for every later turn. Output costs more than input too. Anthropic's Sonnet 5.5, for example, is priced at $2 per million input tokens and $10 per million output tokens, so a generated token costs five times a read one. Agents that write whole files or think at length generate a lot.
Sub-agents multiply all of this, because each starts its own conversation with its own context: four working in parallel are roughly four bills (see subagents explained). Retries do the same. A failing test the agent can't fix, or a bad edit it keeps redoing, costs a full request per attempt, and nothing in the loop stops on its own.
What makes a bill jump
| What changed | Why the bill moves |
|---|---|
| A long-running session | The context grows, so every later turn re-reads more. Cost per turn climbs through the session. |
| A larger model | Per-token rates differ many times over between small and frontier models. See what the major APIs charge. |
| Sub-agents or parallel tools | More simultaneous conversations, each with its own context. |
| An unattended run | A loop with no turn limit runs until you notice. Overnight is a common way to find one. |
| Big files and logs in context | One large read is paid for again on every turn after it. |
Ways to cut it
- Start a fresh session between unrelated tasks. Clearing or compacting the context stops old tool output being re-read for the rest of the day.
- Keep big output out of the conversation. Ask for the failing test's error, not the whole log, and point the agent at the part of the file it needs.
- Use a smaller model for routine edits and keep the expensive one for planning and hard debugging.
- Set a turn or iteration limit if your tool has one, so a loop ends by itself.
- Watch the usage page for the first few hours of any new setup, before you leave anything running overnight.
Stopping a runaway loop
A loop usually shows as the same tool call or the same edit repeating. Stop the agent and look at what it kept hitting: a test it can't pass, a permission it doesn't have, a file that doesn't exist. Give it the missing fact or change the task. Telling it to try again just repeats the loop. An unattended agent should also have its own API key with a stream cap, so it can't take over everything else you run. See one key per project and leaving an agent running overnight.
When a flat rate is the simpler fix
The tips above lower the number of tokens. They don't change the fact that on a per-token API every extra token is money, which makes an agent that explores, retries and runs sub-agents the most expensive way to use one. A flat-rate plan charges for how many requests run at once, so a long session or a retry costs nothing extra. The trade is one open model and a cap on parallel streams. What a day of agent work costs and the cost calculator show which side of the line your usage is on.
Why is my Claude Code or Codex API bill so high?
Is a high token count a sign the agent is broken?
Will a flat rate stop an agent from looping?
- Anthropic: Claude Sonnet 5.5 (API price per million tokens) (checked 13 November 2026)
- NeverMetered pricing and plan limits (checked 13 November 2026)
- How NeverMetered runs (checked 13 November 2026)