Claude API pricing is per million tokens, with separate rates per model for uncached input, output, cache writes and cache reads. Output costs several times input; cache reads cost a fraction. For coding agents the replies are not the expensive part: re-read context is, and how many times it gets re-read.
The four token types on a Claude API bill
Each response reports input_tokens, cache_creation_input_tokens, cache_read_input_tokens and output_tokens, each at its own rate. For agents the ratios matter more than the prices.
Input vs output tokens: why input dominates for agents
An agent sends instructions, tools, history and every file and command output, gets a short step back, and sends it all again. Each request is mostly input it has sent before.
- cache read67.7%
- cache write18.9%
- output13.2%
- uncached input0.2%
How prompt caching changes Claude API cost
A request that starts with the same bytes as a recent one reads the shared prefix at the discount. Claude Code sets the breakpoints.
- 1Warm while active
The default cache lives about five minutes and refreshes on every use.
- 2Idle gaps break it
Past the lifetime, the next request writes the whole context again.
- 3Prefix changes break it
A new tool list, system prompt or model invalidates everything after it.
- 4Cheap is not free
Re-reading 300k tokens a turn still bills 300k cache reads a turn. See caching vs compression.
Worked example: Claude tokens to dollars for one agent turn
One mid-session turn: 150,000 cached tokens, 3,000 new tokens of tool output and a 500-token reply, at the Sonnet 4.5 ratios.
| Part of the turn | Tokens | Rate | Input-token equivalents |
|---|---|---|---|
| Cache read | 150,000 | 0.1× | 15,000 |
| Cache write, 5 minutes | 3,000 | 1.25× | 3,750 |
| Output | 500 | 5× | 2,500 |
| Total | 21,250 |
- cache read70.6%
- cache write17.6%
- output11.8%
Multiply 21,250 by the input rate per million and divide by one million for dollars. Repeat with growing context and cost outruns turn count.
Estimating Claude API cost for your own agent
- 1Read it live
/costin Claude Code on an API key. - 2Log the four fields
For your own agent, record usage from every response and price it per model.
- 3Divide by finished tasks
Not requests (why). Then see Claude Code cost and subscription vs API key.
How to lower Claude API cost for coding agents
- 1Keep the cache warm
Stable prefix, no long idle gaps, model switches only at task boundaries.
- 2Cheapest model that finishes
Sonnet or Haiku for routine steps, Opus where it saves turns.
- 3Send less context
Fresh sessions, fewer tools, shorter output. The Batch API discount does not help interactive agents.
- 4Compress the history
condense shrinks re-sent context and freezes compressed spans so they stay cached: $154 to $43 on Sonnet, $771 to $214 on Opus.
Savings grow with session length and are negative on very short sessions. Anthropic still bills you directly; condense charges 15% of what it saves, and organizations can split traffic to measure it against a control.
Frequently asked questions
Why are Claude output tokens more expensive than input tokens?
Anthropic prices output at several times input. In agents it is still a minority of the bill, because re-sent input is much larger.
What is the difference between cache write and cache read tokens?
A cache write stores a prompt prefix at slightly above input price; a cache read reuses it later at a steep discount.
How long does the Claude prompt cache last?
About five minutes by default, refreshed on each use; a longer option costs more to write. After an idle gap the context is written again.
How do I convert Claude tokens to dollars?
Multiply each usage field by its per-million rate, divide by one million and add them up. In Claude Code, /cost does it for you.
Is Opus more expensive than Sonnet on the API?
Yes. One 938-turn session cost $154 on Sonnet and $771 on Opus at list rates.