MarkTechPost@AI 07月23日 03:13
Context Engineering for AI Agents: Key Lessons from Manus
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文探讨了构建高效AI代理的关键——上下文工程。Manus项目发现,设计和管理AI处理的信息(即“上下文”)对代理的速度、成本、可靠性和智能至关重要。与传统的微调相比,利用前沿模型的上下文学习能实现快速迭代。文章详细介绍了Manus在上下文工程方面的六个核心经验:围绕KV-Cache设计以提高性能;通过掩码而非移除来管理工具;利用文件系统作为无限上下文;通过“默写”操纵注意力;保留错误信息以促进学习;以及通过引入多样性避免“少样本学习”陷阱。掌握这些原则是构建强大AI代理的基础。

🎯 **围绕KV-Cache设计以优化性能**:KV-Cache是AI代理性能的关键,直接影响延迟和成本。通过保持系统提示的稳定性、采用追加式上下文以及明确设置缓存断点,可以最大化KV-Cache的命中率,显著降低处理时间和成本。

🎭 **通过掩码而非移除来管理工具**:当AI代理拥有更多工具时,为了避免模型混淆和保持上下文稳定,Manus项目建议使用上下文感知状态机来管理工具可用性,通过掩码(masking)来控制token logits,而不是直接移除工具定义,这能防止模型选择无效或不恰当的动作。

🗄️ **将文件系统作为上下文的延伸**:对于超出模型上下文窗口限制的真实世界观察,Manus项目将文件系统视为无限上下文。代理可以按需读写文件,将文件系统用作外部化、结构化内存,并通过可恢复的压缩策略(如保留URL但移除页面内容)来缩减上下文长度,避免永久性数据丢失。

📝 **通过“默写”操纵注意力以维持全局目标**:在复杂的多步任务中,AI代理可能失去焦点或忘记长期目标。Manus项目通过让代理不断重写一个todo.md文件,将目标和进度“默写”到上下文末尾,从而偏向模型的注意力,使其聚焦于全局计划,缓解“迷失在中间”的问题。

💡 **保留错误信息以促进学习和恢复**:AI代理的错误(如幻觉、错误行为)是学习过程的一部分。Manus项目发现,保留失败的动作和观察信息在上下文中,能够隐式更新模型的内部信念,帮助其学习并减少重复犯错的可能性,将错误恢复视为真实代理行为的关键指标。

🔄 **通过引入多样性避免“少样本学习”陷阱**:虽然少样本提示对LLM有效,但在代理中可能导致模仿和次优行为。Manus项目通过引入序列化模板、措辞或格式上的微小变化,制造“噪声”来打破重复模式,转移模型注意力,防止其陷入僵化的模仿,从而实现受控的多样性。

Building effective AI agents means more than just picking a powerful language model. As the Manus project discovered, how you design and manage the “context” – the information the AI processes to make decisions – is paramount. This “context engineering” directly impacts an agent’s speed, cost, reliability, and intelligence.

Initially, the choice was clear: leverage the in-context learning of frontier models over slow, iterative fine-tuning. This allows for rapid improvements, shipping changes in hours instead of weeks, making the product adaptable to evolving AI capabilities. However, this path proved far from simple, leading to multiple framework rebuilds through what they affectionately call “Stochastic Graduate Descent” – a process of experimental guesswork.

Here are the critical lessons learned at Manus for effective context engineering:

1. Design Around the KV-Cache

The KV-cache is vital for agent performance, directly affecting latency and cost. Agents continuously append actions and observations to their context, making the input significantly longer than the output. KV-cache reuses identical context prefixes, drastically reducing processing time and cost (e.g., a 10x cost difference with Claude Sonnet).

To maximize KV-cache hits:

2. Mask, Don’t Remove

As agents gain more tools, their action space becomes complex, potentially “dumbing down” the agent as it struggles to choose correctly. While dynamic tool loading might seem intuitive, it invalidates the KV-cache and confuses the model if past context refers to undefined tools.

Manus instead uses a context-aware state machine to manage tool availability by masking token logits during decoding. This prevents the model from selecting unavailable or inappropriate actions without altering the core tool definitions, keeping the context stable and the agent focused.

3. Use the File System as Context

Even with large context windows (128K+ tokens), real-world agentic observations (like web pages or PDFs) can easily exceed limits, degrade performance, and incur high costs. Irreversible compression risks losing crucial information needed for future steps.

Manus treats the file system as the ultimate, unlimited context. The agent learns to read from and write to files on demand, using the file system as externalized, structured memory.Compression strategies are always designed to be restorable (e.g., keeping a URL but dropping page content), effectively shrinking context length without permanent data loss.

4. Manipulate Attention Through Recitation

Agents can lose focus or forget long-term goals in complex, multi-step tasks. Manus tackles this by having the agent constantly rewrite a todo.md file. By reciting its objectives and progress into the end of the context, the model’s attention is biased towards its global plan, mitigating “lost-in-the-middle” issues and reducing goal misalignment. This leverages natural language to bias the AI’s focus without architectural changes.

5. Keep the Wrong Stuff In

Agents will make mistakes – hallucinate, encounter errors, misbehave. The natural impulse is to clean up these failures. However, Manus found that leaving failed actions and observations in the context implicitly updates the model’s internal beliefs. Seeing its own mistakes helps the agent learn and reduces the chance of repeating the same error, making error recovery a key indicator of true agentic behavior.

6. Don’t Get Few-Shotted

While few-shot prompting is powerful for LLMs, it can backfire in agents by leading to mimicry and sub-optimal, repetitive behavior. When the context is too uniform with similar action-observation pairs, the agent can fall into a rut, leading to drift or hallucination.

The solution is controlled diversity. Manus introduces small variations in serialization templates, phrasing, or formatting within the context. This “noise” helps break repetitive patterns and shifts the model’s attention, preventing it from getting stuck in a rigid imitation of past actions.

In conclusion, context engineering is very new but a critical field for AI agents. It goes beyond raw model power, dictating how an agent manages memory, interacts with its environment, and learns from feedback. Mastering these principles is essential for building robust, scalable, and intelligent AI agents.


Sponsorship Opportunity: Reach the most influential AI developers in US and Europe. 1M+ monthly readers, 500K+ community builders, infinite possibilities. [Explore Sponsorship]

The post Context Engineering for AI Agents: Key Lessons from Manus appeared first on MarkTechPost.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

AI代理 上下文工程 KV-Cache LLM Manus项目
相关文章