Context Engineering for Claude Code
What context engineering is, what fills Claude Code's context window, and how to manage conversation length, compaction, memory between sessions and tool output.
By Shryxa · Updated · 9 min read
Short answer
Context engineering is deciding what goes into a language model's context window, and what stays out, so it has what it needs without wasted tokens.
In Claude Code that comes down to four levers: how long the conversation grows, what a compaction keeps, what carries over between sessions, and how much tool output enters the window.
What is context engineering?
A model only knows what is in its context window at the moment it answers. Context engineering is the practice of curating that window: putting in the instructions, files and history the task needs, and keeping out what it doesn't.
Prompt engineering is about how you phrase one request. Context engineering is about everything around it, and how it changes over time. For a single chat question the prompt matters most. For a coding agent that reads files, runs commands and works for hours, the context matters far more: by the middle of a session, your latest message is a tiny fraction of what the model reads.
What fills Claude Code's context window
| Part | When it is added | What you control |
|---|---|---|
| System prompt and built-in tool definitions | Every session | Little |
| MCP server tool definitions | Every session, per connected server | Which servers are connected |
| CLAUDE.md memory files | Every session | Their length and content |
| Your messages and Claude's replies | Each turn | /clear, /compact, session length |
| Tool results: file reads, command output, search results | Each tool call | What Claude reads and runs; output size |
Run /context in Claude Code to see the current breakdown. In a fresh session, the fixed parts already take tens of thousands of tokens. In a long session, tool results and messages dominate.
Why it matters: cost, limits and quality
- Cost. Every reply re-reads the whole window. A bloated window makes every single reply more expensive.
- Usage limits. On Pro and Max plans, long conversations use up the 5-hour and weekly limits faster, even though cached re-reads count for less.
- Quality. Models can lose track of details buried in very long contexts. Stale information, like an approach you abandoned an hour ago, can steer the model the wrong way.
- Continuity. The opposite failure: clear too aggressively and Claude forgets your goal, decisions and open bugs.
Good context engineering balances the last two: as small as possible, but never missing what the task depends on.
The four levers
1. Conversation length
The biggest lever. Decide when a conversation should end or be summarised, rather than letting it grow until an automatic limit. Use /clear between unrelated tasks, and start compaction earlier with the autoCompactWindow setting.
2. What compaction keeps
A compaction replaces the conversation with a summary. A generic summary may drop the one detail you needed. Tell it what matters: the goal, decisions made and why, files changed, errors still open and next steps. With /compact you can type this as an instruction; a PreCompact hook can add it to every compaction automatically.
3. Memory between sessions
A new session or a /clear starts from zero. Durable facts (build commands, conventions, architecture) belong in CLAUDE.md. Task state (where you are, what is broken) belongs in a short note: a file Claude writes before you clear, or context added by a SessionStart hook.
4. Tool output
A single test run can print thousands of lines of passing tests. Keep the signal (failures, errors, warnings, summaries) and drop the noise: quiet flags, | tail, reading file ranges, and sending broad searches to a subagent that returns only a summary. A PostToolUse hook can shorten output before the model sees it.
Claude Code's context engineering toolkit
| Tool | Lever | Use it to |
|---|---|---|
/context | All | See what is filling the window |
/clear | Length | Start fresh between unrelated tasks |
/compact [instructions] | Length, compaction | Summarise now, keeping what you name |
autoCompactWindow | Length | Compact automatically at a size you choose |
CLAUDE.md | Memory | Durable project facts, loaded every session |
| Subagents | Tool output | Do broad reading in a separate context |
PreCompact hook | Compaction | Add standing instructions to every compaction |
SessionStart hook | Memory | Add context after startup, resume, clear or compaction |
PostToolUse hook | Tool output | Replace a tool's output before the model sees it |
A practical workflow
- Start lean. Check
/contextin a fresh session. TrimCLAUDE.mdand disconnect unused MCP servers. - One task per conversation. When the task is done, have Claude write a short note if anything carries over, then
/clear. - Compact on purpose. In a long task, run
/compactwith instructions at a natural break, before the conversation gets huge. - Keep output quiet. Ask, in
CLAUDE.md, for quiet test flags, filtered build output and partial file reads. - Delegate broad reading. Let subagents explore and report back a summary.
Steps 2–4 can be automated with hooks and settings. Lighten does this: earlier guided compaction, a memory note after compaction or /clear, and shorter noisy output. For a step-by-step list focused on cost, see How to Reduce Claude Code Token Usage.
Common mistakes
- One endless session. Fixing a bug, writing docs and planning a feature in one conversation means each reply pays for all three.
- A giant CLAUDE.md. It is read on every reply of every session. Put rarely needed detail in separate files and point to them.
- Clearing without a note. You save tokens and then spend them again re-explaining the task.
- Cutting output by position. Keeping only the first N lines of a log often throws away the error at the end. Cut by meaning: drop the noise, keep failures and warnings.
- Treating the context window as free because it is big. Size is capacity, not a budget.
Frequently asked questions
What is context engineering?
Context engineering is deciding what goes into a language model's context window, and what stays out, so the model has the information it needs for the task without wasted tokens or distracting detail.
How is context engineering different from prompt engineering?
Prompt engineering is about how you word a single request. Context engineering is about everything else the model sees with it: history, files, tool results, memory and instructions, and how that changes over a long session. For agents like Claude Code, context is usually the bigger factor.
What is a context window?
The context window is the total amount of text, measured in tokens, that a model can consider at once. In Claude Code it holds the system prompt, tool definitions, CLAUDE.md, the conversation so far and every tool result.
Does a bigger context window make context engineering unnecessary?
No. A bigger window lets sessions run longer, but every reply still re-reads everything in it, so cost grows with size, and models can lose track of details buried in very long contexts. A large window makes good context habits more valuable, not less.
Can hooks be used for context engineering in Claude Code?
Yes. A PreCompact hook can add instructions to the compaction summary, a SessionStart hook can add context when a session starts, resumes, clears or compacts, and a PostToolUse hook can replace a tool's output before the model sees it.
Shryxa is not affiliated with or endorsed by Anthropic. "Claude" and "Claude Code" are trademarks of Anthropic. Claude Code changes often; check the official docs for the latest behaviour.