Skip to content

Context engineering and compression

Treat context as a scarce budget: pack the right instructions, evidence and history in the right order.

7 MIN READUPDATED 2026-11-18Context & protocols
ORIGINAL DIAGRAM · 4 STEPS1Separate stable instruc…2Place retrieved evidenc…3Summarise old turns into4Include only tools rele…

Original flow diagram from note steps — not a copied image

§ 00Notes & rules
Stable prefix first
Place static system instructions and reusable policy at the start of the prompt to maximise prompt-cache hits. Put per-request data (user input, retrieved chunks) after the stable prefix.
Evidence is not instruction
Retrieved documents and tool output are untrusted data. Delimit them clearly so the model does not follow instructions embedded inside evidence.
§ 01Key concepts
Context engineeringDesigning what goes into the model's context, in what form and order.
Context rotQuality loss as irrelevant or stale content fills the window.
CompactionSummarising older history to free space while retaining key facts.
§ 02When to use
  • Long sessions or large document sets.
  • Answers degrade as conversations grow.
  • Token costs rise without better results.
§ 03How it works
  1. 01Separate stable instructions from task-specific context.
  2. 02Place retrieved evidence near the question it supports.
  3. 03Summarise old turns into a compact state instead of replaying everything.
  4. 04Include only tools relevant to the current step.
§ 04Use cases & scenarios

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.
§ 05Examples

Context assembly order

text
[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 message

Keep a token budget per section and compact old history into structured state when nearing the limit.

Compaction checkpoint for a support session

json
{
  "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.

§ 06Trade-offs

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

§ 07Implementation checklist
  • 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.
§ 08Configuration reference
KeyTypical valueNote
context_budgetper taskReserve output headroom.
summary_trigger70–80% fullCompaction point.
history_turnssmallKeep recent turns verbatim.
§ 09Troubleshooting
SymptomCauseFix
Model follows text inside a documentUntrusted content looks like instructionsDelimit and label evidence clearly.
Long sessions forget factsOver-aggressive compactionPersist key facts in a structured state.
§ 10Frequently asked questions

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