Context Engineering: The Art and Science of Prompt Architecture

Published on 04 August 2025
60 min read
context-engineering
Context Engineering: The Art and Science of Prompt Architecture

Context Engineering: The Art and Science of Prompt Architecture

Table of Contents

  1. Introduction: The Context Revolution
  2. Lessons from the Trenches: Manus AI’s Context Engineering Mastery
  3. Foundational Research: The Academic Pillars of Context Engineering
  4. Understanding the Economics: Why KV-Cache Optimization Matters
  5. Deep Dive: The Manus Context Engineering Framework
  6. The Context-Aware State Machine: Elegant Guardrails Without Cache Invalidation
  7. Response Prefill: Say No to Stateful Logit Processors
  8. Memory Systems: The Foundation of Persistent Context
  9. LangChain’s Four-Strategy Framework
  10. Anthropic’s Guide to Building Effective Agents
  11. Conclusion: The Art of Engineering Intelligence

Introduction: The Context Revolution

In the rapidly evolving landscape of artificial intelligence, a quiet revolution is taking place - one that doesn’t involve new model architectures or training paradigms, but rather focuses on how we engineer the very foundation of AI interaction: context. As Andrej Karpathy eloquently puts it, LLMs are like a new kind of operating system where the context window serves as RAM - the model’s working memory with limited capacity that requires careful curation.

Context engineering has emerged as what Cognition calls “effectively the #1 job of engineers building AI agents.” This assertion gains weight when we consider that modern AI agents often engage in conversations spanning hundreds of turns, requiring sophisticated context management strategies that go far beyond simple prompt engineering.

The field draws inspiration from foundational research in reasoning and in-context learning. The ReAct paper (Yao et al., 2022) demonstrated how language models could be prompted to generate reasoning traces and task-specific actions in an interleaved manner, establishing the groundwork for agentic behavior through context alone. This was further refined by research into in-context learning dynamics, which revealed how models can adapt their behavior based on demonstrations and instructions provided within their context window, without requiring parameter updates.

Even seemingly abstract concepts like creativity have been shown to be context-dependent. Recent studies on temperature and creativity in language models reveal that the relationship between randomness and creative output is nuanced and heavily influenced by how context is structured and presented to the model.

The practical implications of these insights become clear when examining production systems. Manus AI’s experience building their agent platform reveals that the KV-cache hit rate is arguably the single most important metric for production AI agents, directly affecting both latency and cost. With cached input tokens costing 0.30 USD per million tokens compared to 3.00 USD for uncached ones - a 10x difference - the economic imperative for sophisticated context engineering becomes undeniable.

This economic reality intersects with technical challenges outlined by LangChain’s comprehensive analysis of context engineering patterns. They identify four core strategies that have emerged across successful agent implementations: write (saving context outside the context window), select (pulling relevant context in), compress (retaining only essential tokens), and isolate (splitting context across multiple agents or environments).

The technical infrastructure supporting these strategies has evolved rapidly. Key-value caching mechanisms in transformer architectures provide dramatic inference speedups - with implementations like nanoVLM achieving 38% performance improvements through optimized caching strategies. Advanced prefix caching systems in frameworks like vLLM enable sophisticated cache reuse patterns that fundamentally change how we architect agent interactions.

The emergence of standardized protocols like the Model Context Protocol (MCP) signals the maturation of this field, providing “USB-C for AI applications” by standardizing how models connect to data sources and tools. This standardization enables more sophisticated context engineering patterns while maintaining interoperability across different systems and providers.

Modern context engineering also intersects with cutting-edge capabilities like function calling and structured outputs, enabling agents to maintain structured state and execute complex workflows. Frameworks like LangChain’s agent architectures and Anthropic’s computer use capabilities demonstrate how context engineering principles scale to multimodal interactions and complex tool usage patterns.

Yet challenges remain. Drew Breunig’s analysis identifies critical failure modes including context poisoning (when hallucinations infiltrate context), context distraction (when context overwhelms training), context confusion (when superfluous context influences responses), and context clash (when parts of context disagree). Understanding and mitigating these failure modes has become essential for building reliable production systems.

As we stand at the intersection of rapid technological advancement and practical implementation challenges, context engineering emerges not just as a technical discipline, but as a fundamental craft that bridges the gap between raw AI capability and reliable, economical, and effective AI systems. This comprehensive exploration will take you through the essential patterns, proven strategies, and emerging techniques that define the state of the art in context engineering - from the mathematical foundations of KV-caching to the practical realities of multi-agent coordination and beyond.

Lessons from the Trenches: Manus AI’s Context Engineering Mastery

The field of context engineering owes a tremendous debt to practitioners who have openly shared their hard-won insights from building production AI systems. Few contributions have been as illuminating as the comprehensive analysis shared by Yichao ‘Peak’ Ji and the team at Manus AI. Their transparency in documenting both successes and failures through “four complete rewrites” of their agent framework provides invaluable lessons for anyone building AI agents at scale.

What sets the Manus analysis apart is its unflinching focus on the economic realities of production systems. Having processed millions of user interactions, they’ve distilled their learnings into six core principles that represent some of the most actionable context engineering guidance available today. These principles, born from what they affectionately call “Stochastic Gradient Descent” - the iterative process of architecture searching, prompt fiddling, and empirical refinement - offer a roadmap for building agents that are not just functional, but economically viable and performant at scale.

The Six Pillars of Production Context Engineering

1. Design Around the KV-Cache

The cornerstone principle that emerges from Manus’s experience is treating KV-cache optimization as the primary design constraint. This isn’t merely a performance consideration - it’s an economic imperative. Their key insight is that the KV-cache hit rate directly impacts both latency and cost, with cached tokens costing 10x less than uncached ones. The practical implementation requires maintaining stable prompt prefixes, implementing append-only context patterns, and ensuring deterministic serialization. As they discovered, even seemingly innocuous elements like timestamps can devastate cache performance, making this principle foundational to any serious production deployment.

2. Mask, Don’t Remove

One of the most counterintuitive insights from their analysis challenges the conventional wisdom of dynamic tool management. Rather than adding or removing tools mid-conversation - which invalidates the KV-cache - Manus advocates for maintaining complete tool definitions while using response prefilling and logit masking to constrain action selection. This approach preserves cache efficiency while still enabling context-aware tool usage. Their implementation using consistent tool naming conventions (like browser_ and shell_ prefixes) demonstrates how thoughtful design choices enable sophisticated control without sacrificing performance.

3. Use the File System as Context

Perhaps the most elegant solution to context window limitations comes from treating the file system as externalized memory. Rather than implementing aggressive compression strategies that risk information loss, Manus treats the file system as unlimited, persistent context that agents can directly manipulate. This approach acknowledges a fundamental truth: agents inherently need access to all prior state, and predicting which observations might become critical steps later is impossible. Their compression strategies are designed to be restorable - web page content can be dropped as long as URLs are preserved, document contents can be omitted if file paths remain accessible.

4. Manipulate Attention Through Recitation

The challenge of maintaining goal alignment across long agent trajectories finds an elegant solution in what Manus calls “recitation.” By constantly rewriting and updating todo lists, agents effectively push their global objectives into recent attention spans, avoiding “lost-in-the-middle” issues. This technique represents a sophisticated understanding of transformer attention patterns, using natural language to bias model focus toward task objectives without requiring architectural modifications. The ubiquitous todo.md files in Manus interactions aren’t mere organizational tools - they’re attention engineering mechanisms.

5. Keep the Wrong Stuff In

One of the most profound insights challenges the instinct to hide failures from AI systems. Manus’s experience demonstrates that preserving error traces and failed actions in context enables implicit belief updating, reducing the likelihood of repeated mistakes. This principle recognizes that error recovery represents one of the clearest indicators of true agentic behavior, yet remains underrepresented in academic benchmarks that focus on success under ideal conditions. The practical implementation requires resisting the urge to clean up traces, retry actions, or reset model state when failures occur.

6. Don’t Get Few-Shotted

The final principle addresses a subtle but critical failure mode in long-running agents. The natural tendency of language models to imitate patterns in context can lead to rigid, repetitive behavior when contexts contain uniform examples. Manus addresses this by introducing structured variation in actions and observations - different serialization templates, alternate phrasing, and controlled randomness in formatting. This approach prevents agents from falling into behavioral ruts while maintaining the benefits of contextual learning.

The depth of practical wisdom embedded in these principles reflects the kind of institutional knowledge that typically remains locked within successful organizations. By sharing their framework openly, the Manus team has provided the AI community with a invaluable reference for building production-grade context engineering systems. Their willingness to document not just what works, but why it works and how they discovered it, represents the kind of scientific transparency that accelerates progress across the entire field.

These principles will serve as our foundation as we explore the broader landscape of context engineering techniques, implementation strategies, and emerging patterns that define the state of the art in 2025.

The Context-Aware State Machine: Elegant Guardrails Without Cache Invalidation

Among the sophisticated techniques that have emerged from production AI systems, few are as elegant or technically impressive as Manus’s context-aware state machine. This approach represents a masterful solution to one of the most challenging problems in agent design: how do you guide an agent through a logical, multi-step process without breaking the performance benefits of the KV-cache?

The context-aware state machine exemplifies the kind of sophisticated engineering that emerges when teams push beyond simple prompt engineering into systematic context architecture. It’s a technique that perfectly embodies the principle of “mask, don’t remove” while solving real business logic requirements that plague production agent systems.

