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

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 history

Notes:

  • summarise can 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.