Context engineering means choosing what the LLM sees at each step, and treating the context window as a scarce budget (Anthropic, 2025). As in RAG, too much text buries what matters.
Why it matters
- the history grows at every step, because each step adds one action and one observation
- every step re-reads the whole history, so the cost grows quickly, see Runaway Loops and Agent Cost
- too much text hides the important facts, as in Long Context and Lost in the Middle
Keeping the context lean
- summarise old steps
- keep notes, such as a to-do list, outside the context
- hand a big search to a sub-agent and keep only its summary
Code
Replace old steps by one summary line, and keep the latest steps in full:
def compact(history, keep_last=3, summarise=lambda steps: f"{len(steps)} earlier steps summarised"):
"""Keep the last few steps in full and replace the older ones by a short summary."""
if len(history) <= keep_last:
return history
old, recent = history[:-keep_last], history[-keep_last:]
return [{"call": {"name": "summary", "arguments": {}}, "obs": summarise(old)}] + recent
short = compact(history) # pass this to the LLM instead of the full historyNotes:
summarisecan be an LLM call, and the lambda here is only a placeholder- a summary loses detail, so keep exact facts such as prices and IDs in notes
Some work is shared between several agents, each with its own context, see Multi-Agent Systems.