跪拜 Guibai
← All articles
Backend · Programmer · Artificial Intelligence

Claude Code Sessions Bleed Tokens. Here's Where They Go.

By cxuanAI ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Agentic coding tools bill by token, not by task. A developer who ignores Session hygiene can burn 5–10x more credits for identical results, turning a cost-effective tool into a surprise expense. These mechanics apply equally to subscription quotas and API-key billing.

Summary

A single Claude Code Session can consume wildly different token counts for the same fix, depending on whether the model searches blindly or is pointed directly at the right files. Each round re-sends the entire conversation history, so irrelevant files and noisy command output compound costs across every subsequent request. The official guidance breaks down where tokens actually go: input prefill, output decode, and prompt caching.

Prompt cache gives a 10x discount on repeated prefixes, but it breaks when you switch models, change effort levels mid-Session, or let the cache expire after an hour. Using @-mentions instead of letting Claude read files saves tool-call rounds. Quiet flags on test and build commands prevent thousands of lines of passing-test output from permanently occupying the context window.

The highest-leverage fixes are the simplest: run /clear between unrelated tasks, check /context before starting work, and compact before stepping away. Subagents isolate noisy analysis from the main Session, but for small tasks they add overhead. Long-running loops belong in a separate terminal with a clean Session to avoid cache-miss reprefills.

Takeaways
Run /clear between unrelated tasks so leftover context from a previous job doesn't pollute the next one.
Use @-mention to attach files directly instead of telling Claude a filename; it saves a Read tool call and one full round-trip.
Add quiet flags to test, build, and log commands, or offload noisy work to a Subagent whose context gets discarded afterward.
Check /context at the start of a fresh Session to see how much space CLAUDE.md and MCP tool definitions already consume.
Run /compact before leaving the keyboard; prompt cache expires after one hour, and compacting while the cache is hot costs far less.
Switch models or effort levels only at Session start or right after /clear — doing it mid-conversation breaks the prompt cache and triggers a full reprefill.
Long-running /loop tasks should run in a separate terminal with a clean Session to avoid cache-miss reprefills every hour.
Set MAX_THINKING_TOKENS=0 for Sessions that are purely repetitive; it disables extra thinking budget below even /effort low.
Conclusions

Token cost is wildly asymmetric: a request might carry tens of thousands of input tokens but generate only a few hundred output tokens, yet output tokens cost 5x more per token.

Prompt cache is automatic but fragile — it matches from the very start of a request, so any change to the prefix (model, effort, fast mode) invalidates the entire cache and forces a full-price reprefill.

Command output under 30,000 characters enters the context verbatim and stays for the entire Session; a test runner printing 400 passing tests line-by-line silently bloats every subsequent round.

Subagents solve context pollution but create a trade-off: they can't see the main Session's history, so they sometimes re-read files the main Session already loaded, adding their own token cost.

The most expensive Session pattern is a long, unfocused conversation where round 40 must re-read the cached context of the previous 39 rounds, each carrying accumulated noise.

Concepts & terms
Prefill
The first phase of a model request where Claude reads the entire current context (system prompt, CLAUDE.md, conversation history, file contents) before generating anything. Priced at the standard input token rate.
Decode
The second phase where the model generates output token-by-token — thinking tokens, tool calls, and the final response. Costs roughly 5x more per token than prefill because the GPU must run continuously.
Prompt cache
A server-side optimization that reuses computed state when a new request starts with the same prefix as a recently processed request. Cache reads cost ~0.1x the normal input price; cache writes cost up to 2x. Automatically managed by Claude Code but easily invalidated by switching models, effort levels, or fast mode mid-Session.
Thinking tokens
Internal reasoning tokens the model generates before producing visible output or tool calls. Controlled by /effort; they count as output tokens and are billed at the higher decode rate. MAX_THINKING_TOKENS=0 disables them entirely.
Subagent
A Claude Code feature that spawns a task in an isolated context window with its own system prompt and tools. The Subagent returns only the final answer to the main Session; all intermediate file reads, command output, and thinking are discarded when it finishes.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