Skip to main content

Context Engineering

The agent loop rebuilds context each turn. Instructions, tool definitions, conversation history, file reads, search results, and your current task — everything enters the same window, and the model predicts from all of it. What enters, where it lands, and when it expires determines whether the model can do useful work.

Context engineering is the practice of shaping each LLM call's context to be optimal for its task. It applies to every call — not just when the window is full. A fresh session with a clear task benefits from the same thinking as a 1M-token conversation approaching compaction.

Six mechanisms control what enters the context window and where: context files, MCP tool schemas, skills, sub-agents, context compaction, and retrieval. Each changes the parent context differently: some enlarge its fixed prefix, some append task-specific material, some replace accumulated history, and some keep work in a separate window. This module covers the fundamental constraint that governs all six — why position in the window matters — and hands each mechanism to its own module (The Six Context Mechanisms).

The Attention Curve: Why Placement Matters

Context management

Attention is strongest at the edges

Context Fill
30%
Attention is reliable near the beginning and end of the window; the middle degrades first as context fills.

Every window has a weak middle — and a bigger window makes it worse. Attention is never flat: at any size the same profile holds — strong at the start, strong at the end, weak in the middle. What changes with size is the toll, not the shape; a longer window strands more content in that middle. Three zones emerge: the primacy zone at the start (instructions, project files), the recency zone at the end (your task, recent results), and the dead center, where attention drops off sharply.

Attention isn't uniform — it can't be. For each token the model scores every earlier token, then turns those scores into shares that must add up to 100%. That step doesn't split evenly — it over-rewards the strongest scores and squeezes the rest toward zero.1 It's not a choice; it's how transformer attention works. So the attention budget pools where it reliably pays off: a small pouch of opening tokens absorbs an outsized share (they become attention sinks), and a rolling window stays locked on the tokens just before the prediction, because they're what it's about to answer from.23 Everything between the two gets what's left over — a thin, unreliable spread. That's the starved middle.

That budget doesn't grow with the window. The same anchors hold at 10K and 1M — only the middle widens. Under load the curve becomes a J, as primacy erodes while recency holds — and when depends on the model, task, and absolute token count, not a fixed utilization mark: 80% fill is 8K tokens in a 10K window, 800K in a 1M one. Position is normalized, not absolute length, so the curve alone can't say where a model's J-curve begins.4

Context engineering works within that limit: keep the active task where the model can still attend — via context files, schemas, skills, sub-agents, compaction, and retrieval.

But attention isn't recall. The attention curve is the mechanism — how the model's focus is spread across the window. The recall curve is the measured outcome: it hides an answer somewhere in the window and demands the model find it and reproduce it, so it probes the whole range rather than the two bright ends. As the window grows, more content lands where attention is thin, and less comes back.

The Context Pressure Problem

You type a prompt and it lands at the end of the window — the recency zone, the spot attention treats best. Then the agent loops.

The loop rebuilds the window every turn, and that's the point: nothing is static. Instructions, tool schemas, and context files sit at the front; each tool call, result, and file read the loop produces lands behind your prompt; then it all feeds back in for the next turn. Your prompt stays in one place in the sequence, but the window around it reflows on both sides with every step, so the task's position on the attention curve changes constantly — and the loop is what moves it.

That movement is the context pressure problem. The window isn't just filling up; the loop is actively re-placing your task, and it has no reason to keep it where you want it. Left to itself, the loop hands the task to wherever the accumulated content settles, and it can end up in the middle of the window — the band attention drains first — where the model strains to act on it.

Context management

Context pressure moves the task through the window

Context engineering is predicting that drift and shaping it. The loop will keep re-placing your task and won't keep it where you want it, so the discipline is to steer the content that lands around the task: what sits before it, what trails behind it, and what never enters this window at all.

The key insight: it's not about running out of tokens — it's about where your task lands. Two sessions can reach the same footprint yet behave differently, because the loop placed the task differently in each. You predict the movement, then shape the context so the important thing lands where the model can still attend to it.

Scope

This section describes behavior in long-context autoregressive LLMs, especially causal transformers. Diffusion language models denoise a sequence iteratively rather than generate left to right, so they don't share the same causal-mask and recency mechanics. Evaluate their positional reliability separately instead of assuming the same curve.

The Six Context Mechanisms

Compare mechanisms through the same context lens: when tokens load, where they land, what they cost, and what they are useful for. The figure makes those effects visible before the following sections examine their operational tradeoffs.

Context management

Six mechanisms share the same context lens

The six context mechanisms: where tokens land and when they load.

These mechanisms do not sit on one spectrum. Context files and eager MCP schemas enlarge the fixed prefix. Skills and tool-driven retrieval add material when the active task needs it. Sub-agents keep their working trace in another window and return a synthesis. Compaction replaces accumulated conversation with a smaller, lossy state. Effective context engineering means choosing the context effect that matches the knowledge and the task.


Next: Context Files

Footnotes

  1. Vaswani et al. (2017), Attention Is All You Need — the score-to-share step is a softmax, which turns a set of scores into weights that sum to 1 and magnifies the largest.

  2. Xiao et al. (2023), Efficient Streaming Language Models with Attention Sinks.

  3. Beltagy et al. (2020), Longformer: The Long-Document Transformer introduced the bounded sliding window — attention limited to a nearby run of tokens. Modern long-context models push this into hybrid attention: DeepSeek-V4 layers a sliding window with compressed and sparse attention, and GLM-5.3 Flash pairs a linear local window with sparse global retrieval.

  4. Liu et al. (2023), Lost in the Middle: How Language Models Use Long Contexts.