| Context engineering | Designing what goes into the model's context, in what form and order. |
| Context rot | Quality loss as irrelevant or stale content fills the window. |
| Compaction | Summarising older history to free space while retaining key facts. |
- Long sessions or large document sets.
- Answers degrade as conversations grow.
- Token costs rise without better results.
- 01Separate stable instructions from task-specific context.
- 02Place retrieved evidence near the question it supports.
- 03Summarise old turns into a compact state instead of replaying everything.
- 04Include only tools relevant to the current step.
Long support session that stops forgetting
- Context
- A support conversation runs for dozens of turns.
- Problem
- Early decisions are lost and the assistant starts contradicting itself.
- Approach
- Compact old turns into structured state with facts and open issues, keeping recent turns verbatim.
- Outcome
- The session stays coherent without replaying the entire transcript.
Evidence that is not an instruction
- Context
- Retrieved documents contain text that looks like a system command.
- Problem
- The model follows text inside the evidence instead of treating it as data.
- Approach
- Delimit and label retrieved content and keep a stable, cacheable instruction prefix.
- Outcome
- Safer behaviour and better cache hit rates.
Context assembly order
[STABLE PREFIX — cacheable]
System role + policies + output contract
Tool definitions relevant to this step
[SESSION STATE — compacted]
Key facts / decisions / summaries
[EVIDENCE — per request, delimited]
<retrieved source="chunk:..." > ... </retrieved>
<tool_result name="..." > ... </tool_result>
[USER REQUEST]
Latest user messageKeep a token budget per section and compact old history into structured state when nearing the limit.
Compaction checkpoint for a support session
{
"facts": ["tenant=acme", "order=ORD-104200"],
"decisions": ["refund path chosen"],
"open_issues": ["awaiting warehouse confirmation"],
"drop": ["earlier small talk", "old tool dumps"]
}Compact old turns into structured state and keep recent turns verbatim; key facts should survive compaction.
Full history
Up: Nothing forgotten
Down: Cost, latency and context rot
Aggressive summaries
Up: Compact and cheap
Down: Can lose crucial details
Just-in-time retrieval
Up: Only what is needed
Down: Retrieval failures become context failures
| Option | Upside | Cost |
|---|---|---|
| Full history | Nothing forgotten | Cost, latency and context rot |
| Aggressive summaries | Compact and cheap | Can lose crucial details |
| Just-in-time retrieval | Only what is needed | Retrieval failures become context failures |
- Give role boundaries explicitly when joining multiple sources.
- Mark retrieved content as evidence, not instructions.
- Keep a token budget per context section.
- Test with long conversations, not just single turns.
| Key | Typical value | Note |
|---|---|---|
| context_budget | per task | Reserve output headroom. |
| summary_trigger | 70–80% full | Compaction point. |
| history_turns | small | Keep recent turns verbatim. |
| Symptom | Cause | Fix |
|---|---|---|
| Model follows text inside a document | Untrusted content looks like instructions | Delimit and label evidence clearly. |
| Long sessions forget facts | Over-aggressive compaction | Persist key facts in a structured state. |
Is prompt engineering dead?
No — it is a subset of context engineering. The bigger question is what information earns a place in the window.
Should I always summarise history?
Only when the window is under pressure. Recent turns are usually more useful verbatim.
More in Context & protocols