What context is
Context is everything the model sees when it decides its next step: the system prompt, the project's instruction files, the descriptions of the tools it can call, the task, every file it has opened, the output of every command it has run, and the record of its own earlier steps. In a chat, most of that is the conversation. In an agent working for an hour, most of it is tool output the agent produced itself.
Prompt engineering was about the wording of one request. Context engineering, the term that took over in 2025, is about curating the whole window across many turns. Anthropic's engineering post Effective context engineering for AI agents puts the goal as finding the smallest possible set of high-signal tokens that maximize the likelihood of the outcome you want. Two words in that sentence carry the weight: smallest and high-signal.
A running example helps. An agent is asked to fix a bug: a discount code is applied twice when a customer refreshes the checkout page. To fix it well, it needs the checkout spec, the order and discount code, the team's rule that every payment-affecting write is idempotent, and the failing test. It does not need the full output of a 900-test suite, the history of the pricing module, or the twenty files it opened while looking for where discounts are calculated.
Why more context makes an agent worse
Context windows now hold hundreds of thousands of tokens, and it is tempting to fill them. Research says otherwise. Chroma's 2025 report Context Rot tested 18 models, including GPT-4.1, Claude 4, Gemini 2.5 and Qwen3, and found that models do not use their context uniformly: their performance grows increasingly unreliable as input length grows, even on simple tasks. Anthropic's post describes the same effect as an attention budget: every token added draws on it, and recall falls as the window fills.
In practice this means three trade-offs that surprise teams:
- A thousand-line instruction file is followed worse than a hundred-line file with links to the rest.
- Five hundred lines of test output hide the two lines that matter; a summary of the failures does not.
- One long session that handles three tasks does worse on the third than three short sessions, one per task.
There is a cost side too. A long session sends its whole history with every request. Prompt caching makes re-reading cheaper, but the Claude Code cost docs are clear that stale context is paid for on every message, and that a one-line question in a session open all day still draws usage for the whole conversation.
The layers of context
It helps to see the window as layers, each with its own decision: does it go in up front, on request, or never?
| Layer | Examples | When it enters | Default decision |
|---|---|---|---|
| Project rules | AGENTS.md, CLAUDE.md, always-on rules | Session start, every session | Up front, kept short |
| Task and spec | The issue, acceptance criteria, the plan | Task start | Up front, for this task only |
| Code | Source files, tests | When the agent searches and opens them | On request; make code easy to find |
| Knowledge base | Architecture notes, decision records, contracts | When a link is followed | On request, by link |
| Tool output | Test runs, builds, logs, search results | After every call | Compressed before the model reads it |
| External systems | Tracker, wiki, logs, databases | Through a CLI or an MCP server | On request, narrow queries |
| Path-scoped rules | Rules for one module or file type | When a matching file is touched | Only where relevant |
Anthropic's post describes Claude Code as a hybrid along these lines: CLAUDE.md files are placed in context up front, while tools such as glob and grep let the agent find everything else just in time. That only works if the code is findable: clear names, one concept in one place, no three copies of the discount logic for search to stumble on.
Six techniques that keep context useful
- Short rules, details by linkInstruction files hold what nearly every task needs. Everything else goes into
docs/with a one-line pointer. Claude Code's docs suggest keeping each CLAUDE.md under 200 lines. See AGENTS.md and CLAUDE.md. - Load on demandProcedures that only some tasks need, such as a release or a migration, become skills the agent loads when it needs them, not paragraphs it reads every session.
- Scope rules to pathsNested AGENTS.md files and Claude Code's
.claude/rules/withpathsload a module's rules only when the agent works in that module. The discount rules arrive when the agent opens the checkout code, not when it edits the README. - Compress noisy outputWrap the test runner so it prints the failing tests and their errors, not every passing line. Cut logs to errors. Claude Code's cost docs show a hook that rewrites a test command to keep only the end of its output, and suggest grepping a 10,000-line log for errors so the model reads hundreds of tokens instead of tens of thousands.
- Short sessions that hand off through filesEnd a session when its task ends. Carry forward what matters in files: the plan, what is done, the open questions. Compaction can stretch a session, but it is itself a large request; Anthropic's post calls clearing old tool results one of the safest, lightest forms of it.
- Sub-agents for noisy searchesA sub-agent can read twenty files to find where discounts are applied and return one paragraph. Anthropic's post describes sub-agents that use tens of thousands of tokens exploring and return a condensed summary, often 1,000 to 2,000 tokens.
Tools are context too
Every tool an agent can call adds its description to the context, and every call adds its result. Anthropic's post names bloated tool sets, with overlapping functions and ambiguous choices, as one of the most common failure modes, and recommends tools that are self-contained, clear in purpose and minimal in overlap.
Two practical consequences for a team:
- Prefer a CLI when one exists. The Claude Code cost docs note that tools like
gh,awsandgcloudare more context-efficient than MCP servers, because they add no per-tool listing. Claude Code now defers MCP tool definitions by default, which helps, but an unused server is still noise. Disable the ones nobody uses. - Shape what tools return. A test script with a
--summaryflag, a log query that defaults to errors from the last hour, a database helper that returns ten rows rather than ten thousand. Each is a small change that pays off in every session.
Context that lives outside the repository
In most companies the context an agent lacks is not in the code. Requirements sit in a tracker, design decisions in a wiki and in people's heads, the team's real conventions in years of review comments. An agent cannot follow a rule it has never seen.
There are two ways to close the gap, and most teams need both:
- Connect the source. Give the agent read access to the tracker or the wiki through a CLI or an MCP server, so it can fetch the issue and the linked decision when the task names them. This suits material that changes often or belongs to other teams.
- Move the knowledge into the repository. An architecture map, API contracts and a decision log in Markdown, reviewed with the code they describe. This suits material the agent needs on most tasks, and it has a side benefit: it stays true, because a change to the code and a change to the doc arrive in the same pull request.
A product's own description belongs in the repository as well: how to build it, test it, deploy it, and which commands are dangerous. That is what AGENTS.md templates are for, and what harness engineering builds on.
Signs an agent is missing context
Missing context rarely announces itself. It shows up as patterns in the work:
| What you see | What is usually missing |
|---|---|
| The agent writes a helper that already exists | Findable code: a clear name, one place for the concept, or a pointer in the rules |
| It breaks an agreement "everyone knows" | The agreement written down, in the rules or as a check |
| It asks questions the docs answer | A link from the instruction file to the doc |
| It loops: fixes one test, breaks another | A clean session with a short summary of the failures, rather than a long history of attempts |
| Reviewers repeat the same remark across pull requests | A rule, a lint check or a test that turns the remark into something the agent sees before review |
Each of these is cheaper to fix in the environment than in the next prompt. A correction typed into one session is gone when the session ends; a line in the rules or a check in CI is there for every session after it.
Questions
How is context engineering different from prompt engineering?
Prompt engineering tunes the wording of one request. Context engineering decides everything the model sees over a long run: rules, files, tool output, history. For agents that work for many turns, the second matters more.
Do we need RAG for a coding agent?
For code, usually not: file search and good names are enough, and the agent reads the actual source rather than a retrieved fragment. Retrieval helps for large bodies of text outside the repository, such as a big wiki.
Why do agents get worse in long sessions?
The window fills with old tool output and abandoned attempts, which dilute the parts that matter; research on context rot shows reliability falling as input grows. Each message also resends that history. Start a fresh session per task and hand off through files.
How many rules should an instruction file hold?
Only what nearly every task needs. Put module rules behind paths, procedures in skills, and everything else in linked docs. If a person cannot read the file in a few minutes, it is too long for the agent too.
Take your team there, step by step
A 13-week pilot on one team: a baseline, a harness built around your process, real backlog tasks through the whole cycle, and the numbers before and after.