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

Each turn sends the whole conversation again, plus one new step (the darker cap). An illustration of the shape, not measured data.

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.

Prompt caching softens this. Providers that cache the unchanged start of a prompt charge a lower rate for the cached part, so a long session costs less than its raw token count suggests. It depends on the provider and the tool, and it doesn't bring the bill to zero.

What makes a bill jump

What changedWhy the bill moves
A long-running sessionThe context grows, so every later turn re-reads more. Cost per turn climbs through the session.
A larger modelPer-token rates differ many times over between small and frontier models. See what the major APIs charge.
Sub-agents or parallel toolsMore simultaneous conversations, each with its own context.
An unattended runA loop with no turn limit runs until you notice. Overnight is a common way to find one.
Big files and logs in contextOne 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?
Agents resend the full conversation on every turn and run many turns per task, so input tokens add up much faster than your own messages suggest. Long sessions, large tool outputs, sub-agents and loops are the usual causes.
Is a high token count a sign the agent is broken?
Not by itself. Normal agent work reads a lot of context. It's a problem when the same step repeats with no progress, which you can see in your tool's transcript or in the requests list.
Will a flat rate stop an agent from looping?
No. A loop still wastes your time, and on a flat plan it ties up a stream. What changes is that a loop no longer costs money per request. Still set a turn limit for unattended runs.
Sources