The Problem: Agents Need Guardrails

Consider building a travel booking agent where a user’s goal (“Book me a trip to Hawaii”) requires a strict sequence of actions:

  1. First, you MUST search_for_flights
  2. Only AFTER you have a flight, can you book_hotel
  3. Only AFTER you have both flight and hotel, can you add_rental_car

This sequence isn’t arbitrary - it reflects real business logic. You can’t book a hotel without knowing your travel dates from the flight. You can’t add a rental car without knowing the hotel location for pickup logistics.

An unguided LLM, however, might get confused by this multi-step process. After successfully booking a flight, it might:

  • Decide to search_for_flights again (redundant and potentially confusing)
  • Try to add_rental_car before booking a hotel (violating business logic)
  • Get stuck in analysis paralysis, uncertain which tool to use next

This isn’t a flaw in the model - it’s an inherent challenge of using general-purpose reasoning systems for domain-specific workflows that have rigid sequential requirements.

The Naive (and Destructive) Solution: Dynamic Prompting

The intuitive approach involves modifying the prompt at each step to show only relevant tools:

Step 1: Present only the search_for_flights tool

Tool Definitions: [search_for_flights]

Step 2: After flight booking, show hotel and car tools

Tool Definitions: [book_hotel, add_rental_car]

Step 3: After hotel booking, show only car tool

Tool Definitions: [add_rental_car]

Why This Approach is Catastrophic: Every prompt modification invalidates the KV-cache completely. The model must re-process the entire conversation history from scratch at each step. For a conversation with substantial history, this approach transforms a fast, cached operation into a slow, expensive recomputation. You’ve solved the business logic problem while destroying the economic viability of your system.

The Manus Solution: Separating Logic from Reasoning

The context-aware state machine is brilliant because it separates business logic from LLM reasoning. The LLM always sees the same stable prompt, but its output is intelligently constrained by an external process that operates during the model’s token generation phase.

This separation enables sophisticated behavioral control without sacrificing cache performance - the holy grail of production agent engineering.

Architecture Deep Dive: The Three-Layer System

Layer 1: The State Machine (Business Logic)

The state machine exists entirely outside the LLM as application code. It defines valid transitions and constraints based on conversation state:

class TravelBookingStateMachine:
    def __init__(self):
        self.state = "AwaitingFlight"
    
    def get_allowed_tools(self):
        if self.state == "AwaitingFlight":
            return ["search_for_flights"]
        elif self.state == "AwaitingHotel":
            return ["book_hotel", "add_rental_car"]
        elif self.state == "AwaitingCar":
            return ["add_rental_car"]
        elif self.state == "Complete":
            return ["get_weather", "modify_booking"]
    
    def transition(self, last_observation):
        if "flight booked successfully" in last_observation:
            self.state = "AwaitingHotel"
        elif "hotel booked successfully" in last_observation:
            self.state = "AwaitingCar"
        elif "rental car added" in last_observation:
            self.state = "Complete"

Key Insight: The state machine makes transitions based on observations (results from tool calls), not on the LLM’s internal reasoning. This creates a robust system that responds to actual progress rather than the model’s intentions.

Layer 2: The Stable Prompt (Unchanging Context)

The prompt sent to the LLM remains identical across all conversation turns:

// --- STATIC PREFIX (NEVER CHANGES) ---
System Prompt: You are a travel booking agent...
Tool Definitions: [
  search_for_flights,
  book_hotel,
  add_rental_car,
  get_weather,
  modify_booking,
  // ... ALL tools are always present
]

// --- DYNAMIC SUFFIX (GROWS OVER TIME) ---
Conversation History:
User: Book me a trip to Hawaii.
Agent: I'll help you book your Hawaii trip...
[Previous interactions...]
User: [Current request]

Critical Detail: All tools are always visible to the LLM. The model’s reasoning process has access to complete tool information, enabling it to understand the full context of available actions while being constrained only during output generation.

Layer 3: Logit Masking (The Enforcement Mechanism)

The technical magic happens during the model’s token generation process. When an LLM generates text, it assigns a probability score (logit) to every possible next token. Higher scores mean higher probability of selection.

The context-aware state machine intercepts this process:

def apply_state_constraints(logits, current_state, state_machine):
    allowed_tools = state_machine.get_allowed_tools()
    
    # Find logits corresponding to tool names
    for tool_name in ALL_TOOLS:
        if tool_name not in allowed_tools:
            # Set forbidden tool logits to negative infinity
            tool_token_ids = tokenizer.encode(tool_name)
            for token_id in tool_token_ids:
                logits[token_id] = float('-inf')
    
    return logits

The Enforcement Process:

  1. LLM processes the prompt and generates reasoning
  2. LLM begins generating tool call tokens
  3. State machine checks current state and identifies forbidden tools
  4. Forbidden tool tokens get logit values of negative infinity
  5. LLM cannot physically generate forbidden tool names
  6. LLM is forced to select from valid options

Detailed Example: The System in Action

Let’s trace through a complete interaction to see how the layers work together:

User Input: “Book me a trip to Hawaii”

State Machine Status: AwaitingFlight Allowed Tools: [search_for_flights]

LLM Processing:

  • Receives stable prompt with all tool definitions
  • Reasons: “I need to help book a Hawaii trip. First, I should search for flights.”
  • Begins generating: {"tool": "search_for_flights"...}

Logit Masking:

  • State machine sees search_for_flights is allowed
  • No masking applied
  • LLM successfully outputs tool call

Tool Execution:

  • search_for_flights("Hawaii") executes
  • Returns: “Found flights: LAX→HNL $450, departure Dec 15, return Dec 22”

State Transition:

  • Observation contains flight booking success
  • State machine transitions to AwaitingHotel

Turn 2: The Critical Test

State Machine Status: AwaitingHotel Allowed Tools: [book_hotel, add_rental_car]

LLM Processing:

  • Sees flight details in context
  • Might reason: “I have the flight. Now I should book a hotel.”
  • But could also think: “Maybe I should search for better flights first.”
  • Begins generating: {"tool": "search_for_flights"...}

Logit Masking (The Magic Moment):

  1. State machine detects current state is AwaitingHotel
  2. Identifies that search_for_flights is forbidden
  3. Intervenes during token generation
  4. Sets logit for “search_for_flights” tokens to -inf
  5. Leaves book_hotel and add_rental_car logits untouched

Result:

  • LLM cannot generate “search_for_flights” (probability = 0)
  • Forced to choose from valid options
  • Outputs: {"tool": "book_hotel"...} instead

The Beauty: The LLM’s reasoning process was allowed to run naturally, but its output was constrained to valid actions. No cache invalidation occurred, and business logic was enforced reliably.

Technical Implementation Considerations

Tokenization Complexity

Real implementations must handle tokenization nuances:

def mask_tool_tokens(logits, forbidden_tools, tokenizer):
    for tool_name in forbidden_tools:
        # Handle different tokenization patterns
        variants = [
            tool_name,                    # "search_for_flights"
            f'"{tool_name}"',            # "search_for_flights"  
            f"'{tool_name}'",            # 'search_for_flights'
            tool_name.replace("_", " "),  # "search for flights"
        ]
        
        for variant in variants:
            token_ids = tokenizer.encode(variant)
            for token_id in token_ids:
                logits[token_id] = float('-inf')

Partial Token Masking

Advanced implementations handle cases where tool names span multiple tokens:

def apply_progressive_masking(logits, forbidden_prefixes):
    # Mask tokens that could start forbidden tool names
    for prefix in forbidden_prefixes:
        prefix_tokens = tokenizer.encode(prefix)
        if len(prefix_tokens) > 0:
            # Mask the first token of forbidden prefixes
            logits[prefix_tokens[0]] = float('-inf')

State Transition Robustness

Production systems implement fuzzy matching for state transitions:

def detect_transition(observation, expected_patterns):
    success_indicators = [
        "successfully booked",
        "booking confirmed", 
        "reservation complete"
    ]
    
    return any(pattern in observation.lower() 
              for pattern in success_indicators)

Advantages and Trade-offs

Advantages

Performance: Perfect KV-cache preservation through stable prompts Reliability: Business logic enforcement is guaranteed, not probabilistic Debugging: Clear separation between reasoning (LLM) and rules (state machine) Flexibility: State machine can be modified without prompt engineering Scalability: Works with arbitrary numbers of tools and states

Trade-offs

Implementation Complexity: Requires sophisticated logit manipulation during inference Model Dependency: Relies on specific tokenization and generation patterns State Management: Requires careful design of state transitions and error handling Debugging Overhead: Failures can occur in multiple layers (reasoning, masking, transitions)

Integration with Broader Context Engineering

The context-aware state machine exemplifies several broader context engineering principles:

Manus Principle Alignment: Perfectly implements “mask, don’t remove” at the token level Cache Optimization: Maintains stable prefixes while enabling sophisticated control External Memory: State machine serves as external business logic memory Error Handling: Can incorporate failure modes into state transitions

Framework Integration: The technique complements LangChain’s “isolate” strategy by isolating business logic from reasoning while maintaining unified context.

The Future of Guided Generation

