Skip to content

Context management for coding agents

Context management for coding agents is the practice of controlling what Claude Code, Codex, or Cursor holds in its context window: instruction files, file reads, command output, and conversation. Too little context makes the agent guess; too much makes it drift from earlier instructions. The fix is tight scoping, timely resets, and a check of what the agent works from.

In the morning the agent fixed a bug cleanly. By afternoon, in the same chat, it edits a module your rules put off limits. This section is for developers who drive agents daily and tech leads who own shared instruction files.

Start from the symptom; each page owns one layer.

SymptomPage
The agent forgets rules mid-session, or quality drops after auto-compactionContext windows and token limits
The agent ignores or misreads CLAUDE.md, AGENTS.md, or Cursor’s RulesDocumentation as context
The instruction file has grown to hundreds of lines of one-off fixesPruning context files
The agent reads a whole directory to change one functionProject structure for agents
The agent cannot find the right code, or finds the wrong copyCode search and indexing
Every new session relearns what the last one discoveredLong-term context retention
The token bill rises faster than the work deliveredContext cost optimization

Three habits that prevent context problems

Section titled “Three habits that prevent context problems”

Every good prompt carries intent (what to change and what “done” means) and state (the files, errors, and test output that describe the code now). A bad session lacks one of them, or buries the intent under state. These three habits keep the balance:

  1. One task per session. Leftover exploration pulls edits toward stale files.
  2. Name the exact files. Point at the function or file, not the directory, so reads stay small.
  3. Keep a short instruction file. Build, test, and lint commands plus the conventions the agent gets wrong, so no session spends tokens rediscovering them.

The commands differ by tool:

  • Measure: /context draws current usage as a grid by category.
  • Reset: /clear starts a new session with empty context; the old one stays resumable with /resume.
  • Compact: /compact followed by what to keep, for example /compact keep the acceptance criteria and the test command.
  • Point at files: type @ and a path to attach a file to the prompt.
  • Instructions: CLAUDE.md; /memory edits it. From v2.1.277 (the latest channel), Claude Code reads AGENTS.md when a project has no CLAUDE.md (v2.1.281 on Bedrock, Google Cloud, Foundry, and LLM gateways); toggle it under Project instructions in /config.

Checked on 2026-09-26 against Claude Code 2.1.283 (the latest channel).

Prompts that set and check the agent’s context

Section titled “Prompts that set and check the agent’s context”

How do you know the agent had the right context?

Section titled “How do you know the agent had the right context?”

Skip the transcript. Three checks take seconds; the fourth is for instruction-file edits:

  • Restate and compare. Run the second prompt at the start and again after any compaction. A missing or reworded constraint means the context was lost; point the agent back at the file before it writes more code.
  • Tests as the oracle. The “done when” command decides whether the behaviour exists. See how strong your oracle is.
  • Diff scope. git status --short should list only the files the prompt named, including new ones; an unexpected path means the agent lost scope.
  • Measure instruction-file changes. Run the same task in a fresh session before and after you edit CLAUDE.md or AGENTS.md, at least three times each way. Compare the pass rate and git status --short, not one result. /context (Claude Code) or /status (Codex) shows what the edit costs in tokens.

The developer owns the restate check; the reviewer owns tests and diff scope. Shared instruction files change through code review with a named owner.

  • A decision from earlier in the session disappears. Compaction summarized it away. Write decisions and acceptance criteria to a file the agent re-reads; context windows shows a handoff note and a compaction hook.
  • The instruction file keeps growing and adherence keeps falling. Every line competes for attention. Archive the file, log what the agent gets wrong, and restore a line only when the same mistake happens twice, following the ablation protocol.
  • The agent follows an instruction nobody on the team wrote. Instruction files are supply chain: review them like code, and never let a CI agent holding secrets load them from an untrusted branch. See how to lock down what CI agents load.