Why Coding Agents Use So Many Tokens: How Claude Code and Codex Spend, and How to Control It
How can one coding task use over a million tokens? Usage broken down into the agent loop, context growth, tool output and reasoning tokens, followed by practical controls — caching, session compaction, tiered models and spend caps.
The first time people complete a full feature with Claude Code or Codex, the request logs often come as a shock: one sentence typed, dozens of calls in the background, and input tokens adding up past a million. That isn't a billing problem — it's how coding agents work. Once you know where the usage comes from, you know where to save.
How an agent works
A coding agent is a loop:
- Send the task, the system prompt, the tool definitions and the conversation so far to the model;
- The model decides the next step — read a file, search the code, run the tests;
- The client performs the action and appends the result to the conversation;
- Back to step 1, until the model considers the task done.
The key is step 1: the model doesn't remember the previous round, so the full context is sent again every round.
Where the usage comes from
Context accumulates round by round
Say a task runs for 30 rounds, the system prompt and tool definitions are about 20K tokens, and each round adds 3K tokens of file content and tool output on average. By round 30 a single request already carries over 100K tokens of input; the total input sent across 30 rounds — the sum of every round — easily passes a million.
That is how "one sentence" turns into a million tokens.
Tool output is often long
Reading a big file, printing a full test log or listing a whole directory all go into the context — and get sent again in every round after that.
Reasoning tokens
At higher reasoning effort, each round's thinking is billed at the output price, which adds up over long tasks.
Hidden calls
Summarising a session title, compacting history, checking command safety, sub-agents searching in parallel — clients make extra calls you never see.
The good news: most input hits the cache
Because every round appends to the previous one, the prefix stays the same, and most of the repeated input hits the prompt cache at the cache-read price. On Claude Opus 5.5 and Sonnet 5.5, cache reads are just 5% of the base input price. So what really drives the bill is usually cache hit rate and output volume, not total input tokens. See How prompt caching works.
Seven controls
- One task, one session: start a new session when a job is done so unrelated history doesn't tag along.
- Compact long sessions: Claude Code's
/compactand Codex's automatic compaction shorten the history and every later round's input. - Point to the files that matter: tell the agent which directory and files to look at, so it doesn't read the whole repo; exclude build output and dependency folders.
- Don't rewrite early messages: editing or deleting history invalidates every cache after it.
- Tier your models: Opus or Sol for the main task, Haiku or Luna for simple sub-tasks. In Claude Code,
ANTHROPIC_DEFAULT_HAIKU_MODELsets the model used for small tasks. - Lower reasoning effort:
mediumis enough for most routine changes. Codex usesmodel_reasoning_effort; Claude useseffort. See Choosing reasoning effort. - Give the key a spend cap: create a dedicated API key for your coding tools with a spend cap, so a runaway loop can't drain your balance.
Using request logs to find the expensive parts
Filter Request logs in the UnioAPI console by key to see every call your coding tool made:
- Input tokens keep climbing but cache reads stay low: the prefix keeps changing — look for per-round content placed near the start.
- The same request repeats within seconds: the client may be retrying or the agent is stuck in a loop; stop the task.
- The reasoning column is always high: consider lower effort for routine tasks.
Connecting on UnioAPI
- Claude Code: uses the official Anthropic protocol; set
ANTHROPIC_BASE_URLandANTHROPIC_AUTH_TOKEN. See Claude Code setup. - Codex: uses the Responses API; add a
[model_providers.unioapi]block toconfig.toml. See Codex setup. - One key works for both tools; every real call has its own request log and is charged by actual token usage.