Context Engineering: Bloat, Attention Loss, and Focused Sessions
Applies to: every agent session you run, from the first day of an engagement · Time to learn: 30 min reading + 15 min exercise · Lever: context
A coding agent only sees what is in its context window, and the quality of its work depends on how much of that window is useful. Long sessions fill the window with old file reads, command output, and abandoned ideas. The result is context bloat, and with it attention loss: the model gets worse at using what it has been given. This page shows what fills the window, why a bigger window does not solve it, and the habits that keep sessions focused.
- The window is a budget. Everything you load, read, or print spends it, and performance degrades as it fills.
- A 1M-token window raises the limit, not the quality: attention loss is measured well below the maximum length.
- One task per session.
/clearbetween unrelated tasks. After two failed corrections,/clearand write a better prompt. - Send exploration to subagents so only a summary returns. Write state to files (plan, scratchpad) before you clear.
- Cut noise at the source: quiet flags,
| head, JSON output, a token-reducing proxy such as rtk, and few MCP servers.
Why it matters in the AI age
Anthropic's Claude Code guidance puts it directly: the context window fills up fast, performance degrades as it fills, and it is the most important resource to manage (best practices). As an FDE you work in unfamiliar client codebases, which means lots of exploration: reading files, running searches, tailing logs. That is exactly the activity that bloats a session. Engineers who manage context get better answers, pay for fewer tokens, and spend less time re-explaining.
This is the first of the three levers (context, feedback loop, guardrails) from the overview, and the others build on it. AGENTS.md, skills, and MCP servers all spend the same budget.
Core concepts
The context window is a budget
Every session starts with a fresh window (memory docs). Some of it is spent before you type anything, and the rest fills as you work.
| What fills the window | When it loads | How it grows | How to control it |
|---|---|---|---|
| System prompt | Session start | Fixed | Nothing to do |
CLAUDE.md / AGENTS.md, unscoped rules, auto memory | Session start | Grows with file size | Keep files short (under 200 lines per CLAUDE.md); see AGENTS.md and rules |
| Skill descriptions | Session start | One short entry per skill | Install only the skills you use; see skills |
| MCP tool names (and full schemas if not deferred) | Session start | Per server and tool | Tool search, fewer servers; see below |
| File reads | On demand | Whole file per read | Read ranges, search first, delegate to a subagent |
| Command output | On demand | Every build, test, and git command | Quiet flags, | head, rtk |
| Conversation and your corrections | Continuous | Every turn, kept forever | /clear, fresh sessions, /compact |
Sources for what loads at start: context window docs.
Inspect your own budget with /context. It shows a live breakdown by category, suggests optimisations, and lists which CLAUDE.md and auto-memory files loaded (context window docs).
Context bloat
Context bloat is a window full of tokens that no longer help the current task. Typical causes:
| Cause | Example |
|---|---|
| Old task still in the window | Debugging the auth bug, then asking for a CSS change in the same session |
| Verbose command output | Full npm install log or 3,000 lines of test output |
| Whole-file reads | Reading a 2,000-line file to find one function |
| Exploration in the main session | "Where is X handled?" answered by reading 20 files |
| Repeated corrections | Four rounds of "no, not like that" in one thread |
| Too many MCP tools or a huge CLAUDE.md | Hundreds of tool definitions loaded upfront |
Old conversation also costs tokens on every later message, so a crowded window is slower and more expensive as well as worse (context window docs).
Attention loss
Attention loss is the measured drop in a model's ability to use information as the input grows. The evidence:
| Source | Finding |
|---|---|
| Lost in the Middle (Liu et al.) | Performance is highest when the relevant information is at the start or end of the input and drops when it is in the middle, including for models built for long contexts. |
| NoLiMa (Modarressi et al., ICML 2025) | 13 LLMs claiming at least 128K context are strong below 1K tokens and degrade as length grows. At 32K, 11 models fall below 50% of their short-length baseline. GPT-4o drops from 99.3% to 69.7%. |
| Chroma, Context Rot (2025-07-14) | 18 LLMs, including Claude models, get less reliable as input grows even on simple tasks. A single distractor lowers performance and four compound it. On LongMemEval, a focused prompt of about 300 tokens scored significantly higher than the full prompt of about 113K tokens. |
| Anthropic, Effective context engineering | Models have an "attention budget": every new token depletes it, because attention relates every token to every other (n² pairwise relationships for n tokens). Context is a finite resource with diminishing marginal returns. |
Read these as direction, not as a prediction for your task: the benchmarks differ in task and model. The practical point is the same in all of them. More tokens in the window means less reliable use of each token.
A bigger window does not fix it
As of 2026-10, current Claude models such as Fable 5.x, Sonnet 5 and later, Haiku 5.5, and Opus 4.7 and later have a 1M-token window by default (model config). That is useful headroom for a genuinely large task. It does not remove the effects above, which appear far below the maximum length. A bigger window also means a session can run longer before auto-compaction, so a bloated session can stay bloated for longer. Treat 1M as a safety margin, not a target to fill.
How to do it
Why keep sessions focused and scoped
A focused session has one goal, a small set of relevant files, and a short history. It gives the model less to sift through and gives you a clean point to review. Use these tools:
| Tool | Use it when | Notes |
|---|---|---|
/clear | Switching to unrelated work | Old conversation crowds out the files you need next (docs) |
| Fresh session | A session has gone off track | After two failed corrections, /clear and write a better prompt that includes what you learned (best practices) |
/compact <instructions> | Same task, window getting full | Example: /compact focus on the auth bug fix |
/rewind then Summarize | You want to compact only part of the conversation | "Summarize from here" or "Summarize up to here" |
/btw | Quick side question | Answer never enters the history |
/context | Any time | See what is using space |
| Subagent | Research or exploration | Own context; only the summary returns |
You can steer compaction in CLAUDE.md, for example "When compacting, always preserve the full list of modified files and any test commands" (best practices).
Start fresh instead of correcting repeatedly. A derailed session contains the wrong approach, your corrections, and the model's apologies. Each new attempt competes with all of that. A new session with a sharper prompt is usually faster than a fifth correction.
Auto-compaction and what survives
Claude Code compacts automatically as the window nears its limit, and /autocompact 500k sets the threshold (context window docs). As of 2026-10, models with a native 1M window compact before it fills, at about 967K tokens by default. Sonnet 4.6 and Opus 4.6 without extended context compact at the 200K boundary (model config).
After compaction, the project-root CLAUDE.md and unscoped rules are re-injected from disk. Path-scoped rules and nested CLAUDE.md files reload on demand, and up to five recently modified files are re-read (context window docs). Anything else, such as a decision you made in chat, survives only as a lossy summary. Write it to a file first.
Anthropic's guidance for compaction is to start by maximising recall, then trim (Effective context engineering). Manual /compact with focus instructions at a natural breakpoint beats waiting for the automatic threshold.
Subagents for exploration
"Use one when a side task would flood your main conversation with search results, logs, or file contents you won't reference again: the subagent does that work in its own context and returns only the summary" (sub-agents docs). Each subagent starts with a fresh context and does not see your conversation history, so put what it needs in the prompt.
Use subagents to investigate how the billing service retries failed
payments. Return: the files involved, the retry policy, and any
open TODOs. Do not change code.
Project subagents live in .claude/agents/, personal ones in ~/.claude/agents/. Details of parallel work are in execution plans.
Write state to files before you clear
Chat is the worst place to keep anything you need tomorrow. Before /clear or the end of the day, write the state down:
- Plan and progress go in the plan file (execution plans).
- Findings, dead ends, and commands that worked go in a scratchpad (memory and knowledge).
- Durable conventions go in
AGENTS.mdor a doc. - Start the next session with "Read
PLAN.mdandSCRATCHPAD.md, then continue with step 3."
This is what Anthropic calls structured note-taking outside the window (Effective context engineering), and it lets you clear without losing work.
Reduce tool-output noise
Command output is often the largest uncontrolled input. Cut it before the agent reads it.
pytest -q -x --tb=short # quiet, stop at first failure
npm test --silent
git log --oneline -n 20
git diff --stat
grep -rn "RetryPolicy" src | head -n 30
gh pr view 123 --json title,state,reviewDecision
kubectl get pods -o json | jq '.items[] | {name: .metadata.name, phase: .status.phase}'
- Prefer CLIs for external services. Anthropic's guidance calls CLI tools such as
gh"the most context-efficient way to interact with external services" (best practices). - Redirect long output to a file and let the agent search it:
make build > /tmp/build.log 2>&1; tail -n 40 /tmp/build.log. - Know the MCP output limits. Claude Code warns above 10,000 tokens and limits MCP tool output to 25,000 tokens by default (
MAX_MCP_OUTPUT_TOKENSchanges it). Text results over 50,000 characters are saved to a file (MCP docs).
rtk: a filtering proxy for shell output
rtk (Rust Token Killer, rtk-ai/rtk) is a CLI proxy that filters and compresses shell command output before the agent reads it. It is a single Rust binary that supports 100+ commands, with under 10 ms overhead, and uses four strategies: smart filtering, grouping, truncation, and deduplication (repo). Examples include git status|diff|log, grep/rg, ls/tree, pytest, go test, ruff check, docker ps, and test runners that show failures only.
| Topic | Detail |
|---|---|
| Claude Code integration | rtk init -g installs a PreToolUse hook (native binary). After a restart, a command such as git status is rewritten to rtk git status (repo). |
| Scope | The hook fires only on Bash tool calls. The Read, Grep, and Glob tools do not pass through it. |
| Other harnesses | The repo lists 18 supported tools, including Claude Code, Codex, OpenCode, Cursor, and Gemini CLI. |
| Claimed savings | "60-90%" on common dev commands. The repo says this reduces bash output only, not the total bill. |
| Caveat | Token counts use bytes / 4 rather than a tokenizer, so treat percentages as estimates and absolute token numbers as approximate. No benchmark methodology is published in the README. Measure on your own commands with rtk gain. |
| Licence | The rtk CLI is Apache-2.0. rtk-engine and rtk-proxy are BUSL-1.1 (source-available); commercial offering to third parties requires contacting the vendor. Check this with your client before you install it on their machines. |
brew install rtk # macOS / Linux; winget install rtk-ai.rtk on Windows
rtk --version
rtk gain # savings dashboard; confirms it is the right "rtk"
rtk init -g # registers the hook for Claude Code
The README warns that another project named rtk exists on crates.io, so check that rtk gain shows the savings dashboard. Hooks are covered in hooks and quality gates. Installing a tool that rewrites every shell command is a change to the client's developer environment, so get approval first.
MCP tool definitions and tool search
Each MCP tool brings a name, description, and JSON schema. Loaded upfront, a few servers can use a meaningful share of the window before you type. In Claude Code, tool search is on by default: "only tool names and server instructions load at session start", and the full tool definitions are discovered on demand (MCP docs).
ENABLE_TOOL_SEARCH | Behaviour |
|---|---|
unset or true | All MCP tools deferred |
auto | Load upfront while definitions total under 10% of the window; defer above that |
auto:N | Same with a custom percentage, for example auto:5 |
false | Load everything upfront |
A server can be exempted with "alwaysLoad": true in .mcp.json; each upfront tool "consumes context". Tool search needs Opus 4, Sonnet 4, Haiku 4.5 or later; earlier models load all MCP tools upfront (MCP docs). Anthropic's code-execution-with-MCP post shows one workflow dropping from 150,000 to 2,000 tokens (98.7%) by reading tool definitions on demand. That is one example, not a typical figure (post). Which servers to add, and how many, is in MCP servers.
Session hygiene routine
| Phase | Do |
|---|---|
| Start | State one goal in the first prompt. Name the files or directories that matter. Run /context once to see the baseline. Point to the plan or scratchpad if resuming. |
| During | Send exploration to a subagent. Pipe noisy commands through | head or quiet flags. Read ranges, not whole files. Run /context when answers feel off. Use /compact <focus> at a natural breakpoint (tests green, feature done). |
| End | Update the plan and scratchpad (what is done, what is next, dead ends). Commit. Update docs that changed. Then /clear or close the session. |
| Switching task | /clear. Do not carry the old task along "just in case". |
Signals your session is degraded
| Signal | Likely cause | Fix |
|---|---|---|
Agent forgets a rule from AGENTS.md | Instruction buried in a long window, or file too long | Shorten the file; for must-happen rules use a hook; start a fresh session |
| Repeats a fix you already rejected | Dead end lost in the middle of a long history | Write the dead end to the scratchpad, /clear, restart with it |
| Re-reads files it already read | Earlier read summarised away or crowded out | /compact with focus on the task, or restart with a plan file |
| Contradicts an earlier decision | Decision only existed in chat | Record decisions in the plan or an ADR |
| Answers get vaguer or hedge more | Long window with distractors | Fresh session, tighter prompt |
| Every message feels slow and costly | Large history sent each turn | /clear between tasks |
| Third correction on the same issue | Wrong approach is anchoring the session | /clear, then write a better prompt using what you learned |
/context shows large MCP or file share | Too many tools, big reads | Enable tool search, drop unused servers, delegate reads to a subagent |
Best practices
- Give each session one goal and clear it when the goal is met.
- Put durable knowledge in files that load only when relevant, not in chat.
- Keep
CLAUDE.mdandAGENTS.mdunder 200 lines; cut any line whose removal would not cause a mistake. - Prefer search then targeted read over whole-file reads.
- Use
/compactwith explicit focus instead of relying only on the automatic threshold. - Measure before and after when you adopt a token-saving tool, and trust your own numbers over published ones.
- Use
/contextregularly. It is the only view of where the budget is going.
Pitfalls to design around
- Keep long sessions short. A session that has run for hours across several tasks should end; start the next task fresh.
- Do correct early, then reset. Two failed corrections is the signal to rewrite the prompt in a clean session.
- Run noisy commands with limits. Add
-q,--stat,| head, or redirect to a file. - Install fewer MCP servers, and rely on tool search for the rest.
- Save state before clearing. Compaction summaries are lossy; files are not.
- Verify what a compression tool hides. A filter can drop a line you needed; the agent can still run the raw command when output looks incomplete.
Try it
Lab 15 minutes on a real repository.
- Open a session, run
/context, and note the baseline by category. - Ask a question that needs exploration ("how does X work?") twice: once in the main session, once with "use subagents to investigate". Run
/contextafter each and compare. - Run your test suite with and without quiet flags and compare how much output lands in the session.
- Optional: install rtk (with approval), run
rtk init -g, restart, repeat step 3, and checkrtk gain. - Write a three-line
SCRATCHPAD.md, run/clear, and resume from it.
Checklist
- I can name what fills the context window and read
/context. - I start each task in a fresh or cleared session.
- I send exploration to subagents and keep only summaries.
- I write plan and findings to files before
/clear. - My common commands use quiet flags, limits, or JSON output.
- I know the tool-search setting and have removed unused MCP servers.
- I checked licence and client approval before installing rtk.
Sources
- Anthropic, Claude Code best practices
- Anthropic, Claude Code context window
- Anthropic, Claude Code memory
- Anthropic, Claude Code model configuration (window sizes and compaction thresholds, as of 2026-10)
- Anthropic, Claude Code subagents
- Anthropic, Claude Code MCP (tool search, output limits)
- Anthropic, Effective context engineering for AI agents (2025-09-29)
- Anthropic, Code execution with MCP
- Liu et al., Lost in the Middle
- Modarressi et al., NoLiMa
- Chroma, Context Rot (2025-07-14)
- rtk-ai/rtk (as of 2026-10; no release version captured)