What Is Context Engineering for Coding Agents? How It Shapes What an AI Coding Assistant Writes

What is context engineering for coding agents?
Context engineering for coding agents is deciding what an AI coding agent reads before it writes: your project’s rules, the right files and what it has already tried.
The agent can’t use what it hasn’t been shown, so the same request can give code that fits your project or code that guesses. In our 30-run test, one line in a rules file took the date format we asked for from 0 of 10 runs to 10 of 10.
If you have used an AI coding tool for more than a day, you have probably seen it ignore a rule you gave it, handle a small fix perfectly and then get lost in a bigger change, or “forget” something you said an hour ago.
These look like moods. Often the cause is what the tool was looking at when it wrote its answer. Choosing what it looks at is the job of context engineering for coding agents; our page on context engineering for code lists our other articles on it.
What context engineering for coding agents means
A coding agent is an AI assistant that works in several steps on its own: it reads files, runs commands and tries again when something fails (the idea behind Agentic Coding). It doesn’t know your project. Each time it writes, it reads a bundle of text, and that bundle is the context.
Context engineering is the practice of choosing what goes into that bundle and keeping it useful as the work goes on. Anthropic defines it as “the set of strategies for curating and maintaining the optimal set of tokens (information) during LLM inference” (published September 29, 2025).
A token is a small piece of text, often a word or part of one; inference is the model producing an answer. In plain words: someone, or something, picks what the model reads and keeps that selection fresh.
What the assistant reads before it answers
The bundle the model reads in one go is called the context window. Apart from what it learned in training, the model sees nothing outside that window on a given call. Four kinds of things go inside:
| What goes in | In everyday words | Example |
|---|---|---|
| Instructions | Standing rules for the project and the role the tool plays | “Write a test for every new function” |
| Files | The code and documents it has open or has looked up | The file you are editing, or a related one it searched for |
| Tools and their results | Actions the tool can take, such as searching or running a command, and what came back | The result of running your tests (small programs that check the code still works), or data from a connected service |
| The conversation so far | Everything said and done earlier in the session | Your earlier requests and its earlier attempts |
Birgitta Böckeler, who writes about coding agents for Martin Fowler’s site, groups the inputs as standing instructions, the tools the agent can call and the files it works on; her diagram adds the conversation.
A file saved in your project is not automatically something the model can read. The tool has to load it into the window first.
A rules file such as CLAUDE.md, a plain text file of project instructions that Claude Code reads, feels like memory because the tool puts its contents into the window for you.
Why the same tool behaves differently in different projects
The model’s weights, the numbers it learned in training, are frozen once training ends. What changes from your project to the next one is the text the tool reads.
A project with a clear rules file and tests the tool can run gives it a lot to work with. A project where the rules live in people’s heads gives it little, and the tool fills the gaps with guesses.
Wording a request is only one input
Prompt engineering is about wording one request well. Context engineering covers everything else the tool reads while it works: rules, files, tool results and history. Anthropic calls it “the natural progression of prompt engineering”. Good wording still helps, but it is one input among several.
When you need to care, and when you don’t
For a small, self-contained job, such as “write a function that sorts this list”, the request carries everything the tool needs. You can skip all of this.
Care starts when the task depends on knowledge the tool hasn’t been given: you change something other code depends on, your project has its own conventions, a session runs long, or you move a whole codebase to a different technology ( AI Code Migration).
Care also starts if you build mostly by describing what you want ( Vibe Coding). You may never read the code the tool writes, so a wrong assumption can slip through, and the rules and tests you give the tool do the checking for you.
One line in a rules file, tested 30 times
We wanted to see the effect for ourselves, so we ran a small test. We gave Claude Code, Anthropic’s coding assistant, the same request 30 times: add the expected delivery date to an order confirmation email. The request never changed. Only the project around it did.
| What the project contained | Date written as 03.11.2026 | What it wrote otherwise |
|---|---|---|
| No hint about the date format | 0 of 10 runs | “03 November 2026” (5 runs) or “2026-11-03” (5 runs) |
| A price shown as “12,50 €” in the same file | 2 of 10 runs | “2026-11-03” (8 runs) |
| One line in the project rules file: dates are written as DD.MM.YYYY | 10 of 10 runs | nothing else |
Measured on October 26, 2026, with Claude Code 2.1.310 and its sonnet model (claude-sonnet-5-5 at the time), run non-interactively with only the project’s own settings: 10 runs per row, each in a fresh copy of the same small project.
Without a hint, the assistant picked a format on its own, and not even the same one each time: one request, two different answers. A European price in the code did not clearly change that: 2 of 10 runs against 0 of 10 is too few to call a difference.
One line in the rules file settled it in every run, because Claude Code loads that file into the context at the start of every session.
The test is small: one task, one tool, one model. It shows the mechanism, not how often your own assistant will get a given rule right.
What you can do without writing code
- Keep a project rules file: how you name things, what must never change, how to run the tests. Keep it brief; Anthropic’s advice is to find the smallest set of high-signal tokens that makes the outcome you want likely. The setup guide for CLAUDE.md, AGENTS.md and Cursor rules shows how.
- Point the tool at the files that matter. GitHub’s guidance for Copilot is to open the relevant files and close the irrelevant ones.
- Start a fresh session (a new chat with the tool) for a new task. Many tools shorten long conversations to fit, and Anthropic warns that overly aggressive shortening can lose subtle but critical context.
- Tell the tool how to check its own work: name the test to run or the behavior to keep.
- Ask what it based an answer on. If a file or rule you expected isn’t part of the answer, add it and try again.
How the context window gets filled, and by whom
From here the article is for developers. If you don’t write code, you can stop here or skim the headings; the first half has what you need.
A tool builder can’t change the model’s weights at runtime, so the input is what is left to adjust. Assistants differ in how they curate it; Böckeler notes that some coding agent tools optimise context under the hood better than others.
One strategy Anthropic describes is just-in-time loading. Instead of pre-loading every possibly relevant file, the agent keeps lightweight identifiers such as file paths and stored queries, and retrieves data at runtime via tools. Claude Code, in Anthropic’s account, uses primitives like glob and grep to navigate and retrieve files this way.
One reason to load less is what Anthropic calls an “attention budget” the model draws on across the window; a long file loaded for one line spends some of it on every other line.
Böckeler’s names for three of these kinds of input are reusable prompts, context interfaces and workspace files, with conversation history beside them in her diagram.
She also splits the loading decision by who makes it: the model, the developer, or the agent software. A skill can be called by the model or by the developer, and a hook is the surrounding software acting deterministically.
Retrieval is the other half: a search can match literal text, and indexed search, such as GitHub’s repository indexing for Copilot, can also match meaning.
GitHub’s documentation says Copilot’s answers are optimized when the repository’s semantic code search index is up to date. Anthropic’s 2025 engineering post says Claude Code’s glob and grep search is effectively “bypassing the issues of stale indexing”.
The context also changes while the agent works. Anthropic notes that an agent running in a loop “generates more and more data that could be relevant for the next turn of inference”.
Its December 2024 guide to building agents adds that the agent needs “ground truth” from the environment at each step, such as tool results or code execution, and warns of “compounding errors”. So tell your agent how to check its work.
A subagent keeps a big job out of the main window. Claude Code’s documentation says it “does that work in its own context and returns only the summary”, so the search results, logs and file contents it read stay out of your main conversation. What it read but left out of the summary stays out too.
Model Context Protocol is the open standard Anthropic introduced in November 2024 and contributed to the Agentic AI Foundation under the Linux Foundation in December 2025. A single tool server can plug into any client that speaks the protocol.
Tool definitions are context too. Anthropic’s API documentation counts tool names, descriptions and schemas as input tokens.
Anthropic’s engineering post on context engineering names “bloated tool sets” as one of the most common failure modes and adds: “If a human engineer can’t definitively say which tool should be used in a given situation, an AI agent can’t be expected to do better.”
More tools also means more to choose from. Anthropic’s tool search documentation says Claude’s ability to pick the right tool degrades once you exceed 30–50 available tools. By default, Claude Code loads only MCP tool names and server instructions until the agent uses a tool; run /context to see what your own setup spends.
Every model has a knowledge cutoff. Anthropic’s Transparency Hub says a Claude model’s knowledge base is “most extensive and reliable” up to that date.
Its web search documentation describes live pages as a way to get “up-to-date information beyond its knowledge cutoff”. For a library release newer than that date, give the agent the version and its documentation.
Inline completion works at a smaller scale: it leans, per GitHub’s documentation for Copilot, on the code around the cursor, snippets from open tabs and a comment that states what you want.
Where more context stops helping
More text is not automatically better. Three documented effects limit it, and none was measured on coding tasks.
- Position. In the models Liu and colleagues tested, accuracy on question answering and key-value retrieval changed with where the relevant item sat, often best at the start or end and worse in the middle.
- Distractors. In Chroma’s 2025 tests of 18 models, performance did not hold steady as input grew, though task difficulty was held constant, and even a single distractor lowered it.
- Compaction. When a tool summarizes a long conversation to make room, Anthropic warns, overly aggressive summarizing can lose subtle but critical details from earlier turns.
A larger window helps when the right information lives in it; it does not make every token equally useful.
Rule of thumb: give the tool the smallest context that still contains the answer. That follows Anthropic’s advice to find the smallest set of high-signal tokens; the studies cited here did not test coding tasks.
When it breaks: supplied context can be stale, contradictory or incomplete, and the model can still write a wrong patch. A model given the same tokens twice can produce different completions, because sampling picks each next token at random from a probability distribution, as Hugging Face’s documentation on text generation explains.
Context engineering improves the odds; it does not remove the variance, so, as GitHub’s guidance says, review and test what the assistant generates.
Cheat sheet
The article on one page: open it full size, save it or share it.

AI-assisted content, human-reviewed. Images AI-generated. Editorial Standards · Our Editors