Materials / Approach

Context engineering: giving an agent the context it needs

An agent's results depend less on how a task is worded than on what else sits in its context window while it works. This guide explains why less is usually better, and how a team decides what an agent sees, when, and what it never sees.

10 min read14 Oct 2026Ilia Poliakov

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:

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?

LayerExamplesWhen it entersDefault decision
Project rulesAGENTS.md, CLAUDE.md, always-on rulesSession start, every sessionUp front, kept short
Task and specThe issue, acceptance criteria, the planTask startUp front, for this task only
CodeSource files, testsWhen the agent searches and opens themOn request; make code easy to find
Knowledge baseArchitecture notes, decision records, contractsWhen a link is followedOn request, by link
Tool outputTest runs, builds, logs, search resultsAfter every callCompressed before the model reads it
External systemsTracker, wiki, logs, databasesThrough a CLI or an MCP serverOn request, narrow queries
Path-scoped rulesRules for one module or file typeWhen a matching file is touchedOnly 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

  1. 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.
  2. 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.
  3. Scope rules to pathsNested AGENTS.md files and Claude Code's .claude/rules/ with paths load 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.
  4. 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.
  5. 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.
  6. 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:

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:

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 seeWhat is usually missing
The agent writes a helper that already existsFindable 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 answerA link from the instruction file to the doc
It loops: fixes one test, breaks anotherA clean session with a short summary of the failures, rather than a long history of attempts
Reviewers repeat the same remark across pull requestsA 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.