How much does Claude Code cost?
What a Claude Code session costs on an API key, why a coding agent uses so many tokens, and six ways to bring the monthly bill down.
Claude Code can run on a Claude subscription or on an API key, and the two bill differently. On an API key you pay per token with no ceiling, which is why people are surprised by the bill after a heavy week: a coding agent doesn't send a prompt, it sends the whole conversation and the files it has read, again, on every step. This page explains what a session actually consumes, how to see what you spent, and what to change when it's too much.
What a Claude Code session spends
An agent works in loops: read a file, run a command, edit, run the tests, think again. Each loop is one API request, and each request contains everything so far: the system prompt, the tool definitions, the earlier messages, the tool results. Three consequences:
A long session sends hundreds of thousands of input tokens for every few thousand it writes.
Claude Code relies on prompt caching, so most of that repeated input is billed at the cache-read price, a small fraction of the normal one. A session whose cache keeps working costs a fraction of one whose cache keeps missing.
The same session on a larger model costs a multiple of one on a smaller model.
At Half Off Dev every one of those prices is half of Anthropic's list price, and the gateway keeps each session on the channel that holds its cache, so the cache stays warm for the whole session.
A realistic turn, priced
One mid-session step on claude-sonnet-5.5: 3,000 new input tokens, 50,000 read from the cache, 1,500 written to the cache and 1,200 tokens of output.
| Token kind | Tokens | You pay per 1M | Cost |
|---|---|---|---|
| Uncached input | 3,000 | $1.00 | $0.003000 |
| Cache read | 50,000 | $0.05 | $0.002500 |
| Cache write | 1,500 | $1.25 | $0.001875 |
| Output | 1,200 | $5.00 | $0.006000 |
| Total | $0.013375 |
A task of fifty such steps is about fifty times that. The number for your project depends on the model, the size of the repository the agent reads and how often it loops, so measure one real task instead of trusting an average.
See what you spend
Open Logs in the dashboard: every request is listed with its model, its input, cached and output tokens and its cost. Run one task, sort by cost, and look at two things: how many steps it took, and what share of input was cached. Set a spend cap on the key (API keys) so an agent left running overnight can't run away.
Six ways to spend less
- Match the model to the step. Use a mid-sized model for everyday edits and keep the largest for planning or hard debugging. Claude Code takes one main model and one small background model, set with
ANTHROPIC_MODELandANTHROPIC_DEFAULT_HAIKU_MODEL. - Keep sessions focused. A fresh session for a new task starts with a small history. A single session carried across five unrelated tasks drags all of it into every request.
- Don't break the cache. Edits to the system prompt or tool list mid-session, and idle gaps longer than the cache lifetime, force a re-write. Work in bursts.
- Point it at the files that matter. Telling the agent which directory and files are relevant beats letting it read half the repository to find out.
- Clear or compact long history when you change subject.
- Pay half the price per token. The setup is two environment variables:
export ANTHROPIC_BASE_URL=https://halfoff.dev
export ANTHROPIC_AUTH_TOKEN=$HALFOFF_API_KEY
claudeFull details, including settings.json and error fixes, are in Claude Code.
Frequently asked questions
How much does Claude Code cost per month?+
On an API key it depends entirely on use: a few focused tasks a week cost very little, a full-time coding day costs much more. Measure one task in Logs, multiply by how many you do, and check the result in the Claude Code cost calculator.
Is Claude Code free?+
The tool is free to install. You pay for the model, either through a Claude plan or per token on an API key.
Can I use Claude Code with an API key instead of a subscription?+
Yes. Set ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN as above and Claude Code talks to the API on your prepaid balance. Claude Pro vs the API compares the two.
Why does Claude Code use so many tokens?+
Because every step resends the conversation and the tool results. The cache turns most of that into cheap reads, which is why keeping sessions focused and uninterrupted saves more than shortening prompts.
Which model is best for Claude Code?+
The mid-sized model is a good default for day-to-day work; use the largest when a task keeps failing on the smaller one. Opus vs Sonnet vs Haiku has the details.
For the lower-level levers, see the cheapest way to use the Claude API. New here? Create an account and make a key.