跪拜 Guibai
← All articles
Artificial Intelligence

Context Engineering for AI Agents: Six Hard-Won Lessons from Building Manus

By oil欧哟 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

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.

Summary

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.

Takeaways
— Manus chose context engineering over fine-tuning because it allows iteration in hours instead of weeks, and it keeps the product architecture decoupled from underlying model changes.
— Manus agents average a 100:1 input-to-output token ratio, making KV-cache hit rate the single most important performance metric.
— Cached input tokens cost $0.30 per million for Claude Sonnet, while uncached ones cost $3 per million, a 10x difference.
— Keep the system prompt prefix stable; avoid adding precise timestamps at the start, as any token change invalidates all subsequent cache.
— Context must be append-only with deterministic serialization; unstable JSON key order can silently break cache.
— MCP and user-provided tools can overwhelm an agent's action space, making it dumber; dynamic add/remove of tools mid-loop invalidates cache and causes confusion.
— Manus uses a state machine that masks token logits, not tool removal, to constrain actions. Response prefill (e.g., using Hermes format) enables this without modifying tool definitions.
— The file system is treated as externalized memory; compression is restorable (e.g., keeping a URL instead of page content) to avoid irreversible information loss.
— Reciting goals via a todo.md file at the end of the context helps the agent maintain focus over long loops (average 50 tool calls).
— Leaving failed attempts in context helps the model learn from errors; error recovery is a key differentiator but is ignored by most benchmarks.
— Few-shot prompting can cause repetitive behavior; introducing controlled randomness in actions and observations breaks the pattern and improves robustness.
Conclusions

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.

Concepts & terms
KV cache
A cache that stores precomputed attention keys and values for repeated context prefixes. When an agent sends a context with the same prefix as before, the service reuses the cached computation, cutting cost and latency. A one-token change in the prefix invalidates all subsequent cache.
Few-shot prompting
A technique where a prompt includes a few input-output example pairs, letting the model infer the desired pattern. It works well for single-turn formatting but can cause repetitive behavior in multi-step agent loops.
State space model (SSM)
An alternative to the Transformer architecture (e.g., Mamba) that compresses the entire context into a fixed-size internal state, making it fast and memory-efficient but unable to precisely look back at earlier tokens. An SSM could pair with external file storage to compensate for its lack of attention.
Response prefill
A technique that pre-fills the initial tokens of a model's response during inference, effectively forcing it to continue from a specific starting point. It can constrain tool calls without modifying the system prompt or tool definitions.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