The context-aware state machine represents an early example of what might become a broader trend: guided generation systems that maintain LLM reasoning flexibility while enforcing domain-specific constraints. As this approach matures, we can expect:

  • Standardized frameworks for logit masking and state management
  • Integration with model serving platforms for built-in constraint enforcement
  • Extension to more complex constraints (semantic rules, multi-agent coordination)
  • Hybrid approaches combining multiple guidance mechanisms

The elegance of the context-aware state machine lies in its recognition that control and flexibility are not mutually exclusive. By operating at the right level of abstraction - constraining outputs rather than inputs - it enables sophisticated agent behaviors while preserving the economic and performance benefits that make production AI systems viable.

This technique represents context engineering at its finest: a sophisticated solution that emerges from deep understanding of transformer architectures, business requirements, and production constraints, all synthesized into an approach that is both theoretically sound and practically effective.

Response Prefill: Say No to Stateful Logit Processors

While the context-aware state machine demonstrates the power of logit masking for behavioral control, the Manus team discovered an even more elegant approach: response prefill. This technique achieves the same behavioral constraints as complex logit processors while being simpler, more stable, and more performant. It represents a masterful example of how thoughtful API design can eliminate entire categories of complexity.

Response prefill embodies a fundamental principle of robust engineering: constraints applied before computation are more reliable than constraints applied during computation. Rather than intercepting and modifying the model’s generation process in real-time, response prefill shapes the model’s behavior by controlling how it begins its response.

The Core Concept: Writing the Opening Lines

At its essence, response prefill is a technique where you, the developer, write the beginning of the AI’s response before the model starts generating. You force the model to start its answer with a specific sequence of words or tokens, fundamentally constraining the space of possible outputs.

The Writer Analogy:

  • Unconstrained: “Write a story” → The writer could start anywhere
  • Prefilled: “Write a story that begins with ‘It was a dark and stormy night’” → The writer must continue from that exact point

