Claude Code bills you for every token sent to and generated by Anthropic’s API, and the size of your session context—the conversation history and tool outputs Claude remembers—directly multiplies that cost. Each new request in a session re-sends the entire accumulated context, so a long-running session with many large file reads can cost far more than the sum of its individual prompts. This article explains exactly how session context drives your bill, how to estimate it, and how to keep costs under control.
Formula
The core formula is simple: Total tokens processed = session hours × requests per hour × average context size per request. Each variable is:
- Session hours (t): how long you keep a single Claude Code session open.
- Requests per hour (r): how many prompts or tool calls you make in that hour.
- Average context per request (c): the number of tokens in each request, including the entire conversation history, system prompt, and any file contents you’ve asked Claude to read. This grows as the session goes on.
To convert tokens to cost, multiply by the per-token price. As of this writing, Anthropic charges $3 per million input tokens and $15 per million output tokens for Claude Sonnet 4, and $15/$75 for Opus 4. The calculator above uses the Sonnet input price for simplicity; your actual bill will be higher because output tokens cost more and because context re-sends accumulate.
Worked Example
Suppose you work in a Claude Code session for 2 hours, make 20 requests per hour, and your average context is 50,000 tokens. Plug into the formula:
- Total requests = 2 × 20 = 40
- Total tokens processed = 40 × 50,000 = 2,000,000 tokens
- Estimated cost = 2,000,000 / 1,000,000 × $3 = $6.00
That matches the calculator’s output. But note: the real cost will be higher because output tokens are billed at $15 per million, and because context grows over time—your 50th request might carry 100,000 tokens, not 50,000.
Why Session Context Multiplies Your Bill
Claude Code is a terminal-based agentic coding tool. When you ask it to fix a bug or refactor a function, it doesn’t just send that one prompt. It sends the entire conversation so far: every previous message, every file it has read, every diff it has generated. This is how it maintains coherence across a long task, but it means the cost per request rises linearly with session length. A 10,000-token prompt on request #1 might become a 100,000-token prompt by request #10.
What Counts as Context
Several things inflate your context size:
- Conversation history: every prior user message and Claude response.
- File contents: when you use
@fileor ask Claude to read a file, the entire file is added to context. - Tool outputs: results from grep, ls, tests, or other commands are included.
- System prompt: the base instructions, which are small but constant.
- Code diffs: when Claude edits a file, the diff is stored in context.
You can see your current context size in the Claude Code status bar or by using the /context command. It’s a good habit to check it periodically.
How to Estimate Your Real Cost
To estimate your actual bill, you need to know your average context size and the mix of input vs. output tokens. The calculator above gives a rough figure, but for a more accurate estimate:
- Track your session length and requests per hour (Claude Code logs these in
~/.claude/). - Use
/contextto see the token count at several points and average them. - Assume output tokens are roughly 20-30% of input tokens for typical coding tasks.
- Apply the current API prices: $3/$15 for Sonnet, $15/$75 for Opus.
Example: if your average context is 80,000 tokens, you make 30 requests per hour, and you work 3 hours, that’s 7.2 million input tokens and maybe 1.8 million output tokens. At Sonnet prices, that’s (7.2 × $3) + (1.8 × $15) = $21.60 + $27.00 = $48.60.
Common Mistakes That Inflate Your Bill
- Leaving sessions open: an idle session still holds context; every new prompt re-sends it all.
- Reading huge files: asking Claude to read a 5,000-line file adds ~50,000 tokens to every subsequent request.
- Using Opus for simple tasks: Opus costs 5× more; use Sonnet or Haiku for routine edits.
- Not using /compact: the
/compactcommand summarizes the conversation, drastically reducing context size. - Ignoring the context limit: when you hit the 200k token limit, Claude Code automatically truncates old messages, but that can lose important context and force re-reads.
Strategies to Reduce Session Context Costs
You can cut your bill significantly with a few habits:
- Use /compact regularly: every 10-15 requests, or when context exceeds 50k tokens.
- Start new sessions for new tasks: don’t carry a long debugging session into a feature build.
- Be selective with file reads: instead of
@wholefile, ask Claude to read specific functions or line ranges. - Use the cheapest model that works: Haiku for simple searches, Sonnet for most coding, Opus only for complex architecture.
- Set a session budget: Claude Code has a
--max-turnsflag and you can monitor usage with/usage.
One misconception is that the context window is free—it isn’t. Every token in the window is billed as input on every request. The only way to avoid that is to reduce the window size.
Comparing Claude Code to Other Tools
Claude Code’s pricing is token-based, unlike flat-rate tools like GitHub Copilot. This means heavy users can pay more, but light users pay less. The key difference is that Claude Code’s agentic nature—it can run tests, edit files, and iterate—tends to generate more requests per task than a simple autocomplete tool. However, it can also complete tasks in fewer human interventions, potentially saving time. For teams, Anthropic offers enterprise plans with usage-based billing that may include discounts, but the per-token math still applies.
Frequently Asked Questions
Does Claude Code have a free tier?
Anthropic offers a limited free tier for Claude Code with a small number of requests per day, but it’s not designed for extended use. After that, you pay per token via API credits or a Pro/Max subscription that includes a token allowance. Check the Anthropic docs for current limits.
What is the context limit in Claude Code?
The context window is 200,000 tokens for Claude Sonnet and Opus models. When you exceed that, Claude Code automatically truncates the oldest messages, which can degrade performance. Using /compact before hitting the limit is recommended.
Can I see how much a session cost?
Yes, use the /usage command in Claude Code to see token counts and estimated cost for the current session. You can also check the Anthropic console for detailed API usage and billing.
Does /compact lose important context?
/compact summarizes the conversation, which can lose details. To minimize loss, you can instruct the summarization to preserve specific information, or save important file contents to a scratchpad file that you re-read later.
Is it cheaper to use Claude Code or the web interface?
For short tasks, the web interface may be cheaper because it uses a flat subscription. For long, automated tasks, Claude Code can be more efficient but may cost more in tokens. The trade-off depends on your usage pattern; use the calculator above to estimate your session cost.
Related
Formula used
in t | Session duration | hours | 2 | 0 | 24 in c | Avg context per request | tokens | 50000 | 0 | 200000 in r | Requests per hour | - | 20 | 1 | 1000 out total_tokens | Total tokens processed | tokens | t * r * c | 0 out estimated_cost | Estimated cost (USD) | $ | (t * r * c / 1000000) * 3 | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Philips DST7060/20 7000 Series Iron: Specs & Fit
- Coal Fueled Jet Aircraft: Could It Work?675 L
- Remote Desktop on Raspberry Pi: How to Enable It
- Word Select Last Line in Paragraph?
- Google Assistant IFTTT Recipes: 7 Useful Automations
- OLED TVs in Dark Rooms: Why They Look Better and When They Don’t