Sub topic 6
Context, history, and keeping sessions focused
This chapter assumes you can start an interactive session in a project, know that an agent calls tools in a loop, and can read a git diff. It covers what the model sees on each turn, why long sessions degrade, and how to keep a session focused in Claude Code, Codex CLI, and Copilot CLI.
What the context window holds
A context window is the maximum amount of text, measured in tokens, that the model can read in one request. A token is a small chunk of text, often a few characters. The model has no memory between requests. On every turn the tool sends the whole conversation so far, and the model answers based only on that.
The window typically contains:
- The system prompt written by the tool vendor.
- The descriptions of every available tool, including any MCP servers you connected.
- Your instruction file (CLAUDE.md or AGENTS.md).
- Your messages and the model's replies.
- Every tool call and its result: file contents, command output, search hits.
Tool results are usually the largest part. Reading a 2,000-line file puts all of it into the window. Running a test suite that prints 500 lines of logs does the same. The window therefore fills faster than conversation alone would suggest.
Why long sessions get worse
A full window causes three practical problems.
- Cost and latency. Each request carries the whole history. Although vendors cache repeated prefixes to reduce the price, longer requests still cost more and respond more slowly.
- Hard limit. When the history approaches the window size, the tool must shrink it or stop.
- Lost focus. Early instructions sit far from the current task. Old file contents may be outdated because the agent or you edited the file since. Detours, such as a debugging tangent, remain in the history and can mislead later steps.
The third problem is the subtle one. A session can still run while the model quietly works from stale or irrelevant material. Treat the window as a limited workspace, not a log to keep forever.
Compaction
Compaction replaces older history with a summary so the session can continue. All three tools do this, and each offers a manual command and automatic behavior near the limit. Command names and thresholds change between versions, so check each tool's /help output or documentation.
| Task | Claude Code | Codex CLI | Copilot CLI |
|---|---|---|---|
| Summarize and continue | `/compact` | `/compact` | `/compact` |
| Start fresh in the same terminal | `/clear` | `/new` | `/clear` |
| Inspect context use | `/context` | `/status` | `/context` |
| Resume an earlier session | `claude --continue`, `claude --resume` | `codex resume` | `copilot --continue`, `copilot --resume` |
Verify these against your installed version before relying on them.
Compaction is lossy. The summary keeps the gist and may drop exact error messages, file paths, or a constraint you stated once. Two habits reduce the damage:
- Compact at a natural boundary, such as after a feature works and its tests pass, not in the middle of debugging.
- Give a focus hint when the tool allows it. In Claude Code,
/compactaccepts instructions, for example/compact keep the failing test names and the decision to use ISO week dates.
After compacting, re-check that important constraints survived by asking the agent to state them.
Keep durable facts out of the history
Anything you must repeat in every session belongs in the instruction file, because the tool reloads that file at the start of each session and after a clear. Anything that is only true for the current task belongs in the conversation.
A useful test: if a new teammate would need the sentence on their first day, put it in the file. If it only matters for today's bug, say it in the prompt.
Keep the file short. Its text is sent on every turn, so a 3,000-line instruction file wastes the window and dilutes the rules that matter.
Controlling what enters the window
You decide a lot about what the agent reads.
- Point to specific files. "Fix the date parsing in
src/dates/parse.ts" avoids a broad search that loads many files. Most tools let you reference files with@path. - Limit command output. Ask the agent to run tests for one file, or pipe long output through
tailor a filter. - Name what to ignore. Say "do not read
dist/orgenerated/", and block those paths in the tool's permission settings if the tool supports it. - Prefer search over reading everything. Grep and symbol search return short matches. Reading whole directories does not.
- Delegate noisy work. Some tools support subagents, which run a task in a separate context and return only a summary. A later chapter covers them.
Session management patterns
One task per session. Finish a bug fix, commit, then clear. A fresh session costs you a few sentences of setup and removes all leftover clutter.
Resume deliberately. Resuming reloads an old history. That helps for long work you paused. It hurts when the old history is large and mostly irrelevant. If a resumed session behaves oddly, start fresh and paste a short summary.
Write state to a file. For work that spans sessions, ask the agent to save a plan or progress notes to a file such as PLAN.md, commit it, and start a new session that reads it. Files persist exactly. Compaction summaries do not.
Restart after a wrong turn. If the agent has gone down a bad path, correcting it in the same session leaves the bad path in the history. Rewinding or clearing and restating the task with what you learned often works better. Use git restore or a branch reset on the files if needed.
Spotting a degraded session
Watch for these signs:
- The agent re-reads files it already read.
- It forgets a rule from the instruction file or an earlier decision.
- It repeats an approach you already rejected.
- It edits a file based on content that has since changed.
When you see them, check context usage, compact or clear, and restate the goal in one or two sentences.
Edge cases
- Large single files. One huge file can consume most of the window. Ask the agent to read a line range or search within it.
- Binary and generated files. Reading them wastes tokens. Exclude them.
- MCP tool descriptions. Each connected server adds tool definitions to every request. Disable servers you are not using for the current task.
- Stale reads. If you edit a file in your editor while the agent works, tell it to re-read the file. It may otherwise rely on the old copy in its history.
- Automatic compaction timing. It can trigger mid-task. If a task is long, compact yourself at a point you choose.
Exercise
You have spent 40 turns debugging a flaky test. The test now passes and is committed. You want the agent to start a new feature in the same repository. Should you continue in the same session, compact, or clear? Give your reason, and name one thing to do first.
Show answer
Clear (or start a new session). The debugging history is mostly irrelevant to the new feature, and it adds cost and risk of misleading the agent. Compaction would keep a lossy summary of work you no longer need. Before clearing, confirm the fix is committed with git status and git log, and optionally ask for a short handoff note if anything from the debugging matters for the feature. The instruction file reloads automatically in the new session.
Exercise
A teammate's instruction file is 1,500 lines and includes today's bug description. Name two problems this causes in terms of the context window, and say what to change.
Show answer
The file is sent on every turn, so it consumes window space and raises cost and latency. It also dilutes the important rules and puts task-specific text, which becomes wrong tomorrow, into a durable file. Shorten the file to commands and conventions a new teammate needs, and move the bug description into the prompt for that session.