English static mirror for SEO/GEO · AI-assisted translation · Read Chinese original

Context Engineering for AI Agents: Lessons from Building Manus

Forum topic · QianXun · 2026-01-04

Summary

This post explains the Manus team's context engineering practices for building production-grade AI agents, arguing that context engineering outperforms fine-tuning for agent development. Key insights include: KV-cache hit rate dominates inference cost and TTFT for long-input/short-output agent workloads, so system design should prioritize prefix stability, append-only context, and explicit cache breakpoints; logit masking with constrained decoding is a more reliable and token-efficient way to manage large tool sets than prompt-based tool swapping; the file system serves as unlimited external memory, letting agents load content on demand to avoid the lost-in-the-middle problem; a todo.md recitation pattern counters recency bias in long tasks; keeping erroneous action-observation pairs in context improves learning from mistakes; and injecting structured noise prevents few-shot mode collapse. The article distills a philosophy for moving from demos to production systems, citing the Manus blog, 'Lost in the Middle', and 'Attention Is All You Need'.

Context Engineering for AI Agents: Lessons from Building Manus

> "The agentic future will be built one context at a time. Engineer them well."

This forum post discusses the Manus team's practical lessons on context engineering — building, managing, and optimizing the input context fed to frontier LLMs — as the preferred path to production-grade AI agents, rather than traditional fine-tuning. The author jokingly calls this iterative trial-and-error craft "Stochastic Graduate Descent," a pun on gradient descent.

Key points

1. Why context engineering over fine-tuning?

  • Fine-tuning pipelines have slow (week-scale) feedback loops and are invalidated whenever a new base model generation ships.
  • In-context learning leverages frontier models directly; agent tasks are long-horizon, multi-step, and uncertain, making context the true lifeline.
  • 2. KV-Cache is the invisible lifeline

    Agent workloads have extremely asymmetric token ratios (~100:1 input:output), so inference cost and TTFT are almost entirely determined by cache hit rate:

    \[Cost_{inference} \propto (1 - \text{CacheHitRate}) \times N_{input} + N_{output}\]

    Three key practices: 1. Prefix stability — keep the head of the context static; never put a per-second timestamp at the start of the system prompt (a common anti-pattern that invalidates all downstream KV cache). 2. Append-only context — never modify past actions or observations; keep serialization deterministic (even JSON key order must be fixed). 3. Explicit cache breakpoints — manually insert markers in inference frameworks without automatic incremental caching.

    These details cut per-task cost from dollars (demo level) to cents (production level).

    3. Taming tool explosion with logit masking

    Instead of dynamically adding/removing tool definitions in the prompt (which confuses the model when histories reference vanished tools), Manus masks logits at decoding time (constrained decoding):

    \[P(token|context) = \text{Softmax}(Logits + Mask)\]

    Implementation details:

  • A finite state machine tracks the currently allowed tool set; disallowed tokens get −∞ probability.
  • Prefill with special tokens (e.g., im_start) to quickly enter tool-call mode.
  • Normalize tool names by prefix (browser_, shell_) for prefix masking.
  • Support Auto / Required / Specified invocation modes.
  • This moves control logic from the expensive context to the nearly free decode stage.

    4. The file system as unlimited external memory

  • Store only URLs or file paths in context instead of full webpages/PDFs/repos.
  • The model calls read_file(path) to load content on demand, then optionally compresses/summarizes it before appending.
  • This sidesteps the "Lost-in-the-middle" attention problem (Liu et al., arXiv:2307.03172).
  • The author likens this to a Neural Turing Machine and speculates that State Space Models (e.g., Mamba, arXiv:2312.00752) with native file I/O could become a more natural agent architecture.
  • 5. Recitation against attention decay

    In 50+ step tasks, the original goal drifts out of attention due to recency bias:

    \[Attention(Q, K, V) = \text{softmax}(\frac{QK^T}{\sqrt{d_k}})V\]

    Manus has the agent maintain a todo.md and recite current progress and remaining goals at the end of the context each step, forcing the global plan back into the strongest attention positions.

    6. Keep error trajectories

    Rather than wiping the context after failures, Manus deliberately retains erroneous actions and their full error observations. Repeated "Action A → Error X" patterns act as negative samples, letting the model naturally lower the probability of repeating the mistake — recovery from errors is core agent intelligence.

    7. Avoid the few-shot trap with structured noise

    When many similar action-observation pairs accumulate (e.g., processing 20 resumes), models fall into mode repetition, loops, or hallucination. Countermeasures:
  • Slightly randomize serialization templates (wording, field order).
  • Vary the phrasing of observation summaries.
  • Tweak tool-call formats.

Philosophy: from Demo to Production

The post condenses Manus's lessons into four maxims:

1. KV-Cache is the lifeline — all design must accommodate caching. 2. Decode-time intervention (logit masking) beats prompt engineering — more reliable and token-efficient. 3. Bigger context isn't better — use external storage and on-demand loading to maintain high signal-to-noise ratio. 4. True robustness comes from embracing errors, not from pursuing one perfect plan.

References

1. Manus Team. Context Engineering for AI Agents: Lessons from Building Manus. https://manus.im/blog/Context-Engineering-for-AI-Agents-Lessons-from-Building-Manus 2. Liu, J. et al. Lost in the Middle: How Language Models Use Long Contexts. arXiv preprint arXiv:2307.03172. 3. Vaswani, A. et al. Attention Is All You Need. Advances in Neural Information Processing Systems 30 (2017). 4. Gu, A. et al. Mamba: Linear-Time Sequence Modeling with Selective State Spaces. arXiv preprint arXiv:2312.00752. 5. OpenAI. GPT-4 Technical Report. arXiv preprint arXiv:2303.08774.

Tags

#context-engineering#ai-agents#kv-cache#logit-masking#llm#manus#tool-use#production-systems

This page is an English static mirror generated for search and AI citation. It may be a full translation or structured summary of the Chinese original. Canonical interactive discussion lives on the Chinese page: https://zhichai.net/topic/176415231