Українська версія

Author: abavyka

Context and cache: why your session goes dumb and what it costs

In an agent the context clogs up faster than in a chat. What exactly lands in there and how to live with it.

Claude chats have their own CLAUDE.md: a project has instructions, and uploaded files (project knowledge) get mixed into every chat in that project before your message even goes in, so they eat context too. When there are so many files that they get close to the window limit, RAG kicks in automatically: Claude no longer keeps the whole base in context, it searches it for the chunks it needs for the specific question. The rule is the same as for CLAUDE.md: keep instructions short and to the point, reference docs go into files.

What’s in the context before the first message
• System prompt: Claude Code’s instructions and the tools description
• CLAUDE.md (global and project) + auto memory
• MCP tool names and short skill descriptions
Your prompt against all that is a drop in the ocean.
What gets added while you work
• Every file it reads. That’s the main space hog
• Command output: tests, grep, logs
• Claude’s own replies
The kicker is that the terminal shows just «Read auth.ts» in one line, while the whole file went into the context.
Why it goes dumb
• The model has a limited “attention budget”. The more text, the worse it holds on to details. Anthropic calls this context rot. Meaning quality drops way before the window runs out.
• Leftovers from previous tasks compete with what you need right now.
• When the context is almost full, Claude Code does an auto-compact: squashes the conversation into a summary. The session doesn’t die, but the full logs and intermediate reasoning are gone, what’s left is the summary and a few of the last changed files. Hence the classic “but we agreed on this 20 minutes ago”.
• Some models have a 1M token window. That pushes the problem back, it doesn’t fix it.
What to do about it
• /context — see who ate up the space
• • (Status line — so you don’t have to type /context every time, you can put a permanent line under the prompt with the context percentage, the model, the 5h and 7d subscription limits with time until reset. Set up with one command, Claude Code generates the script itself and writes the settings:
/statusline [your wishlist. Runs locally, doesn’t eat tokens)
• /clear — between unrelated tasks (a backend bug and a form layout are two different sessions)
• /compact focus on auth bug — squash it yourself with a focus during a break between tasks, instead of waiting for the auto one in the middle of work
• /rewind — if you went the wrong way, roll back instead of spending half the session typing “no, let’s do it differently”
• Specific prompts: “fix in auth.ts” instead of “find where our auth lives”, then the agent reads fewer files
• Keep important rules in CLAUDE.md: after a compact it gets pulled in again from disk
• Hand big research off to a subagent: it reads files in its own context and returns only a short summary (subagents get their own post)
Cache
The model remembers nothing, so on every message Claude Code sends the whole context again. Without a cache every turn would mean fully processing the whole history. With the cache the API remembers the start of the request (the prefix), and if it matches a recent one, it rereads it way cheaper and faster: roughly 10% of the normal input price. Claude Code does it all by itself, but there’s a catch: the match has to be exact from the very start. Something changed at the start — everything after it gets counted again.
What breaks the cache (the next turn is slower and pricier)
• Switching the model with /model, because every model has its own cache
• /compact, because the history gets rewritten (one more reason to do it on a break)
• A long pause. The cache lives for a limited time: on a subscription within the plan limit the main session keeps it for an hour, on an API key — 5 minutes. Every turn extends the timer
• Updating Claude Code: the first session after an update builds the cache from scratch
What doesn’t break it
• Editing files, changing permission mode, calling skills, /rewind, subagents
If you edit CLAUDE.md mid-session, the cache doesn’t break, but the changes don’t apply either. Claude works with the old version until /clear, /compact or a restart.
Whether the cache works, you can see in /usage: on fresh versions there’s a Prompt cache (main) line with the hit percentage.
Optimization: you pick the model at the start depending on the task, and do /compact and /clear on breaks between tasks, not in the middle.

Read more:
• Context window
• Prompt caching
• Managing context
• Statusline
• The context window with an interactive session simulation: https://code.claude.com/docs/en/context-window
• How Claude Code uses the cache: https://code.claude.com/docs/en/prompt-caching
• Anthropic on context engineering (that’s where context rot comes from): https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents
• https://code.claude.com/docs/en/statusline