If you hit your usage limit sooner than you expect, or notice that after a few hours of work Claude Code answers in a muddier way, the cause is almost always the same: the context is full of things that no longer matter. This guide explains what fills the context, what actually costs tokens, and which habits keep it under control, step by step.
If your problem is the opposite, Claude forgetting everything between sessions, start with why Claude Code forgets context: they're two sides of the same topic, and the fixes fit together.
Why the context fills up
The model remembers nothing between requests. With every message, Claude Code sends the entire context again: system prompt, CLAUDE.md, auto memory, every previous message, and every tool result. Prompt caching avoids reprocessing the unchanged part from scratch, but the official docs are explicit: in a session that has been open all day, even a one-line question draws usage for the whole conversation.
The three main sources of growth are:
- File reads: every file Claude opens enters the context in full, unless it reads only part of it.
- Tool output: test logs, search results, shell command output. These are often the heaviest entries, and they're almost always needed only once.
- Long history: failed attempts, corrections, detours into problems already solved. They stay there and get reread every turn.
There's a quality effect too, not just a cost one. Claude Code's best-practices guide recommends /clear between tasks precisely because a focused context gets better answers and costs less. If you've already corrected the same thing twice, a third correction just adds another failed attempt for Claude to keep rereading.
What actually costs tokens
Before optimizing, measure. Two commands are enough:
/contextshows what's occupying the context right now, broken down by category, including which CLAUDE.md and memory files loaded./usageshows the session's consumption. On subscription plans it adds a breakdown of what counts against your limits (skills, subagents, MCP servers) with concrete suggestions.
The entries that show up most often are CLAUDE.md, loaded at every session start, large file reads, command output, and the accumulated conversation. Extended thinking counts as output, and for simple tasks you can lower it with /effort.
These are two different tools, and mixing them up is the first source of waste.
/clear wipes the conversation and starts fresh. It costs nothing, and you should use it whenever you move to work that has nothing to do with what came before. If you think you'll want to return to the session, name it first with /rename and find it later with /resume.
/compact replaces the history with a summary and lets you continue the same work with less weight. One caveat: to produce the summary, Claude Code has to read the conversation, so compacting a huge context is itself a large request. It's best done at a natural break, between sub-tasks, instead of waiting for auto-compaction mid-task. You can also say what to keep:
/compact focus on the auth bug fix and the failing tests
If instead you've gone down a path you want to abandon entirely, /rewind goes back to an earlier point in the conversation without building a new summary.
A test runner that prints thousands of lines puts all of them into the context. Most of the time you only need the errors and the final summary. Ask Claude to filter the output, or write it straight into the prompt:
npm test 2>&1 | tail -n 50 grep -n "ERROR" logs/app.log | head -n 30
The same goes for logs: searching for ERROR lines instead of reading the whole file cuts the context from tens of thousands of tokens to a few hundred, and it's the example the official docs use.
If the problem is in one function of a two-thousand-line file, there's no need to load all of it. Claude Code's read tool accepts a starting point and a number of lines, so you can explicitly ask it to read only the part that matters, ideally after a search that tells you where it is.
The prompt makes a difference too. The docs put it plainly: a vague request like "improve this codebase" triggers broad scanning, while "add input validation to the login function in auth.ts" lets Claude work with minimal file reads.
A subagent works in a separate context window and returns only a summary to the main conversation. It's the right tool for noisy work: finding where something is defined across the project, reading a dozen files to understand a flow, running a test suite, analyzing a log. The raw material stays in the subagent's context; you get the conclusion.
Claude Code ships with a few already, such as Explore, a read-only agent built for searching and analyzing code. For more on organizing specialized agents, see the guide on specialist agents and skills.
Two honest limits: a subagent still spends tokens of its own while it works, and launching many that return long reports fills the main context back up. The useful rule is to ask for short summaries and use a smaller model for simple tasks.
CLAUDE.md is loaded at every session, so you pay for every line every time, even when you're working on something unrelated. The official docs say to stay under 200 lines per file. Two details many people miss: @path imports help with organization but don't reduce the cost, because imported files load together with the CLAUDE.md that references them. Instructions that only matter for part of the codebase belong in rules under .claude/rules/ with a paths field, which load only when Claude works on matching files.
Long, specific procedures, like a PR review or a database migration, fit better in a skill, which loads only when invoked. How to write a CLAUDE.md that stays short and useful is the subject of the guide on how to write CLAUDE.md.
Every time you re-explain an architecture or a decision made weeks ago, those explanations land in the context, and they're often longer than needed. The alternative is to write decisions down once in a structured place and have them searched when needed: a targeted search returns only the relevant nodes, not the project's entire history.
That's the role of a knowledge graph for AI coding. In the VibeCoded Orchestrator, free and open source, the search is called hybrid_search and combines semantic similarity with links between nodes; the steps to set it up are in the guide to persistent memory for Claude Code. The same applies to teams: if everyone re-explains the same context in their own session, it gets paid for several times. We cover that in Claude Code in a team with shared context.
Hooks are scripts that run at specific points in Claude Code's lifecycle, outside the model. A PreToolUse hook on the Bash tool can rewrite a command before it runs, for example appending a tail to test output, so the filter doesn't depend on Claude remembering it. That's exactly the official docs' example: keep only the last 100 lines of test output.
Hooks work in the other direction too: a SessionStart hook matching the compact source can re-inject essential context after compaction, instead of relying on the summary alone. The full overview of events and the difference between blocking and observing is in the article on automation hooks.
Quick checklist
Before opening a long session, or when you feel the answers getting worse:
- Is the task new and unrelated to the previous one?
/clear. - Continuing the same work but the history is long?
/compactwith a note on what to keep, at a natural break. - Corrected the same thing twice?
/clearand a fresh prompt that includes what you learned. - Have you checked
/contextto see what actually weighs the most? - Do noisy commands have a filter (
tail,grep,head)? - Are reads targeted to the part of the file you need?
- Are broad searches and logs handled by a subagent?
- Is CLAUDE.md under 200 lines, with niche instructions moved into rules or skills?
- Are project decisions written into a queryable memory instead of being re-explained?
What to expect in practice
None of these tactics is a trick: they're habits. The most effective one is usually the most boring, /clear between tasks, because it drops all the accumulated weight in one go. The others keep you from filling the context back up too quickly. The result shows up in two ways: you hit usage limits later, and long sessions stay sharper. If generated code often breaks after a few hours of work, a saturated context is one of the reasons, alongside the ones in why AI-written code breaks.
Sources: Manage costs effectively, Explore the context window, How Claude Code uses prompt caching, Subagents, Memory, Best practices.
Want to try it? The QuickStart installs the free, open source VibeCoded Orchestrator in about 10 minutes of active time.