Skip to content

Context engineering

What occupies the model's window on this request, in what order, assembled by what code, and what gets evicted when the next thing no longer fits.

Prompt engineering asks how to word an instruction. Context engineering asks a different question, and it only becomes the interesting one once an application runs for more than a single turn: given a window with a fixed capacity and a price per token, what should be inside it right now? A chat that has been going for forty turns, an agent that has called nine tools, a coding assistant that has read six files — none of those has a prompt in any useful sense. Each has an assembled context, built by code somebody wrote, out of parts that compete for room.

That code is the subject here. These pages are about allocation between the system block, tool schemas, conversation history, retrieved documents and tool output; about compaction that summarises old turns without dropping the decision made in them; about eviction with a stated priority order rather than an accident; and about the state that has to survive when the session ends, the agent hands off, or the model is swapped. Token counting itself belongs to the tokens cluster, phrasing belongs to prompt engineering, and retrieval quality belongs to RAG. What is left over is the assembly, and the assembly is where long-running applications actually break.

Context Engineering: The Discipline After Prompting

A definition of context engineering that draws a hard boundary against prompt engineering, and the four questions every context assembler has to answer.

4 min read

Budgeting the Context Window Across a Session

A working allocator that divides a fixed window between system, tools, history and retrieved material, with floors, priorities and proportional shrink.

5 min read

Conversation Compaction: Summarising Without Losing State

A compaction routine that keeps recent turns verbatim and compresses older ones into a structured state record, with the rule that stops summaries decaying.

5 min read

Memory Architectures for Long-Running Assistants

Four ways to give an assistant memory across turns and sessions, each with its per-turn token cost derived from stated assumptions.

4 min read

What to Put in the System Prompt vs Retrieve on Demand

A placement rule derived from hit rate, block size and the cached-token discount, deciding when a block belongs in the static prefix and when it should be fetched.

5 min read

Tool Results Are Context Too

Why tool output is the block most likely to destroy a context budget, and an adapter that bounds it before it reaches the window.

5 min read

Context Rot: Why Long Sessions Get Worse

What published long-context research actually found, and the three mechanical causes of decay in a long session that are properties of your assembly code rather than of the model.

5 min read

Structuring Context for Retrieval Within the Window

Why identical content in a different order is a different request, and an ordering function that resolves the conflict between relevance order and cache order.

5 min read

Multi-Agent Context: What Each Agent Should See

Why sub-agents should receive a brief rather than a transcript, with the cost multiplier that shared context implies and the contamination it causes.

5 min read

Handoff Between Sessions and Agents

A handoff document that carries a run across a session boundary, an agent boundary or a model change, and the provider-specific things it must not contain.

5 min read

Scratchpads and Externalised Working Memory

When a file beats the window as a place to keep working state, with the break-even in read frequency and the properties files have that context does not.

5 min read

Context Caching Strategy: Ordering for Cache Hits

How to lay out a prompt so the longest possible prefix is byte-identical between requests, and what quietly breaks it.

5 min read

Selective Attention: Filtering Before You Fill

A relevance gate that runs before allocation, the arithmetic of whether the filter pays for itself, and the cases where stuffing the window is genuinely correct.

5 min read

Instruction Placement: Top, Bottom or Both?

What the published position research supports about where instructions go, the cache cost of each placement, and a harness for settling it on your own task.

5 min read

Managing Context in a Coding Agent

Repo maps, file windows and an eviction policy with a stated priority order, for the hardest context-management problem in common use.

5 min read

Context for RAG vs Context for Agents

Two workloads that fill the same window for opposite reasons, and why a context strategy tuned for one is wrong for the other.

4 min read

Token Accounting Across a Long Session

Why a long conversation costs far more than the text it contains, derived: re-sent history makes total input quadratic in the number of turns.

5 min read

Structured Context: XML, JSON or Prose?

The token overhead each encoding imposes on the same payload, why JSON is the expensive one, and a script that counts it on your own tokenizer.

4 min read

Context Windows as an Architectural Constraint

Designing a system around a fixed token budget the way you would design around a memory limit, including what has to be decided before any of it works.

4 min read

Debugging a Context Problem

A diagnostic order that separates a model problem from a prompt problem from a context problem, starting with the dump that answers most of them immediately.

5 min read

Context engineering · Multigrid