Sub topic 6
Context, history, and keeping sessions focused
This chapter assumes you can start a session in Claude Code, Codex CLI, or Copilot CLI and that you know the agent loop (model call, tool call, observation). It covers what the agent can "see" at any moment and how to keep that view useful.
The context window
A model reads a fixed amount of text per call. That limit is the context window. Everything the agent knows during a session has to fit inside it:
- Your instruction file (CLAUDE.md or AGENTS.md)
- Your prompts and the agent's replies
- Contents of files it has read
- Output from shell commands and tests
Each loop iteration adds more. The whole history is sent again on the next model call, so long sessions get slower and costlier. Windows differ by model and plan, so check your tool's documentation for the current size.
What goes wrong in a crowded session
When the window fills, tools either drop or summarize older material. Even before that, a long history causes problems:
- Early instructions get less weight, so the agent drifts from your conventions.
- Old, wrong attempts stay visible and can mislead later steps.
- Large file reads and verbose test output push out useful detail.
If the agent starts repeating mistakes or forgetting decisions, the cause is often a cluttered context, not a weak model.
Managing the session
Each tool offers commands to inspect and trim the session. Exact names vary by tool and version, so confirm with the in-session help.
| Goal | Typical command | Notes |
|---|---|---|
| Start fresh on a new task | `/clear` | Discards the history but keeps instruction files |
| Shrink a long session | `/compact` | Replaces history with a summary (Claude Code, Codex CLI) |
| See what is using space | `/context` | Claude Code; other tools show usage differently |
| Continue earlier work | `claude --continue` or `codex resume` | Reopens a saved session |
Keeping sessions focused
- One task per session. Fixing a date bug and then refactoring the build in the same history mixes two sets of context.
- Name files in your prompt. Pointing at
src/date.tssaves the agent from searching and reading unrelated files. - Put durable facts in the instruction file. Commands and conventions belong there, not in repeated prompts. They are reloaded at the start of every session.
- Limit noisy output. Ask for a test run of one file, not the whole suite, when you are iterating.
- Compact with guidance. If the tool allows it, say what to keep, for example "keep the failing test names and the chosen approach."
Worked example
You spent an hour fixing a parsing bug. Now you want to add a new CLI flag. Instead of continuing, commit the fix, run /clear, and prompt: "Add a --verbose flag to src/cli.ts. Follow the conventions in CLAUDE.md. Add a test." The agent starts with only the instruction file and your request, so its attention stays on the new work.
Exercise
Your session has run for a long time, the agent keeps ignoring a rule you gave at the start, and test output has filled the history. Name two actions that would help and explain why each works.
Show answer
Move the rule into CLAUDE.md or AGENTS.md so it is reloaded at the start of every session, and use /clear (or /compact with guidance) to remove the stale, noisy history. The first makes the rule persistent instead of dependent on old history. The second frees space and removes outdated attempts that can mislead the model.