Context Engineering for AI Agents: Six Hard-Won Lessons from Building Manus
For developers building multi-step agents, these patterns directly affect latency, cost, and reliability. Ignoring KV-cache prefix stability can multiply inference bills tenfold, and the masking technique shows how to keep dynamic tools without breaking the loop.
Manus's team lays out a pragmatic playbook for context engineering in AI agents, drawn from building a production system that averages 100 input tokens for every output token. The core insight: treat the context as a carefully managed asset. The first lesson is designing around KV-cache hit rate, since a single changed prefix token can multiply inference costs tenfold. The team keeps prompts append-only, uses deterministic serialization, and inserts cache breakpoints where necessary.
For tool management, they advocate masking token logits rather than removing tool definitions, preserving cache stability and preventing confusion. The file system becomes externalized memory, where compression is always restorable. The agent recites its goals through a todo.md file to steer attention, keeps failed attempts in context for error recovery, and adds diversity to avoid few-shot traps. These six principles form a coherent approach to making multi-step agents faster, cheaper, and more robust.
The 100:1 input-output ratio in agent systems makes KV-cache optimization a financial necessity, not just a performance nicety; a single character change can wipe out the cached prefix and force a 10x re-computation cost.
Masking token logits instead of removing tools is a clever inversion of dynamic action spaces. It preserves cache stability while still allowing state-dependent control, which is critical for long-running agents.
The file system as context is a practical realization of the Neural Turing Machine idea, showing that external memory can extend an LLM's effective context window beyond its architectural limits.
Error recovery is an underrated benchmark metric; production agents fail often, and the ability to learn from failures is what separates robust systems from brittle demos.
The few-shot trap reveals a tension: few-shot examples are useful for formatting but harmful for decision consistency. Diversity injection is a novel way to balance imitation with adaptability.