# Cost control > Where the tokens actually go in an agentic workload, and the four levers that move the bill. _Source: https://aiprimetech.io/docs/guides/cost-control/ · Home > Docs > Working with the API_ If you have run a coding agent for a full working day you have seen the bill. Agentic loops are token-hungry in a way ordinary chat never is: every file read, every tool result and every diff lands in context and is resent on the next turn. ## Output tokens are the expensive ones On every frontier model, output costs 4–5× input. A model that thinks out loud in its final answer burns money faster than one that answers tightly. Cap `max_tokens` deliberately and prefer concise models for routine work. | Model | $/M in | $/M out | Ratio | |---|---|---|---| | `claude-opus-4-8` | $3 | $15 | 5× | | `claude-sonnet-4-6` | $0.90 | $4.50 | 5× | | `claude-haiku-4-5` | $0.40 | $2 | 5× | | `gemini-3-flash` | $0.20 | $1.20 | 6× | ## The four levers 1. **Route by task.** Classification, extraction and routing do not need Opus — Haiku does them at roughly a seventh of the price. Reserve the flagship for reasoning-heavy work. 2. **Prompt caching.** Stable system prompts and tool definitions are re-read from cache at a fraction of the input price. For agents this is the single biggest lever — see [Prompt caching](/docs/guides/prompt-caching/). 3. **Trim context each turn.** Do not resend an entire transcript; keep recent turns plus a running summary. Unbounded context growth is what turns a $2 session into a $20 one. 4. **Cap the loop.** Set a hard step limit on autonomous runs so a stuck agent cannot spin forever. ## Estimating before you run ```python IN_RATE = {"claude-opus-4-8": 3.0, "claude-sonnet-4-6": 0.9, "claude-haiku-4-5": 0.4} OUT_RATE = {"claude-opus-4-8": 15.0, "claude-sonnet-4-6": 4.5, "claude-haiku-4-5": 2.0} def cost(model, tok_in, tok_out): return tok_in / 1e6 * IN_RATE[model] + tok_out / 1e6 * OUT_RATE[model] print(cost("claude-opus-4-8", 20_000, 2_000)) # 0.09 print(cost("claude-sonnet-4-6", 20_000, 2_000)) # 0.027 print(cost("claude-haiku-4-5", 20_000, 2_000)) # 0.012 ``` The same 20K-in / 2K-out task costs 7.5× more on Opus than on Haiku. If the task is classification, that multiple buys nothing. ## Measuring after Every response carries a `usage` object. Log it against a request id and a task type — without that, cost optimisation is guesswork. The gateway dashboard shows per-model usage, but only your own logs know *why* a request was made. > The most common expensive mistake is not the model choice — it is resending a growing transcript on every turn of a long agent session. Fix context growth first, then tune models. - [Prompt caching](https://aiprimetech.io/docs/guides/prompt-caching/) — The biggest single lever - [Context management](https://aiprimetech.io/docs/guides/context-management/) — Keeping history bounded - [Models](https://aiprimetech.io/docs/getting-started/models/) — Rates for every model --- _ClaudeAPIKey.dev is an independently operated, Anthropic-compatible API gateway. Not affiliated with Anthropic._