Context Engineering Strategies for Production AI Agents
manus · Tech · 2025 · source Agentic: yes · promotional bias: low · source-backed · extracted with prompt 0.5.6 (schema v2)
Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.
Problem
Domain: Production general-purpose AI agents
Manus had to build an agent architecture that could improve quickly without training a custom end-to-end model, while keeping long tool-use loops fast, cheap, and stable as context grows and the action space expands.
Constraints:
- The average agent loop has much longer input than output, making prefix prefill and KV-cache reuse central to cost and latency.
- Tool definitions can explode as MCP/user-configurable tools are added.
- Context windows of 128K or more still fail under real agent workloads and can degrade performance.
- Compression must avoid losing observations that might matter many steps later.
- Agent behavior is shaped by the examples and failures left in context.
Architecture
Manus chose context engineering on top of frontier models. Its production loop keeps prompt prefixes stable and append-only for KV-cache reuse, masks tools instead of dynamically removing them, uses the file system as restorable external memory, keeps task goals recent via todo.md recitation, preserves failed actions for recovery, and adds controlled context variation to avoid repetitive-action ruts.
Diagram confidence: explicit (topology described by the source).
flowchart LR user-request["User request"] --> agent-loop["Agent loop"] agent-loop --> virtual-machine-sandbox["Virtual machine sandbox"] virtual-machine-sandbox --> file-system-memory["File-system memory"] agent-loop --> action-space-state-machine["Action-space state machine"] action-space-state-machine --> append-only-context["Append-only context"] agent-loop --> append-only-context append-only-context --> kv-cache-layer["KV-cache layer"] agent-loop --> todo-memory["Todo memory"]
Components
| Component | Role | Interacts with |
|---|---|---|
User request |
Initial task input that starts the Manus agent loop. | Agent loop |
Agent loop |
Model-driven loop that selects actions, receives observations, and continues until task completion. | User request, Virtual machine sandbox, Action-space state machine, Append-only context, Todo memory |
Virtual machine sandbox |
Execution environment for actions and externalized file-system state. | Agent loop, File-system memory |
Action-space state machine |
Context-aware tool-availability controller that masks token logits rather than removing tools mid-iteration. | Agent loop, Append-only context |
Append-only context |
Deterministic action/observation history optimized for KV-cache stability. | Agent loop, KV-cache layer |
File-system memory |
Externalized, restorable context store for files, documents, web pages, and long-lived state. | Virtual machine sandbox, Agent loop |
Todo memory |
Natural-language task recitation mechanism that keeps objectives in recent attention. | Agent loop |
KV-cache layer |
Inference cache whose hit rate determines much of the cost and latency profile for long-input agent loops. | Append-only context |
Data flow
A user request enters the Manus agent loop. The model selects actions from a predefined action space; the environment executes them in a virtual machine sandbox; observations append to context. To keep the loop stable, Manus preserves deterministic append-only context, offloads restorable details to files, masks unavailable tools by state, and keeps task objectives near the end of context.
Why this architecture
Manus chose context engineering because it allowed faster iteration than fine-tuning and kept the product orthogonal to fast-moving model progress. The architecture focuses on KV-cache stability because agent inputs dwarf outputs and cached prefixes materially change cost and latency.
Models used: Frontier models · Claude Sonnet · GPT-3 · Flan-T5
Scale
- Manus reports an average input-to-output token ratio around 100:1.“In Manus, for example, the average input-to-output token ratio is around 100:1.”article, stated
- Cached Claude Sonnet input tokens are described as 10x cheaper than uncached input tokens.“with Claude Sonnet, for instance, cached input tokens cost 0.30 USD/MTok, while uncached ones cost 3 USD/MTok—a 10x difference.”article, stated
- A typical Manus task requires about 50 tool calls on average.“A typical task in Manus requires around 50 tool calls on average.”article, stated
- The post says Manus learned from real-world testing across millions of users.“At Manus, we've learned these lessons through repeated rewrites, dead ends, and real-world testing across millions of users.”article, stated
Alternatives considered (roads not taken)
Train an end-to-end agentic model on open-source foundations
Rejected because: Past fine-tuning cycles took weeks and were vulnerable to frontier model progress making the custom model obsolete.
Dynamically add or remove tools mid-iteration
Rejected because: Tool-definition changes invalidate KV-cache prefixes and confuse the model when old actions reference removed tools.
Overly aggressive compression of context
Rejected because: Irreversible compression can remove observations needed for future action prediction.
Patterns
semantic-caching (model-operations)
Manus keeps prompt prefixes stable, context append-only, and cache breakpoints explicit because KV-cache hit rate drives agent latency and cost.
tool-use-orchestration (orchestration)
Tool availability is controlled by a context-aware state machine and logit masking instead of changing the serialized tool set.
agent-memory (context)
The file system is used as externalized structured memory that the model can write to and read from on demand.
context-compaction (context)
Manus shrinks context by dropping restorable content when URLs or sandbox paths remain available.
fallback-degradation (reliability)
Failed actions and stack traces are intentionally preserved so the model can update its beliefs and avoid repeating mistakes.
Tradeoffs
Product adaptation strategy
Chose context engineering on frontier models over training an end-to-end open-source agentic model.
Rationale: Context engineering let the team ship improvements in hours instead of weeks and stay aligned with model progress.
Tool availability control
Chose masking token logits based on context state over removing tools from the serialized action space.
Rationale: Masking avoids KV-cache invalidation and stale references while still enforcing state-specific tool choices.
Context compression
Chose restorable file-based externalization over irreversible compression/truncation.
Rationale: Agents may need any prior observation later, so Manus keeps a path or URL that can restore omitted content.
Gotchas (what bit them)
KV-cache can be silently destroyed by unstable prompt prefixes.
Root cause: Even a single-token difference near the beginning of the prompt invalidates the cache from that point onward.
Resolution: Keep prompt prefixes stable, avoid precise timestamps at the start, and serialize context deterministically.
Dynamic tool loading makes the model dumber.
Root cause: Large user-configurable action spaces and mid-iteration tool changes increase wrong actions, schema violations, and hallucinated actions.
Resolution: Keep tools serialized and use state-machine/logit masking to prevent or enforce actions.
Long contexts still create lost-goal behavior.
Root cause: A 50-tool-call loop is vulnerable to drifting off-topic or forgetting earlier goals.
Resolution: Use todo.md task recitation to keep the plan in the recent attention span.
Uniform context can create repetitive action ruts.
Root cause: Models imitate repeated action-observation patterns even when they are no longer optimal.
Resolution: Add small controlled variation in serialization, phrasing, order, or formatting.
Evaluation
Manus judges context architecture through production metrics and behavior: KV-cache hit rate, latency/cost, action stability, task drift, recovery after errors, and repeated real-world rewrites.
Metrics: KV-cache hit rate · time-to-first-token · inference cost · input-to-output token ratio around 100:1 · typical 50 tool calls per task
Outcomes
- Context engineering allowed Manus to ship improvements in hours instead of weeks.
- The team rebuilt the agent framework four times while searching for better context-shaping strategies.
- The resulting patterns support faster, cheaper, more recoverable agent loops at production scale.
The article cites millions of users, average 50 tool calls per task, 100:1 input/output token ratio, and a 10x cached-vs-uncached Claude Sonnet input-token price difference.