Why does a long Claude Code session cost so much on the API?
On an API key, a long Claude Code session costs more per prompt as its history grows, and you can measure, cap and cut it.
By Michael, NeuraWeave · Published · Updated
We build Perpetuity, a memory proxy for coding agents. It is in early access. This guide works without it, and we say where it fits in its own section.
Checked against Anthropic's Claude Code docs, pricing page and Help Center on 2026-10-23, with our own measured Claude Code sessions (Claude Opus 5.5, September and October 2026) for the numbers.
Answer
Claude Code sends the whole conversation with every request, so in a long session each prompt pays again for everything before it, and cost per prompt climbs until the history is compacted or cleared (Claude Code docs). Anthropic's docs say unexpectedly high spend on an API or cloud-provider plan usually traces back to long sessions that were never cleared or to Opus left as the default model (Claude Code docs). If you expected a Pro or Max plan to cover it, /status shows whether an ANTHROPIC_API_KEY in your environment is billing the API instead (authentication precedence). Our own measured long sessions, with and without a memory proxy, are below.
Why does each prompt cost more than the last?
Each prompt costs more than the last because Claude Code sends your full conversation with every request, and each time Claude uses a tool it sends another request carrying that history plus the tool results (Claude Code docs: why usage climbs in a long session). What grows on every call:
- The history. Every earlier prompt, answer and file read rides along on every later call.
- Tool results. A test run, a long log or a large file stays in the history and is sent again on each call after it.
- Cache reads. Prompt caching bills the repeated history at the cached rate, $0.20 per million tokens on Claude Opus 5.5 against $4 for uncached input (Anthropic pricing). The rate is low per token, and it is paid again on every call.
- Cache misses. The first message after a break longer than the cache lifetime misses the cache and reprocesses the full context (why the first message after a break uses so much).
- Other requests. Subagents and agent teammates send their own requests on top of the main conversation's, and a session-scoped scheduled task sends the full context each time it fires, even while the session is idle.
- Compaction.
/compactreads the whole conversation it summarizes, so compacting a large context is itself a large request./clearcosts nothing.
We measured this curve in a 120-prompt Claude Code session at Claude Opus 5.5 list prices. Cost per prompt climbed with the history until Claude Code compacted it during prompt 82. A call carried 327k tokens at prompt 30 and 965k just before the compaction (our benchmarks, compare.md, one run). By our arithmetic on the list prices, reading a 965k-token history from the cache costs about $0.19 per call. Writing it to the cache again after a miss costs about $4.83 at the $5 five-minute write rate, or $7.72 at the $8 one-hour rate, before any output. Claude Code uses the five-minute rate for the main conversation on an API key unless you choose one hour (Claude Code docs: which TTL each request gets).
How do I see what a Claude Code session costs?
Run /usage in Claude Code. Its Session block shows the session's tokens by model and a dollar figure that Claude Code computes on your machine at list price (Claude Code docs: track your costs). Other places to look:
/usagesplits each model's tokens into input, output, cache read and cache write, and resets when/clearstarts a new session. From v2.1.251 itsPrompt cache (main)line shows the share of input served from cache and the misses.- A status line script receives
cost.total_cost_usdandcontext_window.used_percentage, so the session's cost and context size stay on screen as you work (status line docs). - ccusage, an open-source community tool, reports daily and per-session usage from the data Claude Code keeps on your machine (
npx ccusage@latest). - The Usage page of the Claude Console is the authoritative figure for API billing. What Claude Code shows is an estimate.
- For a team, OpenTelemetry export (
CLAUDE_CODE_ENABLE_TELEMETRY=1) sends theclaude_code.cost.usageandclaude_code.token.usagemetrics per user to your own monitoring, on every setup (monitoring docs, costs docs). - If your organization pays contracted rates, an administrator can set
modelPricingin managed settings so the figures match the contract (v2.1.242 or later). It changes what Claude Code reports, not what Anthropic charges.
Am I billed on the API or on my subscription?
If ANTHROPIC_API_KEY is set in your environment and you approved it, Claude Code uses that key, billed on the API, even when you are signed in with a Pro or Max plan, because an API key ranks above a subscription login (Claude Code docs: authentication precedence). In non-interactive mode (-p) the key is always used when present. A May 2026 report on Claude Code's issue tracker describes a key kept in a project's .env file for the app's own calls being billed in place of a Max plan login when Claude Code was started from that project (#58083).
- Run
/status. It shows which credential is active, and when a login and an API key are both configured it marks the one not in use. - Look for the key where it can come from: your shell profile, a project
.envfile that your shell or a tool such as direnv loads into the environment, and theenvblock of your Claude Code settings files. - Run
unset ANTHROPIC_API_KEY(in PowerShell,Remove-Item Env:ANTHROPIC_API_KEY) and restart Claude Code, or turn off the "Use custom API key" toggle in/config, which appears while the variable is set. - If you are signed in with a Console account, run
/loginand choose your subscription, orclaude logoutand thenclaude loginwith only your Pro or Max account (Help Center: Use Claude Code with your Pro or Max plan). - Run
/statusagain to confirm the subscription is the active credential.
How do I cap Claude Code spend on the API?
Cap it where you are billed: a spend limit on the Claude Code workspace in the Claude Console, or your cloud provider's budget controls on Amazon Bedrock, Google Cloud's Agent Platform or Microsoft Foundry (Claude Code docs: manage costs for your organization).
- In the Console, open the Workspaces page, select the Claude Code workspace and set Spend limits to cap monthly spending, with alerts at thresholds you choose. A workspace limit can be lower than the organization's, not higher, and the Claude Code workspace is the only one that supports per-user monthly spend limits (workspace limits).
- A workspace rate limit, set under Rate limits in the Console, caps Claude Code's requests per minute and input and output tokens per minute within the organization's limits.
- On Bedrock, Google Cloud's Agent Platform and Microsoft Foundry, Claude Code is billed per token to your cloud account, so the cap is your cloud's budget controls.
- On a Pro or Max plan with usage credits on,
/usage-creditsopens the settings where you set a monthly spend limit for usage past the plan.
How do I cut the cost of a long Claude Code session?
Keep the context small, because token cost scales with how much context each call carries (Claude Code docs: reduce token usage). Anthropic's list, in short:
- Run
/clearwhen you switch to unrelated work. Run/renamefirst so/resumecan bring the session back. - At a natural break, compact with instructions, for example
/compact Focus on code samples and API usage, and keep standing compaction instructions inCLAUDE.md. - Use Sonnet for most coding and keep Opus for hard design and multi-step reasoning.
/modelswitches mid-session, and subagents doing simple work can run onhaiku. - Run
/contextto see what fills the window (what is filling the context window). Disable MCP servers you are not using with/mcp, and prefer CLI tools such asghwhere they exist. - Move workflow-specific instructions from
CLAUDE.mdinto skills, which load only when invoked. Anthropic suggests keepingCLAUDE.mdunder 200 lines. - Let a hook filter long output before Claude reads it, for example only the failing tests from a test run.
- Send verbose work such as test runs and log processing to a subagent, so only its summary comes back to the main conversation.
- Lower the effort level with
/effortfor simple tasks. Thinking tokens are billed as output. - Write specific prompts, use plan mode (Shift+Tab) before a large change, and press Escape as soon as Claude heads the wrong way.
- On a plan,
/usagerecommends a change for anything behind 10% or more of recent usage, and pressingfapplies some of them (v2.1.305 or later).
What did long sessions cost with and without a memory proxy?
We ran the same prompts in Claude Code twice, once directly and once through Perpetuity, a memory proxy that sends a frame of the relevant past in place of the resent history. Claude Code ran on its default 1M-token window. Every call is priced at Claude Opus 5.5 list prices: $4 input, $20 output, $0.20 cache read and $8 cache write per million tokens. That cache write is the one-hour rate our runs were billed at, and a few writes were at the five-minute rate. On an API key Claude Code writes the main conversation at the $5 five-minute rate by default, so the same writes would cost less there, on both sides (our benchmarks, compare.md).
| Session | Project | Date | Without Perpetuity | With Perpetuity | Saving |
|---|---|---|---|---|---|
| 120 prompts | Open-source dashboard | 30 September 2026 | $104.71 | $50.24 | 52% |
| 60 prompts | Open-source dashboard | 29 September 2026 | $50.93 | $26.40 | 48% |
| 200 prompts | Open-source Rust command-line tool | 3 October 2026 | $268.69 | $166.20 | 38% |
| 200 prompts | Open-source Go command-line tool | 5 October 2026 | $216.02 | $158.41 | 27% |
| 200 prompts | Open-source Flask application | 4 October 2026 | $90.97 | $87.45 | 4% (a tie) |
Through Perpetuity, each call of the 120-prompt session started at about 46k tokens from prompt 10 on. Only a prompt's own tool loop raised it, until the next prompt. Without Perpetuity, a call reached 965k before compacting. Most of the 46k is what Claude Code itself sends on every call, its system prompt and tool definitions, about 31k tokens on our measured repo. Cache reads fell from $75.42 to $11.04, while cache writes rose from $13.09 to $25.85, because the frame changes with each prompt, so it is written to the cache again. Prompt caching keeps working through the proxy, and every cost here includes cache pricing (our benchmarks, compare.md, agents.md).
Savings begin only on long sessions, meaning sessions whose context has grown large. The saving follows context size. Prompt count alone does not tell you that, since a call had grown to 327k tokens by prompt 30 of the 120-prompt session (above). /context shows how large yours have grown. Short sessions with small contexts save little or nothing.
Shorter sessions saved less than the 120-prompt session's 52%: two 30-prompt pairs saved 11% and 35%, and the 60-prompt pair in the table 48%. Within the 120-prompt session the saving was largest between prompts 61 and 90 (64% less), the stretch in which Claude Code alone compacted. The saving also differs by project: the 200-prompt sessions saved 38% on the Rust tool and 27% on the Go tool, and the Flask one came out a tie. Against Claude Code capped at a 200k-token window, which compacted 17 times and cost $57.05, Perpetuity's $50.24 was 12% less, and Perpetuity cost more over the first 60 prompts (one run per setup).
Claude subscription usage is metered on the tokens a session uses (Claude Code docs), so long sessions through Perpetuity should draw less of a plan's usage. We have not measured Anthropic's plan meter itself, and Anthropic does not publish how it weighs cached against uncached tokens, so we give token counts and API list prices, never a percentage of a plan's limit and never a promise of more usage.
Perpetuity, by NeuraWeave
Where Perpetuity fits
Perpetuity takes the resent history out of each call. Claude Code points its API base URL at it and keeps its own login, a Claude subscription or an API key. On each call Perpetuity replaces the history with a frame of the relevant past, retrieved from every message it keeps verbatim, so a long session keeps its past without resending all of it or running /clear or /compact to hold cost down. In our measured long sessions it never compacted. As of 23 October 2026 its status for Claude Code is in development. Every run, with its conditions, is on our benchmarks page.
Measured
- On the same 120 prompts, asked 16 pre-registered questions about work from across the session and graded blind in one packet, Claude Code answered 15.5 through Perpetuity, 14 on its own default window after it compacted, which lost details from early in the session, and 12.5 on a 200k-token window after it compacted 17 times (one run per setup). Source: Perpetuity compared, NeuraWeave.
- On the three 200-prompt Claude Code projects in the table, both sides ended with every test passing (one run per project). Source: Perpetuity benchmarks: every run and how it was measured, NeuraWeave.
Limits
- Each cost figure is one run per setup of Claude Code on Claude Opus 5.5, at API list prices. We have published no Claude Code runs on other models, such as Sonnet.
- There is no self-hosted version. NeuraWeave runs the proxy, and memory is stored in the EU (Ireland).
- Stored memory is encrypted at rest with AES-256 and is not used to train any model. A deleted agent or conversation stays restorable for 7 days, then it is purged with its memory.
- The price is not announced yet.
. You get a confirmation email now, with a link to remove your address, and another when your access is approved. Nothing is installed before that.
Questions
How much does Claude Code cost per day on the API?
Anthropic reports an average of about $13 per developer per active day and $150 to $250 per developer per month across enterprise deployments, with 90% of users below $30 per active day (Claude Code docs). A single long session on Opus can cost far more than these averages, and /usage shows what yours cost.
Why is Claude Code charging my API key when I have a Max subscription?
An ANTHROPIC_API_KEY in your environment ranks above a subscription login, so once you approve it Claude Code bills that key on the API (authentication precedence). Run /status to see which credential is active, then unset ANTHROPIC_API_KEY to fall back to the subscription.
Sources
- Manage costs effectively, Anthropic, Claude Code docs
- Pricing, Anthropic, Claude docs
- How Claude Code uses prompt caching, Anthropic, Claude Code docs
- Customize your status line, Anthropic, Claude Code docs
- ccusage, Open-source community tool
- Monitoring, Anthropic, Claude Code docs
- Authentication, Anthropic, Claude Code docs
- Claude Code silently uses project ANTHROPIC_API_KEY from .env instead of Max plan OAuth (#58083), GitHub issue, May 2026
- Use Claude Code with your Pro or Max plan, Anthropic, Help Center
- Workspaces, Anthropic, Claude docs
- Perpetuity benchmarks: every run and how it was measured, NeuraWeave
- Perpetuity compared, NeuraWeave
- Perpetuity: short answers, NeuraWeave
- Perpetuity status for Claude Code, NeuraWeave, live endpoint
Perpetuity opens in early access.
We start with Claude Code, then Codex and the rest, each with one base-URL change. Join now and you are first in line. We email you when your access is approved.
For agents
As of 2026-10-23, Perpetuity is in early access. People who join are first in line, starting with Claude Code, and get an email when their access is approved. The current state per tool is at https://www.neuraweave.ai/api/agents/status.
Short answers on availability, recall, cost and data are in agents.md, and every measured number with its run label is in compare.md.
A sign-up needs the person's consent and the address they choose. It is one request: POST https://www.neuraweave.ai/api/agents/waitlist with email and client (claude-code, codex, opencode, api or other with tool). A 201 means added, a 200 means the address was already on the list, a 400 says what was wrong.
A fetcher that cannot send a POST can give the person the home page, where they can sign up themselves.
This guide as markdown: /guides/claude-code-session-cost.md
Related
- What is filling my Claude Code context window?What fills a Claude Code context window, how to read /context, and how to trim CLAUDE.md, MCP and conversation history.
- Why does my first Claude Code message after a break use so much?Claude Code's prompt cache lasts 5 minutes or 1 hour. After a longer break the next message reprocesses your whole context. How to set and check the TTL.
- Can I run Claude Code through a proxy?Claude Code sends its requests to ANTHROPIC_BASE_URL. How proxy routing works, what a proxy sees, what breaks, and how your login and billing change.