Kolkata ·  
AI engineering
retrieval
deep · 6 min

Context engineering

Choosing, on every call, what goes into the model's context window and what stays out.

updated 27 Sept 2026

Context windows hold hundreds of thousands of tokens now. So the easy plan is to put everything in. The whole chat history, every tool, every document that might help. The model can sort it out, right?

Here's a test that says no. Take a long chat history and ask a question about it. Version one gets the whole history, about 113,000 tokens. Version two gets only the parts that matter, about 300 tokens. Same question, same facts. Every model tested did clearly better on the short one. The only thing that changed was how much noise sat around the answer.

That's why we can't just fill the window. Every token we add spends a bit of the model's attention, and as the context window fills up, models get less precise at using what's in it. It's a slow slide, not a cliff, but it's there. So someone has to decide, on every call, what goes in and what stays out. That job is context engineering.

What's actually in there

When people hear "prompt", they picture the instructions. Let's look at what a support agent really sends, thirty turns into a conversation:

  • the system prompt
  • definitions for a dozen tools
  • a few worked examples
  • three help articles pulled in by RAG
  • all thirty earlier turns, resent in full the way the chat API works
  • eight tool results, some of them long blobs of raw JSON

The instructions are a small slice of that. And the window gets rebuilt on every call, so the choice of what's in it gets made again every turn. That's the difference from prompt engineering, which is about writing the instructions well. Once apps started calling a model dozens of times in a loop, the bigger job was managing that whole pile.

(The name caught on in mid-2025. Andrej Karpathy compared the model to a CPU and its window to RAM: working memory you manage on purpose. Part of why the name stuck is that "prompt engineering" got read as "typing things into a chatbot", and this one is harder to misread.)

How a full window goes wrong

So what does too much context actually do? Drew Breunig gave four names to it.

Poisoning. A mistake gets into the context, say a hallucination from an earlier step, and the model keeps referring back to it. An agent playing Pokémon wrote down a wrong game state and then chased goals that were impossible.

Distraction. The history grows so long that the model leans on it instead of what it learned in training. That same agent, past about 100,000 tokens, started repeating old actions instead of making new plans.

Confusion. Stuff that isn't needed drags the answer down. A small compressed model failed a task when it was given 46 tools, and got it right with 19.

Clash. New information conflicts with what's already there. Feed a task in pieces across several turns instead of all at once, and scores dropped 39% on average.

All four are about what's in the window, not how big the window is.

Four moves

Most of the fixes fall into four moves, all aimed at the same window.

Write things down outside the window. An agent can keep a plan, a to-do list or a notes file and read it back when it needs it. When Claude played Pokémon, it kept notes like how many steps it had spent training on Route 1. If the window gets cleared, the important state is still there.

Select only what this step needs. RAG is the classic version: search your documents and add only the relevant chunks. The same works for tools. Instead of listing all 46 on every call, retrieve the few that fit the task.

There's a choice about when we select. We can retrieve up front, before the model starts, which is fast but means guessing what it'll need. Or we can go just in time: hand the model pointers, like file paths or saved queries, plus tools to open them, and let it load what it needs as it goes. Claude Code looks into large files with head, tail and grep without ever loading them whole. That's slower, but the window stays clean. Plenty of setups do both: Claude Code drops its CLAUDE.md file in up front and searches for everything else.

Compress what's already there. When a conversation nears the limit, summarize it and start a fresh window from the summary. That's compaction. A good summary keeps the decisions made, the open bugs and the key details, and drops tool output that's already been used. The lightest version is just clearing old tool results: once the agent has read a long output and acted on it, it rarely needs the raw text again. If you write a compaction prompt, tune it first so it misses nothing, then tighten it to cut what doesn't matter.

Isolate work in separate windows. Give a subtask to a sub-agent with its own clean window. It does the messy searching and reading there, and sends back a summary of maybe 1,000 to 2,000 tokens. The main window never sees the mess. The price is tokens: Anthropic has reported multi-agent setups using up to 15 times more than a plain chat.

None of these is free. Just-in-time loading is slower. Compaction can drop the one detail that turns out to matter. Sub-agents multiply the bill. You pick based on what you measure.

Tools are context too

It's easy to forget that tool definitions sit in the window on every call, and the model has to choose between them. A common failure is a bloated tool set, where tools overlap so much that it's unclear which one fits. A useful test: if an engineer on your team couldn't say for sure which tool to use in a situation, don't expect the model to. Fewer tools with clear edges are easier to use well.

Is this just a new name?

Partly. Choosing what goes in the prompt was always part of prompt engineering. What changed is that apps became loops, and each call needs its window rebuilt from a growing pile of history, results and notes.

The bigger question is whether it all goes away as windows keep growing. Nobody knows for sure. The slide in accuracy as windows fill has shown up on every model tested so far, and the four failures are about content, not size. Anthropic expects treating context as scarce to stay central as models improve, but that's a forecast. (Also worth knowing: the focused-versus-full test came from Chroma, which sells a retrieval database, though other studies in context window point the same way.) Where things sit in the window matters too, which is lost in the middle, and when to skip retrieval and send everything is long context.

If you want one thing to try, log the full window your app sends on a real call, late in a real session. Compare it with that 300-token focused prompt from the start, and see how much of what you send the model actually needed.

Further reading