In LLM Context:

  • Unconstrained: <|im_start|>assistant → Model has complete freedom
  • Prefilled: <|im_start|>assistant<tool_call>{"name": "browser_ → Model must complete this specific pattern

This seemingly simple constraint is extraordinarily powerful because specific token sequences trigger specific behaviors in language models. The model’s training has created strong statistical associations between certain patterns and expected continuations, which we can leverage for precise behavioral control.

The Three Modes of Function Calling

The true elegance of response prefill becomes apparent when examining how it enables different modes of function calling through carefully crafted prefixes. Using the Hermes conversation format, we can demonstrate three distinct behavioral modes:

Mode 1: Auto Mode (Complete Freedom)

Prefill Pattern: <|im_start|>assistant

Semantic Meaning: “It’s your turn to speak. You have complete autonomy to either generate a direct text response or call a tool based on your judgment.”

Use Case: Default mode for general conversation where the model should decide independently whether tools are needed.

Example Behaviors:

  • User: “Hello, how are you?” → Model generates direct text response
  • User: “What’s the weather in London?” → Model chooses to call search_web tool
  • User: “Explain quantum computing” → Model may respond directly or search for current information

Implementation:

# Standard assistant turn - model decides approach
prefill = "<|im_start|>assistant"
prompt = system_prompt + conversation_history + prefill

Mode 2: Required Mode (Forced Tool Usage)

Prefill Pattern: <|im_start|>assistant<tool_call>

Semantic Meaning: “It’s your turn, and you must call a tool. You are forbidden from generating direct text responses. I don’t care which tool you select, but tool usage is mandatory.”

Use Case: When the agent’s state machine dictates that action is required, regardless of the specific tool needed.

Example Scenarios:

  • State: MustGatherInformation after user provides complex query
  • State: RequiresVerification after making factual claims
  • State: ActionRequired in goal-oriented workflows

Implementation:

# Force tool usage - model must select some tool
prefill = "<|im_start|>assistant<tool_call>"
prompt = system_prompt + conversation_history + prefill
# Model will generate: {"name": "some_tool", "parameters": {...}}

Mode 3: Specified Mode (Categorical Constraint)

Prefill Pattern: <|im_start|>assistant<tool_call>{"name": "browser_

Semantic Meaning: “It’s your turn. You must call a tool. And the tool name must start with the exact prefix ‘browser_’.”

Mechanical Constraint: The model must complete the JSON string. Its vocabulary of possible next tokens becomes dramatically constrained - it cannot generate search_web or file_read, only tokens that complete the prefix pattern.

The Manus Innovation: Clever tool naming conventions with consistent prefixes (browser_, shell_, file_) enable precise categorical control through simple string prefixes.

Example Constraint Resolution:

# Prefill forces browser_ prefix
prefill = '<|im_start|>assistant<tool_call>{"name": "browser_'

# Model's possible completions:
# ✓ "open_url"     → browser_open_url
# ✓ "scroll_down"  → browser_scroll_down  
# ✓ "click_element" → browser_click_element
# ✗ "search_web"   → Invalid (doesn't complete the prefix)
# ✗ "read_file"    → Invalid (doesn't complete the prefix)

The Manus Tool Architecture: Prefixes as Categories

The true genius of the Manus approach lies in their systematic tool naming convention that makes response prefill extraordinarily powerful:

Browser Tools:

  • browser_open_url
  • browser_scroll_down
  • browser_click_element
  • browser_extract_text

Shell Tools:

  • shell_execute_command
  • shell_change_directory
  • shell_list_files
  • shell_kill_process

File Tools:

  • file_read_content
  • file_write_content
  • file_append_content
  • file_delete

This naming convention transforms response prefill from a simple constraint mechanism into a sophisticated categorical control system. State machines can enforce tool categories without complex logic:

def get_prefill_for_state(current_state):
    state_prefills = {
        "web_browsing": '<|im_start|>assistant<tool_call>{"name": "browser_',
        "system_admin": '<|im_start|>assistant<tool_call>{"name": "shell_',
        "file_management": '<|im_start|>assistant<tool_call>{"name": "file_',
        "open_ended": '<|im_start|>assistant'  # Auto mode
    }
    return state_prefills[current_state]

Why “Stateless” Matters: The Complexity Elimination

The subtitle “Say No to Stateful Logit Processors” captures a profound architectural insight about reliability and maintainability in production systems.

The Stateful Approach (Complex and Fragile)

Traditional logit masking requires a stateful logit processor - a complex component that:

  1. Maintains Internal State: Tracks current agent state, allowed tools, forbidden tokens
  2. Real-time Inspection: Examines every token the model considers generating
  3. Dynamic Intervention: Modifies probability distributions during generation
  4. Synchronization: Coordinates between agent state and generation process

Implementation Complexity:

class StatefulLogitProcessor:
    def __init__(self):
        self.current_state = None
        self.allowed_tools = []
        self.forbidden_tokens = set()
    
    def update_state(self, new_state):
        self.current_state = new_state
        self.allowed_tools = get_tools_for_state(new_state)
        self.forbidden_tokens = compute_forbidden_tokens(self.allowed_tools)
    
    def process_logits(self, input_ids, logits):
        # Complex real-time masking logic
        for token_id in self.forbidden_tokens:
            logits[0, token_id] = float('-inf')
        return logits

Failure Modes:

  • State Desynchronization: Agent state and processor state drift apart
  • Performance Overhead: Token-level processing adds latency
  • Complex Debugging: Failures can occur in multiple interacting components
  • Race Conditions: Concurrent state updates during generation

The Stateless Approach (Simple and Robust)

Response prefill is stateless - it requires no persistent component monitoring generation:

  1. Single Decision Point: Determine appropriate prefill based on current state
  2. Prompt Construction: Build complete prompt with prefill included
  3. Standard Generation: Send prompt to model using normal inference
  4. Natural Constraint: Model behavior is constrained by prefill tokens

Implementation Simplicity:

def generate_response(agent_state, conversation_history):
    # Single decision - no persistent state required
    prefill = get_prefill_for_state(agent_state)
    
    # Standard prompt construction
    prompt = system_prompt + conversation_history + prefill
    
    # Normal inference - no special processors
    response = llm.generate(prompt)
    return response

Advantages:

  • Simplicity: Just string concatenation - no complex state management
  • Stability: Fewer moving parts means fewer failure modes
  • Performance: No per-token processing overhead
  • Debuggability: Easy to inspect and understand prompt construction
  • Determinism: Identical states produce identical prefills reliably

Advanced Prefill Patterns

While categorical tool selection represents the most common use case, response prefill enables sophisticated control patterns:

Hierarchical Constraints

# Force specific tool with parameter constraints
prefill = '<|im_start|>assistant<tool_call>{"name": "browser_open_url", "parameters": {"url": "'

# Model must complete with valid URL
# ✓ "https://example.com"
# ✗ "invalid-url" (training bias toward valid URLs)

Multi-Step Forcing

# Force specific reasoning pattern
prefill = '<|im_start|>assistant\nThought: I need to search for information about'

# Model must complete thought before action
# Enforces ReAct-style reasoning structure

Format Enforcement

# Force structured output format
prefill = '<|im_start|>assistant\n```json\n{'

# Model must generate valid JSON response
# Useful for structured data extraction

Integration with State Machines

Response prefill integrates seamlessly with the context-aware state machine pattern, providing a simpler implementation alternative:

class StateMachineWithPrefill:
    def __init__(self):
        self.state = "initial"
    
    def transition(self, observation):
        # Standard state transition logic
        if "flight booked" in observation:
            self.state = "awaiting_hotel"
        elif "hotel booked" in observation:
            self.state = "awaiting_car"
    
    def get_prefill(self):
        # Simple mapping - no complex logic
        prefills = {
            "initial": '<|im_start|>assistant<tool_call>{"name": "search_',
            "awaiting_hotel": '<|im_start|>assistant<tool_call>{"name": "hotel_',
            "awaiting_car": '<|im_start|>assistant<tool_call>{"name": "car_',
            "complete": '<|im_start|>assistant'
        }
        return prefills[self.state]

Performance and Reliability Benefits

The response prefill approach delivers measurable advantages in production systems:

Performance Gains:

  • Reduced Latency: No per-token processing overhead
  • Better Caching: Prefills can be cached and reused across similar states
  • Simpler Infrastructure: No need for custom logit processor deployment

Reliability Improvements:

  • Fewer Failure Modes: Stateless operation eliminates synchronization bugs
  • Easier Testing: Deterministic prefill generation enables comprehensive testing
  • Cleaner Debugging: Prompt inspection reveals exact constraints applied

Operational Simplicity:

  • Reduced Complexity: Teams need to understand string manipulation, not logit processing
  • Standard Deployment: Works with any LLM API supporting prefills
  • Clear Monitoring: Request/response logging captures complete constraint information

The Design Philosophy: Constraint Through Structure

Response prefill exemplifies a broader design philosophy in context engineering: achieving behavioral control through structural constraints rather than runtime intervention. This approach aligns with several fundamental engineering principles:

Fail-Fast Design: Constraints are applied before computation begins, making failures immediate and obvious rather than subtle and delayed.

Separation of Concerns: Business logic (state transitions) remains separate from generation mechanics (prefill construction), improving maintainability.

Principle of Least Surprise: The system behaves predictably because constraints are visible in the prompt rather than hidden in processing logic.

Composability: Prefill patterns can be combined and extended without complex interaction effects.

The Broader Context Engineering Lesson

The evolution from complex logit masking to elegant response prefill demonstrates a crucial insight for context engineering practitioners: the most sophisticated solution is often the simplest one that works reliably. The Manus team’s journey from stateful processors to stateless prefills represents the kind of architectural refinement that emerges from production experience and deep system understanding.

This technique showcases how thoughtful API design can eliminate entire categories of complexity. By leveraging the model’s natural completion behavior and statistical biases learned during training, response prefill achieves precise control with minimal machinery.

The combination of systematic tool naming conventions and response prefill creates a powerful, context-aware state machine that is simultaneously sophisticated in capability and elegant in implementation - a hallmark of mature context engineering practice.

Memory Systems: The Foundation of Persistent Context

While the previous techniques focus on optimizing context within individual conversations, the true power of context engineering emerges when we consider memory across interactions. Memory systems represent the bridge between ephemeral context windows and persistent intelligence, enabling agents to learn, adapt, and improve over time. This capability transforms agents from stateless responders into truly intelligent systems that accumulate wisdom through experience.

The challenge of memory in AI systems reflects a fundamental limitation of transformer architectures: models are inherently stateless. Each conversation begins fresh, with no recollection of previous interactions or learned insights. While this simplicity enables parallelization and reproducible behavior, it severely limits an agent’s ability to provide personalized, contextually-aware responses that improve over time.

Modern memory systems address this limitation by implementing sophisticated storage and retrieval mechanisms that extend an agent’s effective context far beyond the constraints of individual prompts. As LangMem’s comprehensive framework demonstrates, effective memory systems can be organized around three fundamental types that mirror human cognitive architecture: semantic, episodic, and procedural memory.

Understanding the Memory Architecture

Before diving into specific memory types, it’s crucial to understand how memory systems integrate with the context engineering patterns we’ve explored. Memory operates at a different timescale than the KV-cache optimizations and state machines discussed earlier:

Context Window (Immediate): Handles current conversation state, tool definitions, and recent history Memory Systems (Persistent): Stores knowledge, experiences, and behavioral patterns across conversations Integration Layer: Decides what memories to retrieve and inject into current context

This multi-layered approach enables sophisticated agent behaviors while maintaining the performance benefits of cache optimization. The memory system serves as an external knowledge store that supplements, rather than replaces, carefully engineered context windows.

Type 1: Semantic Memory - Facts and Knowledge

Definition: Semantic memory stores essential facts, user preferences, and domain knowledge that ground an agent’s responses. This type of memory captures “what” the agent knows about the world and the user.

Core Principle: Unlike episodic memory which preserves specific interactions, semantic memory distills conversations into reusable knowledge that applies across different contexts.

Two Implementation Patterns:

Collections: Unbounded Knowledge Storage

Collections represent the most common implementation of semantic memory, storing individual facts as searchable documents. This approach excels when tracking diverse, evolving knowledge across many interactions.

Example Implementation:

# User preference extraction
semantic_memories = [
    {
        "content": "User prefers concise explanations over detailed ones",
        "source": "conversation_2023_12_15",
        "strength": 0.85,
        "last_accessed": "2023_12_20"
    },
    {
        "content": "User is a Python developer working on machine learning projects",
        "source": "conversation_2023_12_10", 
        "strength": 0.92,
        "last_accessed": "2023_12_20"
    },
    {
        "content": "User dislikes overly technical jargon in explanations",
        "source": "conversation_2023_12_18",
        "strength": 0.78,
        "last_accessed": "2023_12_19"
    }
]

Key Challenge: Collection-based semantic memory requires sophisticated consolidation mechanisms. When new information conflicts with existing memories, the system must decide whether to update, merge, or invalidate previous knowledge. LangMem addresses this through memory enrichment processes that balance precision (avoiding over-extraction) with recall (avoiding under-extraction).

Retrieval Strategy: Collections use hybrid retrieval combining semantic similarity, memory importance, and recency/frequency of access. This ensures that relevant, high-quality memories surface in appropriate contexts.

Profiles: Structured State Management

Profiles represent a more constrained approach to semantic memory, maintaining a single document that captures current state rather than accumulating multiple records.

Example Implementation:

user_profile = {
    "name": "Alex Chen",
    "role": "Senior ML Engineer",
    "preferred_communication_style": "concise and technical",
    "current_projects": ["LLM fine-tuning", "RAG optimization"],
    "learning_goals": ["context engineering", "production ML systems"],
    "response_preferences": {
        "code_examples": True,
        "step_by_step_explanations": False,
        "academic_references": True
    },
    "last_updated": "2023-12-20"
}

Key Advantage: Profiles provide immediate access to current state without complex search operations. They’re ideal for user preferences, configuration settings, and task-specific information that follows predictable schemas.

Update Strategy: When new information arrives, profiles update existing fields rather than creating new records, ensuring the system always reflects the latest understanding of user preferences or system state.

Type 2: Episodic Memory - Past Experiences

Definition: Episodic memory preserves successful interactions as learning examples that guide future behavior. Unlike semantic memory which stores facts, episodic memory captures the full context of an interaction - the situation, thought process, and outcomes.

Core Principle: Episodes serve as few-shot examples that help agents learn from experience, adapting responses based on what has worked before in similar situations.

Implementation Characteristics:

  • Context Preservation: Episodes maintain the full interaction context, including user intent, agent reasoning, and outcomes
  • Success Criteria: Only successful interactions become episodes, creating a curated set of positive examples
  • Situational Matching: Episodes are retrieved based on similarity to current situations rather than semantic content

Example Implementation:

successful_episode = {
    "situation": {
        "user_intent": "debug Python code with memory leak",
        "user_expertise": "intermediate",
        "context": "production environment issue"
    },
    "agent_approach": {
        "initial_response": "Asked for specific error symptoms and environment details",
        "tool_sequence": ["code_analysis", "memory_profiler", "optimization_suggestions"],
        "reasoning": "Gathered context before proposing solutions to avoid generic advice"
    },
    "outcome": {
        "user_satisfaction": "high",
        "problem_resolved": True,
        "follow_up_questions": 0
    },
    "success_indicators": [
        "User explicitly thanked agent",
        "No clarification requests needed",
        "Solution implemented successfully"
    ],
    "extracted_pattern": "For debugging issues, gather specific context before proposing solutions"
}

Retrieval and Application: When faced with similar debugging requests, the agent can retrieve this episode as a few-shot example, applying the successful pattern of gathering context before proposing solutions.

Memory Formation: Episodic memories typically form through post-conversation analysis, where the system identifies interactions that resulted in positive outcomes and extracts the key patterns that led to success.

Type 3: Procedural Memory - System Instructions

Definition: Procedural memory encodes how an agent should behave and respond. It starts with system prompts that define core behavior, then evolves through feedback and experience to optimize agent performance.

Core Principle: Procedural memory captures “how” the agent should operate, encoding behavioral patterns, response styles, and interaction protocols that have proven effective.

Dynamic Evolution: Unlike static system prompts, procedural memory adapts based on user feedback and interaction outcomes, continuously refining agent behavior.

Example Implementation:

procedural_memory = {
    "core_personality": {
        "base_prompt": "You are a helpful AI assistant specializing in software engineering.",
        "refinements": [
            "Always ask clarifying questions for ambiguous requests",
            "Provide code examples when explaining concepts", 
            "Acknowledge uncertainty rather than guessing"
        ]
    },
    "interaction_patterns": {
        "greeting_style": "Professional but friendly",
        "error_handling": "Apologize briefly, then provide correction",
        "code_formatting": "Use proper syntax highlighting and comments"
    },
    "learned_optimizations": [
        {
            "context": "User asks for code review",
            "pattern": "Structure feedback as: strengths, areas for improvement, specific suggestions",
            "evidence": "85% positive feedback when this format used"
        }
    ]
}

Feedback Integration: Procedural memory improves through explicit user feedback and implicit signals (follow-up questions, satisfaction indicators, task completion rates). This enables continuous optimization of agent behavior.

Application in Context: Procedural memories integrate directly into system prompts and behavioral guidelines, ensuring that learned patterns immediately influence agent responses.

Memory Formation Strategies

The timing and method of memory formation significantly impacts system performance and user experience. LangMem identifies two primary approaches:

Conscious Formation (Hot Path)

Approach: Memories form during conversation, enabling immediate updates when critical context emerges.

Implementation:

# During conversation
user_message = "I prefer bullet points over paragraphs"
# Agent immediately updates semantic memory
update_user_preference("communication_style", "bullet_points")
# Memory available for same conversation

Advantages:

  • Immediate incorporation of new information
  • User can see their preferences being learned in real-time
  • No delay between learning and application

Trade-offs:

  • Adds latency to conversations
  • Increases complexity of agent decision-making
  • May interrupt conversation flow

Subconscious Formation (Background Processing)

Approach: Memories form between conversations through retrospective analysis, finding patterns without impacting real-time interactions.

Implementation:

# After conversation ends
async def background_memory_processing(conversation_id):
    conversation = await get_conversation(conversation_id)
    
    # Extract semantic memories
    user_preferences = extract_preferences(conversation)
    await update_semantic_memory(user_preferences)
    
    # Identify successful episodes
    if conversation.user_satisfaction > 0.8:
        episode = extract_episode(conversation)
        await store_episodic_memory(episode)
    
    # Optimize procedural patterns
    behavioral_insights = analyze_interaction_patterns(conversation)
    await update_procedural_memory(behavioral_insights)

Advantages:

  • Zero impact on conversation latency
  • Enables deeper analysis and pattern recognition
  • Reduces cognitive load on the agent during interactions

Trade-offs:

  • Delayed memory formation
  • New insights not available until next conversation
  • Requires background processing infrastructure

Integration with Context Engineering

Memory systems complement rather than replace the context engineering techniques we’ve explored:

KV-Cache Optimization: Memory retrieval can be cached using the same prefix-preservation strategies, with stable memory queries enabling cache hits across conversations.

State Machines: Memory systems can inform state transitions, with user preferences and past experiences influencing workflow decisions.

Response Prefill: Procedural memories can determine appropriate prefill patterns, with learned behavioral preferences guiding constraint application.

Example Integration:

class MemoryAwareAgent:
    def __init__(self):
        self.semantic_memory = SemanticMemoryStore()
        self.episodic_memory = EpisodicMemoryStore()
        self.procedural_memory = ProceduralMemoryStore()
    
    async def generate_response(self, user_input, conversation_context):
        # Retrieve relevant memories
        user_profile = await self.semantic_memory.get_profile(user_id)
        similar_episodes = await self.episodic_memory.search(user_input)
        behavioral_rules = await self.procedural_memory.get_rules()
        
        # Construct context with memory integration
        context = self.build_context(
            user_input=user_input,
            conversation_history=conversation_context,
            user_profile=user_profile,
            few_shot_examples=similar_episodes,
            system_instructions=behavioral_rules
        )
        
        # Apply state machine and prefill based on memories
        state = self.determine_state(context, user_profile)
        prefill = self.get_prefill(state, behavioral_rules)
        
        return await self.llm.generate(context + prefill)

The Memory-Context Engineering Synthesis

The integration of sophisticated memory systems with context engineering represents the next evolution in agent architecture. While individual techniques like KV-cache optimization and response prefill solve specific performance challenges, memory systems enable agents to transcend the limitations of stateless interactions.

This synthesis creates agents that:

  • Learn from Experience: Episodic memory enables pattern recognition and adaptation
  • Maintain Relationships: Semantic memory preserves user preferences and accumulated knowledge
  • Evolve Behavior: Procedural memory continuously optimizes interaction patterns
  • Scale Efficiently: Memory integration works within existing context engineering constraints

The result is a new class of AI systems that combine the performance benefits of carefully engineered contexts with the adaptive intelligence of persistent memory - creating agents that are not just stateless responders, but learning partners that improve through experience.

Foundational Research: The Academic Pillars of Context Engineering

While production insights from teams like Manus provide invaluable practical guidance, the theoretical foundations of context engineering rest on rigorous academic research that has systematically explored how language models process, utilize, and respond to contextual information. Three seminal papers have particularly shaped our understanding of these dynamics, each addressing fundamental questions about model behavior, reasoning capabilities, and learning mechanisms that directly inform modern context engineering practices.

Temperature and Creativity: Debunking the Randomness Myth

The paper “Is Temperature the Creativity Parameter of Large Language Models?” tackles one of the most persistent misconceptions in AI deployment: that increasing temperature directly enhances creativity. This research provides crucial insights for context engineers who must balance novelty with coherence in their prompt designs.

The Core Finding: Temperature Controls Randomness, Not Creativity

The authors demonstrate that temperature primarily adjusts the probability distribution of next-word prediction rather than fostering genuine creativity. While low temperatures produce deterministic, focused outputs that can become repetitive, high temperatures increase risk-taking behavior that often leads to nonsensical or irrelevant outputs. The fundamental tension lies in the creativity trade-off: higher temperatures generate more novel text but simultaneously decrease logical consistency and quality.

Implications for Context Engineering

This research reveals that there is no universal “creativity” setting - optimal temperature depends entirely on the specific task context. More critically, the paper supports a key tenet of context engineering: context structure is far more powerful than parameter tweaking. A well-crafted prompt can guide a low-temperature model toward novel, useful ideas, while a high-temperature model with poor context will likely produce noise.

This finding validates the Manus principle of maintaining stable, structured contexts rather than relying on parameter adjustments to achieve desired behavior. Context engineers must focus on information architecture rather than hoping temperature changes will solve prompt engineering challenges.

ReAct: The Foundation of Agentic Reasoning

The ReAct paper (Yao et al., 2022) introduced a paradigm that fundamentally changed how we think about agentic AI systems. By synergizing reasoning and acting, ReAct established the template for modern AI agents that can think, act, and adapt based on environmental feedback.

The Thought-Act-Observation Loop

ReAct’s core innovation lies in its structured approach to agent behavior:

  1. Thought: The model generates explicit reasoning about what needs to be done
  2. Act: Based on its reasoning, it executes a specific action
  3. Observation: The model receives and processes feedback from the action

This cycle repeats, enabling dynamic reasoning, information gathering, and plan adjustment based on real-world results.

Key Contributions to Context Engineering

The ReAct framework provides several foundational insights that directly inform modern context engineering practices:

Reasoning Improves Action: By requiring explicit thought generation, models formulate more precise and effective actions. The reasoning trace helps break down problems, identify missing information, and select appropriate tools - a principle that underlies sophisticated prompt structures in production systems.

Action Grounds Reasoning: Observations from actions provide external information that prevents hallucination and grounds the reasoning process in concrete results. This creates a feedback loop that validates and corrects model assumptions, a mechanism that Manus leverages in their “keep the wrong stuff in” principle.

Enhanced Interpretability: Explicit thought steps make agent behavior debuggable and trustworthy. This transparency is crucial for production systems where understanding agent decision-making is essential for reliability and user trust.

Robustness Through Error Handling: When actions fail or return unexpected results, the ReAct framework allows models to reason about failures and formulate recovery plans. This connects directly to Manus’s insight about preserving error traces in context - the model can learn from mistakes and adapt its approach.

Structured Prompting Power: ReAct demonstrates that carefully structured prompts encouraging specific formats (Thought:…, Action:…) can elicit dramatically more powerful behavior from language models. This insight forms the foundation of systematic context engineering approaches.

In-Context Learning: The Science of Demonstration-Driven Adaptation

The comprehensive “Survey on In-Context Learning” provides the theoretical framework for understanding how models adapt their behavior based on contextual demonstrations without parameter updates. This research directly explains why context engineering techniques work and reveals the limitations that practitioners must navigate.

The Emergent Nature of In-Context Learning

In-Context Learning (ICL) represents one of the most surprising capabilities of large language models - the ability to perform tasks by pattern recognition from examples provided in the prompt itself. The survey reveals that ICL is an emergent property of scale, appearing only in very large models and operating through implicit meta-learning acquired during pre-training on diverse datasets.

The Architecture of Effective Demonstrations

The research identifies critical factors that determine ICL effectiveness, providing scientific grounding for context engineering best practices:

Format Consistency: The structure of examples is crucial. Clear, consistent labeling (Input:/Output:, Q:/A:) helps models understand task structure, while inconsistent formatting creates confusion. This validates the importance of systematic prompt templates in production systems.

Strategic Selection: Example relevance dramatically impacts performance. Demonstrations should be contextually similar to target queries - a principle that underlies sophisticated example selection algorithms in modern RAG systems.

Order Effects and Recency Bias: The sequence of examples significantly influences model behavior, with recent examples carrying disproportionate weight. This finding provides theoretical justification for the Manus “recitation” technique, which strategically places important information at the end of contexts to maintain model focus.

Shot Count Optimization: While few-shot generally outperforms zero-shot, diminishing returns and potential confusion from too many examples create an optimization challenge. This connects to the Manus warning about “getting few-shotted” - too many similar examples can create rigid, repetitive behavior patterns.

Critical Limitations and Solutions

The survey identifies fundamental ICL constraints that directly motivated the techniques documented by Manus:

Context Window Limits: ICL is fundamentally bounded by context capacity, motivating externalized memory strategies like the Manus file system approach.

Computational Costs: Long demonstration-heavy prompts are expensive and slow, driving the economic imperative for KV-cache optimization strategies.

Sensitivity and Brittleness: ICL’s extreme sensitivity to prompt details necessitates robust context engineering methodologies that work reliably across diverse scenarios.

Lost in the Middle: Models’ tendency to focus on prompt beginnings and endings while ignoring middle content provides the theoretical foundation for attention manipulation techniques like recitation.

Bridging Theory and Practice

These three papers collectively provide the scientific foundation for understanding why context engineering works, what its limitations are, and how to overcome them systematically. The temperature research shows us that parameter tweaking cannot substitute for thoughtful context design. ReAct demonstrates how structured reasoning can create robust, interpretable agent behaviors. The ICL survey explains the fundamental mechanisms that make context-based adaptation possible while revealing the constraints that drive practical engineering solutions.

Together, they form a theoretical framework that validates and explains the practical wisdom embedded in production systems like those developed by Manus. Every context engineering principle can be traced back to insights from this foundational research, creating a coherent bridge between academic understanding and real-world implementation.

Understanding the Economics: Why KV-Cache Optimization Matters

Before diving deeper into the Manus principles, it’s essential to understand the fundamental economic and computational constraints that drive modern context engineering. The challenge begins with a basic reality: LLMs are expensive and stateless.

The Computational Reality

Every time you send a prompt to an LLM, it performs massive computation, calculating Key-Value pairs (the “KV” in KV-cache) for every token to understand relationships with every other token. This attention mechanism is what makes transformers powerful, but it’s also what makes them computationally intensive and expensive.

Critically, the model is stateless - it has no memory of previous interactions. To maintain a conversation, applications must re-send the entire conversation history with every new message. For long conversations, re-computing the entire history repeatedly is incredibly inefficient, like re-reading a book from page one every time you want to read the next page.

The KV-Cache Solution

The KV-cache serves as the model’s short-term memory for a single request. As it processes a prompt, it saves calculated Key-Value pairs. If the next prompt begins with an identical sequence, the model can load cached results and compute only the new portions. This “cache hit” is the key to making agents fast and affordable.

The Static Prefix/Dynamic Suffix Strategy

This architectural pattern is designed to guarantee cache hits on every conversational turn by maintaining strict separation between stable and variable content.

Static Prefix Structure:

  • System prompts that remain constant
  • Complete tool definitions (never modified mid-conversation)
  • Core instructions and formatting guidelines

Dynamic Suffix Structure:

  • Conversation history that grows with each turn
  • New user inputs and agent responses
  • Tool observations and results

A Detailed Example: Cache Optimization in Action

Turn 1: The Cold Start

User asks: “Find the capital of France.”

The application constructs:

// --- STATIC PREFIX ---
System Prompt: You are a helpful assistant.
Tool Definitions: [search, write_file, read_file]
Core Instructions: Think step-by-step...

// --- DYNAMIC SUFFIX ---
Conversation History:
User: Find the capital of France.

The model processes every token from scratch (cold start), caches all KV-pairs, and responds: Agent: Thought: I will use the search tool. Action: search("capital of France")

Turn 2: The Cache Hit

User asks: “What is its population?”

The application appends to create:

// --- STATIC PREFIX (IDENTICAL) ---
System Prompt: You are a helpful assistant.
Tool Definitions: [search, write_file, read_file]
Core Instructions: Think step-by-step...

// --- DYNAMIC SUFFIX (GROWN) ---
Conversation History:
User: Find the capital of France.
Agent: Thought: I will use the search tool. Action: search("capital of France")
Observation: Paris
User: What is its population?

The model recognizes the identical prefix, achieves a cache hit for everything through “Observation: Paris,” and only computes the new query. This transforms a potentially expensive operation into a fast, economical one.

Why “Mask, Don’t Remove” is Essential

Consider what happens if you attempt to optimize by providing only relevant tools in Turn 2:

// --- STATIC PREFIX (MODIFIED!) ---
System Prompt: You are a helpful assistant.
Tool Definitions: [search] // <-- REMOVED OTHER TOOLS!
Core Instructions: Think step-by-step...

This seemingly minor optimization destroys the cache. The model detects that Tool Definitions: [search] differs from the previous Tool Definitions: [search, write_file, read_file], resulting in a complete cache bust. The entire prompt must be recomputed from scratch, eliminating all performance gains.

This economic reality drives the Manus principle of maintaining complete tool definitions while handling logic through dynamic instructions or post-processing - preserving the cache while maintaining functionality.

Deep Dive: The Manus Context Engineering Framework

With the economic foundations established, we can now examine how the Manus team translated these constraints into a comprehensive context engineering framework. Their blog post represents a masterclass in practical AI engineering, moving beyond simple prompt engineering into a rigorous discipline that addresses the theoretical limitations identified in foundational research.

Each principle can be understood as a battle-tested solution to specific challenges in In-Context Learning, agentic frameworks like ReAct, and transformer behavior patterns.

Principle 1: Design Around the KV-Cache

Manus Implementation: Treating KV-cache hit rate as the primary performance metric, requiring stable prompt prefixes, append-only context patterns, and deterministic serialization.

Research Connection: This directly addresses the computational cost and latency limitations highlighted in the “Survey on In-Context Learning.” By ensuring most prompts are cached, the team minimizes new token processing, making agents economically viable and responsive.

Critical Insight: Even seemingly innocuous elements like precise timestamps can devastate cache performance. The principle requires treating cache optimization as a first-class design constraint, not an afterthought.

Example Implementation:

✓ Good: Stable timestamp format
✗ Bad: Precise second-level timestamps that change every turn
✓ Good: Deterministic JSON serialization
✗ Bad: Object serialization with unstable key ordering

Principle 2: Mask, Don’t Remove

Manus Implementation: Maintaining complete tool definitions while using response prefilling and logit masking to constrain action selection during decoding.

Research Connection: This preserves the format consistency identified as crucial in ICL research while maintaining cache efficiency. The stable tool list ensures identical prompt structure, leading to more reliable model behavior.

Advanced Technique: Using consistent tool naming conventions (browser_, shell_ prefixes) enables sophisticated logit masking that can constrain entire tool categories without modifying prompt structure.

Example Implementation:

# Instead of dynamic tool removal:
tools = get_relevant_tools(context)  # ✗ Cache-busting

# Use masking:
all_tools = get_all_tools()  # ✓ Cache-preserving
allowed_prefixes = ["search_", "file_"]
mask_tools_during_generation(all_tools, allowed_prefixes)

Principle 3: Use the File System as Context

Manus Implementation: Treating the file system as unlimited, persistent context that agents can directly manipulate, with compression strategies designed to be restorable.

Research Connection: This elegantly solves the primary ICL limitation of finite context windows while maintaining agent access to all prior state - a fundamental requirement for complex reasoning tasks.

Philosophical Insight: Rather than predicting which observations might become critical later (impossible), this approach acknowledges that agents inherently need access to complete state history.

Example Implementation:

# Instead of context bloat:
context += f"Webpage content: {10000_word_article}"  # ✗

# Use file system externalization:
save_file("webpage.txt", content)
context += f"Saved webpage to webpage.txt"  # ✓

Principle 4: Manipulate Attention Through Recitation

Manus Implementation: Constantly rewriting and updating todo lists to push global objectives into recent attention spans.

Research Connection: This exploits transformer attention patterns documented in ICL research, specifically recency bias and “lost-in-the-middle” problems. By keeping goals at the context end, attention remains focused on objectives.

Mechanical Understanding: The ubiquitous todo.md files aren’t organizational tools - they’re attention engineering mechanisms that use natural language to bias model focus without architectural modifications.

Example Implementation:

# After complex multi-step operations:
agent.action("read_file", "todo.md")
# Observation: "Main Goal: Write comprehensive AI report"
# This recitation refocuses attention on the primary objective

Principle 5: Keep the Wrong Stuff In

Manus Implementation: Preserving error traces and failed actions in context to enable implicit belief updating and reduce repeated mistakes.

Research Connection: This perfectly implements the ReAct framework’s Thought→Act→Observation loop, where errors are simply observations that inform subsequent reasoning. It also provides crucial negative examples for In-Context Learning.

Counterintuitive Wisdom: Resisting the instinct to clean up traces, retry actions, or reset model state when failures occur. Errors contain valuable learning signal.

Example Implementation:

# Wrong: Hiding failures
if action_failed:
    retry_without_trace()  # ✗

# Right: Learning from failures
context += f"Action: {failed_action}"
context += f"Observation: Error - {error_message}"
# Next thought can reason about the failure

Principle 6: Don’t Get Few-Shotted

Manus Implementation: Introducing structured variation in actions and observations through different serialization templates, alternate phrasing, and controlled randomness in formatting.

Research Connection: This addresses ICL sensitivity to demonstration distribution while offering a sophisticated alternative to temperature-based randomness. Instead of inducing incoherence through high temperature, this introduces controlled novelty in context structure.

Pattern Breaking: Preventing rigid behavioral loops by ensuring context diversity, particularly important in long-running agents that might fall into repetitive patterns.

Example Implementation:

# Avoid monotonous patterns:
templates = [
    "Action: read_file('{file}')",
    "Reading file: {file}",
    "File access: {file}"
]
# Randomly vary action descriptions while maintaining semantic meaning

The Framework’s Broader Impact

The Manus framework represents institutional knowledge that typically remains locked within successful organizations. Their transparency in documenting not just what works, but why it works and how they discovered it through “Stochastic Gradient Descent,” provides the AI community with actionable guidance for building production-grade systems.

Each principle addresses specific failure modes identified in academic research while providing practical implementation strategies. The framework bridges the gap between theoretical understanding and real-world deployment, offering a systematic approach to context engineering that balances performance, cost, and reliability.

LangChain’s Four-Strategy Framework

While Manus provides deep operational insights from building a specific agent platform, LangChain’s analysis offers a broader taxonomic view of context engineering patterns that have emerged across the entire agent ecosystem. Their comprehensive framework identifies four core strategies that successful agent implementations use to manage context: write, select, compress, and isolate.

This classification system provides a valuable lens for understanding how different teams have approached similar challenges, offering a structured way to think about context engineering trade-offs and design decisions.

Strategy 1: Write Context - External Memory Systems

Core Principle: Saving context outside the context window to help agents perform tasks effectively.

LangChain identifies two primary mechanisms for writing context:

Scratchpads serve as session-specific memory systems, implemented either as tool calls that write to files or as fields in runtime state objects. The key insight is that scratchpads enable agents to save useful information during task execution without overwhelming the main context window.

Memories extend beyond single sessions, creating persistent knowledge that accumulates across multiple interactions. Drawing inspiration from research like Reflexion and Generative Agents, this approach enables agents to learn from past experiences and build institutional knowledge over time.

Production Examples: Popular products like ChatGPT, Cursor, and Windsurf have all implemented sophisticated memory systems that auto-generate long-term memories from user-agent interactions, demonstrating the practical value of external memory architectures.

Strategy 2: Select Context - Intelligent Information Retrieval

Core Principle: Pulling relevant context into the context window to help agents perform tasks.

The selection strategy addresses the fundamental challenge of choosing which information to include from potentially vast external knowledge stores.

Scratchpad Selection can be handled through tool calls when implemented as external files, or through selective exposure of state fields when implemented as runtime objects. This provides fine-grained control over what context is available to the LLM at each turn.

Memory Selection becomes more complex with larger collections of facts and relationships. LangChain highlights the challenge of ensuring relevant memories are selected, noting both success stories (like ChatGPT’s user-specific memory retrieval) and failure cases (like unexpected location injection in image generation).

Tool Selection addresses agent overload from too many available tools. Recent research shows that applying RAG principles to tool descriptions can improve tool selection accuracy by 3x, suggesting that context engineering principles extend beyond text to tool management.

Knowledge Retrieval represents the most mature application of selection strategies, with sophisticated implementations in code agents that use combinations of grep/file search, knowledge graph retrieval, and re-ranking to handle large codebases effectively.

Strategy 3: Compress Context - Efficient Information Distillation

Core Principle: Retaining only the tokens required to perform a task effectively.

Context Summarization emerges as the most common compression technique, with implementations ranging from Claude Code’s automatic compaction when approaching context limits to strategic summarization at specific workflow points. The challenge lies in preserving critical information while reducing token count.

Context Trimming offers a more mechanical approach, using hard-coded heuristics or trained pruning models to filter context. LangChain notes emerging research in trained context pruners that can intelligently remove non-essential information while preserving task-relevant details.

Implementation Considerations: The compression strategy requires careful balance between information preservation and token efficiency. Too aggressive compression risks losing critical information, while insufficient compression fails to address the fundamental context limitations.

Strategy 4: Isolate Context - Distributed Processing Architectures

Core Principle: Splitting context across multiple agents or environments to enable parallel processing and specialized handling.

Multi-Agent Isolation represents the most visible form of context isolation, where teams of agents handle specific sub-tasks with their own context windows. Anthropic’s multi-agent researcher system demonstrates this approach’s power, showing how parallel agents with isolated contexts can outperform single-agent systems by enabling specialized focus on different aspects of complex tasks.

Environmental Isolation offers an alternative approach, using sandboxes or code execution environments to isolate token-heavy objects from the main context. HuggingFace’s deep researcher exemplifies this pattern, where code agents output tool calls that execute in isolated environments, keeping computation state separate from conversational context.

State-Based Isolation provides a lighter-weight alternative through careful runtime state design, where context can be written to specific fields but selectively exposed to the LLM based on current needs.

Framework Integration and Tooling

LangChain positions their analysis within their broader ecosystem of tools designed to support these patterns:

LangGraph provides infrastructure for both short-term (thread-scoped) and long-term memory, with flexible abstractions for implementing write and select strategies effectively.

LangMem offers specialized memory management abstractions that simplify implementation of sophisticated memory patterns.

LangGraph Bigtool addresses tool selection challenges through semantic search over tool descriptions, enabling effective management of large tool collections.

The framework emphasizes that successful context engineering requires understanding both the patterns and the tools available to implement them effectively.

Strategic Implications

LangChain’s four-strategy framework provides a valuable meta-perspective on context engineering, showing how the Manus principles fit within broader industry patterns. Their analysis reveals that successful teams consistently implement combinations of these strategies rather than relying on any single approach.

The framework also highlights an important insight: context engineering is fundamentally about information architecture. Whether writing external memories, selecting relevant information, compressing for efficiency, or isolating for specialization, the underlying challenge is designing systems that can effectively manage information flow between limited context windows and unlimited external information spaces.

Anthropic’s Guide to Building Effective Agents

While the previous frameworks focus specifically on context engineering, Anthropic’s comprehensive guide provides crucial architectural context for understanding how context engineering fits within broader agent design principles. Their analysis, drawn from working with dozens of teams across industries, offers a pragmatic perspective on when and how to implement sophisticated context management systems.

The Complexity Spectrum: When Context Engineering Matters

Anthropic’s fundamental insight is that simplicity should be the default, with complexity added only when demonstrably beneficial. This principle directly applies to context engineering: sophisticated context management systems should be implemented only when simpler approaches fail to meet requirements.

The Progression Path:

  1. Single LLM calls with retrieval and in-context examples (often sufficient)
  2. Structured workflows for predictable, decomposable tasks
  3. Autonomous agents for open-ended problems requiring dynamic decision-making

This progression helps context engineers understand when to invest in advanced techniques like KV-cache optimization, external memory systems, or multi-agent architectures.

Architectural Patterns and Context Implications

Anthropic identifies several core patterns that have different context engineering requirements:

The Augmented LLM Building Block

The foundation of all agentic systems is an LLM enhanced with retrieval, tools, and memory capabilities. This building block directly embodies the context engineering challenge: how to provide the model with the right information at the right time.

Context Engineering Relevance: This building block requires implementing the “select” strategy from LangChain’s framework, determining what tools, memories, and retrieved information to include in each LLM call.

Workflow Patterns with Distinct Context Needs

Prompt Chaining decomposes tasks into sequential steps, where each LLM call processes the previous output. This pattern has specific context engineering requirements around maintaining relevant information across the chain while avoiding context bloat.

Routing classifies inputs and directs them to specialized prompts. This pattern exemplifies the “isolate” strategy, where different contexts are maintained for different task types.

Parallelization runs multiple LLM calls simultaneously, either on different subtasks (sectioning) or the same task (voting). This pattern can dramatically reduce context requirements by distributing information across multiple shorter contexts.

Orchestrator-Workers uses a central LLM to dynamically break down tasks and delegate to workers. This pattern requires sophisticated context engineering to manage information flow between the orchestrator and workers without overwhelming any single context window.

Evaluator-Optimizer implements iterative refinement through separate evaluation and generation LLMs. This pattern highlights the importance of the “compress” strategy, as feedback loops can rapidly expand context without careful management.

Autonomous Agents: The Context Engineering Frontier

True autonomous agents represent the most challenging context engineering scenario. Anthropic emphasizes that agents “plan and operate independently, potentially returning to humans for further information or judgment.” This autonomy creates several context engineering challenges:

Extended Operation: Agents may operate for many turns, requiring sophisticated memory management and context compression strategies.

Environmental Feedback: Agents must incorporate “ground truth” from tool execution and environmental interaction, requiring careful balance between information preservation and context efficiency.

Error Recovery: Agents must maintain context about failures and recovery attempts, directly implementing the Manus principle of “keeping the wrong stuff in.”

Production Insights: Context Engineering in Practice

Anthropic’s experience with customer implementations reveals practical context engineering considerations:

Customer Support Applications

Customer support agents naturally benefit from context engineering because they require access to external information (customer data, knowledge bases) while maintaining conversational flow. The success of usage-based pricing models in this domain suggests that effective context engineering directly translates to business value.

Coding Agents

Software development represents an ideal context engineering testbed because:

  • Code solutions are verifiable through automated tests
  • Agents can iterate using test feedback (implementing write/select cycles)
  • The problem space is well-defined but requires managing large codebases
  • Success can be measured objectively

Anthropic’s SWE-bench results demonstrate how effective context engineering enables agents to solve real GitHub issues, but they emphasize that context engineering extends beyond the core prompt to tool design and environmental setup.

Tool Design as Context Engineering

One of Anthropic’s most valuable insights is that tool design is a form of context engineering. They advocate investing as much effort in Agent-Computer Interfaces (ACI) as traditionally goes into Human-Computer Interfaces (HCI).

Key Principles for Context-Aware Tool Design:

Cognitive Load Reduction: Tools should be designed to minimize the mental effort required from the LLM. Complex formats, counting requirements, or escape sequences increase cognitive overhead and reduce effective context utilization.

Natural Format Alignment: Tool formats should resemble text the model has seen during training. JSON with extensive escaping is harder for models than markdown, affecting how efficiently they can use context.

Error Prevention: Tools should be designed to prevent common mistakes. For example, requiring absolute rather than relative file paths eliminates a class of errors that would otherwise consume context with error-correction cycles.

Documentation as Context: Tool descriptions function as specialized context that must be carefully engineered. Clear examples, edge cases, and boundaries help models use tools effectively while minimizing trial-and-error cycles that consume context.

Framework Integration: Balancing Abstraction and Control

Anthropic acknowledges that frameworks like LangGraph, Amazon Bedrock Agents, Rivet, and Vellum can accelerate development but warns about the context engineering implications of abstraction layers:

Benefits: Frameworks simplify standard tasks like LLM calling, tool parsing, and workflow chaining.

Risks: Extra abstraction layers can obscure underlying prompts and responses, making context debugging difficult. They can also tempt developers to add complexity when simpler solutions would suffice.

Recommendation: Start with direct LLM API usage to understand context engineering fundamentals, then selectively adopt frameworks when they demonstrably improve outcomes.

Strategic Context Engineering Principles

Anthropic’s broader agent design principles directly inform context engineering strategy:

Maintain Simplicity: Context engineering solutions should be as simple as possible while meeting requirements. Complex memory systems, multi-agent architectures, and sophisticated compression strategies should be justified by measurable improvements.

Prioritize Transparency: Agent planning steps should be explicit and observable. This transparency requirement affects context design, favoring approaches that maintain interpretability over pure efficiency.

Craft Agent-Computer Interfaces Carefully: The intersection between agents and their tools/environment represents a crucial context engineering boundary that requires thoughtful design and testing.

Integration with Production Reality

Anthropic’s analysis provides crucial perspective on how context engineering fits within production constraints. Their emphasis on measurement, iteration, and gradual complexity increase offers a roadmap for implementing sophisticated context management systems:

  1. Start with simple context approaches and measure performance
  2. Identify specific context bottlenecks through comprehensive evaluation
  3. Implement targeted context engineering solutions that address measured problems
  4. Iterate based on production feedback rather than theoretical optimization

This methodology ensures that context engineering efforts focus on real performance improvements rather than premature optimization, providing a practical framework for teams building production agent systems.

Conclusion: The Art of Engineering Intelligence

As we stand at the threshold of a new era in artificial intelligence, context engineering emerges not merely as a technical discipline, but as a fundamental craft that bridges the gap between raw computational power and genuinely useful intelligence. The journey we’ve taken through the landscapes of production systems, academic research, and emerging frameworks reveals a profound truth: the future of AI lies not in building more powerful models, but in learning to communicate with them more effectively.

The insights from teams like Manus AI demonstrate that context engineering is where theory meets reality. Their six principles - designing around the KV-cache, masking rather than removing, using the file system as context, manipulating attention through recitation, preserving failures, and introducing structured variation - represent hard-won wisdom from the trenches of production AI. These aren’t academic exercises; they’re battle-tested strategies that determine whether AI systems succeed or fail in the real world.

Yet the elegance of techniques like response prefill and context-aware state machines reveals something deeper: the most sophisticated solutions often emerge from embracing constraints rather than fighting them. By working with the statistical nature of language models instead of against it, we can achieve remarkable behavioral control while maintaining the performance characteristics that make AI systems economically viable.

The academic foundations remind us that this field stands on rigorous research. From the ReAct framework’s demonstration that reasoning and acting can be synergized through context alone, to the nuanced understanding of how temperature affects creativity, to the deep insights into in-context learning dynamics - each contribution builds toward a more complete picture of how language models process and utilize contextual information.

Perhaps most importantly, the integration of memory systems reveals the transformative potential of persistent context. When we extend an agent’s effective memory beyond individual conversations through semantic, episodic, and procedural memory systems, we’re not just solving a technical challenge - we’re enabling the emergence of true learning and adaptation. The combination of carefully engineered immediate context with sophisticated memory retrieval creates agents that don’t just respond to queries, but accumulate wisdom through experience.

The frameworks from LangChain and Anthropic provide the strategic perspective needed to navigate this complexity. LangChain’s four-strategy framework - write, select, compress, and isolate - offers a systematic approach to context management that scales from simple applications to sophisticated multi-agent systems. Anthropic’s emphasis on starting simple and adding complexity only when justified provides the practical wisdom needed to avoid over-engineering while remaining ready to implement advanced techniques when they’re truly needed.

The Economics of Excellence

Throughout this exploration, a crucial theme emerges: context engineering is fundamentally about economics. The 10x cost difference between cached and uncached tokens isn’t just a billing detail - it’s the driving force that makes sophisticated context management essential for production systems. The teams that master KV-cache optimization, implement effective memory systems, and design cache-friendly architectures will build the economically sustainable AI systems that define the next decade.

This economic reality intersects with a technical truth: the most advanced AI capabilities emerge not from brute force computation, but from intelligent information management. A well-engineered context window that combines relevant memories, appropriate tool definitions, and carefully structured prompts can enable remarkable behaviors from models that would be helpless with poorly crafted input.

The Craft of Context

Context engineering represents a new kind of craftsmanship - one that requires understanding transformer architectures, human communication patterns, business logic constraints, and system performance characteristics all at once. It’s a discipline where computer science meets cognitive psychology, where software engineering meets linguistics, where theoretical understanding meets practical constraints.

The practitioners mastering this craft are developing intuitions about how information flows through language models, how attention patterns can be influenced through structure, how errors can become learning opportunities, and how constraints can enable rather than limit intelligence. These intuitions, born from countless iterations of prompt engineering, system optimization, and production debugging, represent a new form of technical expertise that will become increasingly valuable as AI systems become more prevalent.

Looking Forward: The Context Revolution

As we look toward the future, several trends seem inevitable:

Standardization will emerge around context engineering patterns. Just as web development converged on standard frameworks and design patterns, we’ll see the consolidation of best practices into reusable components and standardized protocols. The Model Context Protocol represents an early example of this standardization effort.

Tooling will mature to support sophisticated context engineering workflows. We’ll see development environments that visualize context utilization, debugging tools that trace information flow through multi-turn conversations, and optimization frameworks that automatically improve cache hit rates and memory retrieval patterns.

Integration will deepen between context engineering and other AI capabilities. Multi-modal models will require context engineering techniques that span text, images, audio, and video. Reasoning systems will need context architectures that support complex logical operations. Real-time systems will demand context optimization strategies that minimize latency while maximizing capability.

Education will formalize around context engineering principles. As this discipline matures, we’ll see the emergence of systematic training programs, certification frameworks, and academic curricula that teach these skills as core competencies for AI practitioners.

The Human Element

Perhaps most importantly, context engineering represents a fundamentally human endeavor. While the systems we’re building are artificial, the intelligence we’re engineering is deeply rooted in human communication patterns, reasoning structures, and information processing strategies. The most effective context engineers understand not just how models work, but how humans think, learn, and communicate.

This human-centered perspective ensures that as AI systems become more capable, they also become more understandable, more reliable, and more aligned with human needs and values. The techniques that enable sophisticated agent behaviors - from memory systems that preserve user preferences to state machines that enforce business logic - ultimately serve to make AI systems more helpful, harmless, and honest.

The Invitation to Excellence

For those embarking on this journey, context engineering offers both tremendous opportunity and significant responsibility. The systems we build today will shape how AI integrates into society, how organizations leverage intelligence augmentation, and how individuals interact with computational assistance. The quality of our context engineering directly influences whether AI becomes a tool that amplifies human capability or creates confusion and frustration.

The path forward requires embracing both the technical rigor demonstrated by academic research and the practical wisdom emerging from production systems. It demands understanding economic constraints while pursuing technical excellence. It requires balancing simplicity with sophistication, predictability with adaptability, performance with capability.

As Andrej Karpathy observed, LLMs represent a new kind of operating system where context serves as RAM. If that analogy holds, then context engineers are the systems programmers of the AI era - the practitioners who understand how to manage this new form of computational memory efficiently, effectively, and elegantly.

The art and science of prompt architecture is still in its infancy, but the foundations are solid, the principles are emerging, and the potential is limitless. The context revolution is not coming - it’s here. The question is not whether sophisticated context engineering will become essential, but whether we’ll master it quickly enough to realize the full potential of the intelligence systems we’re building.

In the end, context engineering represents more than a technical discipline - it’s a bridge between human intention and artificial capability, between what we want AI systems to do and what they can actually accomplish. Mastering this bridge is perhaps the most important skill for anyone working at the intersection of human needs and artificial intelligence.

The future belongs to those who can engineer context as masterfully as they engineer code. Welcome to the revolution.