Skip to content

Part 9: Production Patterns

🎯 Overview

Getting an agent to work in a lab is one thing. Getting it to reliably work in production at scale is another.

This section covers the patterns that separate prototype agents from production systems: - How to keep agent state consistent across failures - When to route work to different agents - How to let humans oversee critical decisions - How to manage expensive context windows - How to see what's happening in production - How to recover gracefully from failures

Key Insight: Production is about tradeoffs. Safety vs speed, cost vs quality, autonomy vs control.


📊 Chapter Statistics

Metric Value
Topic Files 6 comprehensive guides
Total Words 11,500+
Code Examples 55+ production-grade
Deployment Patterns 20+ real patterns
Warnings 18+ anti-patterns
Real-World Cases 8+ case studies

🏭 Production Reality

Lab Environment:

Controlled data
Unlimited time
High latency acceptable
Few requests
No failures expected

Production Environment:

Messy real-world data
Strict latency budgets
Millions of requests
Failures happen daily
Users hate delays


🔑 Key Production Concerns

Concern Impact Pattern
Agent crashes Lost context State Management & Recovery
Can't see issues Discover problems from users Observability & Monitoring
Bad decisions Regulatory/compliance risk Human-in-the-Loop
Too many requests Cascading failures Routing & Load Balancing
Errors cascade System outage Error Recovery
Context too expensive Token costs spiral Context Management
Resource leaks OOM, crashes Resource Management

📚 Complete Chapter Organization

1. State Management & Recovery (2,100 words)

01 State Management - Agent state and consistency - Checkpointing strategies - Recovery from failures - Distributed state coordination - Saga pattern for multi-step workflows

2. Routing & Escalation (1,800 words)

02 Routing Escalation - Request routing strategies - Load balancing - Escalation policies - Risk-based routing - Queue management

3. Human-in-the-Loop (1,900 words)

03 Human In The Loop - When to escalate to humans - Approval workflows - Feedback integration - Hybrid autonomy model - Scalable human oversight

4. Context Management (1,700 words)

04 Context Management - Context window economics - Compression strategies - Retrieval-augmented generation - Context window optimization - Token budgeting

5. Observability & Monitoring (1,900 words)

05 Observability Monitoring - Structured logging - Trace collection - Metrics and dashboards - Alerting strategies - Post-incident analysis

6. Error Recovery (2,100 words)

06 Error Recovery - Error classification - Recovery strategies - Circuit breakers - Graceful degradation - Fallback patterns


🎯 Learning Paths

Path 1: Full Production Deployment (6 hours)

  1. State Management - Keep state consistent
  2. Routing - Handle traffic
  3. Human Loop - Add oversight
  4. Context - Control costs
  5. Observability - See what's happening
  6. Error Recovery - Handle failures

Path 2: Quick to Production (3.5 hours)

  1. State Management - Stability first
  2. Observability - Visibility
  3. Error Recovery - Reliability
  4. Human Loop - Safety

Path 3: Scale Existing System (4 hours)

  1. Routing - Handle load
  2. Context - Reduce costs
  3. Error Recovery - Prevent cascades
  4. Observability - Track performance

Path 4: Optimize Running System (3 hours)

  1. Context - Reduce token costs
  2. Routing - Better load distribution
  3. Observability - Find bottlenecks

⚖️ The Hybrid Autonomy Model

2025-2026 Best Practice:

Risk Level    Autonomy Model                  Examples
─────────────────────────────────────────────────────────
Low (< $10)   ✅ Fully Autonomous            Standard support reply
              → Agent decides alone          Simple data lookup

Medium        ⚠️  Approve then Execute      Refund > $50
($10-$100)    → Human reviews               Account modification
              → Agent executes              Policy adjustment

High          👤 Human Decision             Delete customer data
(> $100)      → Agent recommends            Override policy
              → Human decides               High-value transaction
              → Agent executes

Critical      🚨 Manual with Review         Payment processing
(> $10k)      → Agent performs recon       Regulatory decisions
              → Multiple humans review      Security incidents
              → Human authorizes

🏗️ Production Architecture

Request
    ↓
Input Validation (Safety)
    ↓
Routing (Cost/Capability)
  - → Simple Agent (Low-risk)
  - → Complex Agent (Medium-risk)
  - → Human with Agent Assistance (High-risk)
    ↓
State Checkpoint (Durability)
    ↓
Execution with Error Handling
    ↓
Context Management (Cost Control)
    ↓
Monitoring & Observability
    ↓
Output Validation & Filtering
    ↓
State Persistence
    ↓
Response + Audit Trail

📊 Common Production Patterns

Pattern 1: Simple Direct Execution ❌ (High Risk)

Request → Agent → Response

Problems:
- No error recovery
- State lost on crash
- Can't rollback
- No visibility
Request → Validate → Route → State Checkpoint → Execute → 
Monitor → Validate Output → Persist → Response

Advantages:
- Each stage independent
- Can add safety checks
- Easy to observe
- Can rollback

⚠️ Critical Warnings Summary

Production Mistakes: - ❌ No state persistence (lost work on crash) - ❌ No human oversight (risky decisions) - ❌ No routing strategy (overwhelming single agent) - ❌ Unbounded context (spiraling token costs) - ❌ No observability (discover issues from users) - ❌ No error recovery (cascading failures) - ❌ No rate limiting (DDoS yourself)


🚀 Quick Start: Minimal Production Setup

class MinimalProductionAgent:
    def __init__(self):
        self.state_manager = StateManager()
        self.router = Router()
        self.monitor = Monitor()
        self.error_handler = ErrorHandler()

    def handle_request(self, request):
        try:
            # 1. Save state before processing
            checkpoint = self.state_manager.checkpoint()

            # 2. Route to appropriate handler
            handler = self.router.select_handler(request)

            # 3. Execute
            result = handler.execute(request)

            # 4. Monitor and validate
            self.monitor.track(result)

            # 5. Persist state
            self.state_manager.persist(result)

            return result

        except Exception as e:
            # Recover from checkpoint
            self.error_handler.recover(checkpoint, e)
            raise

✅ Production Readiness Checklist

Before deploying agents to production:

  • State Management: Checkpoint/recovery in place
  • Routing: Request distribution strategy defined
  • Human Loop: Escalation policy for high-risk
  • Context: Token budgeting and compression
  • Observability: Logging and metrics set up
  • Error Handling: Recovery paths for all failures
  • Monitoring: Dashboards and alerts configured
  • Rate Limiting: Capacity protection in place
  • Incident Response: On-call process documented
  • Rollback Plan: How to revert changes quickly

  • Safety & Reliability (Ch 7): Safe execution
  • Evaluation (Ch 8): Production metrics
  • Tool Use (Ch 6): Tool observability
  • Memory Systems (Ch 4): State persistence
  • Planning & Reasoning (Ch 5): Decision tracing

🌟 Key Insights

  1. State is everything - Agents without persisted state are unreliable
  2. Humans at the helm - Critical decisions need human oversight
  3. Observability is non-negotiable - If you can't see it, you can't fix it
  4. Context matters - Token costs can spiral; manage proactively
  5. Failures will happen - Design for graceful degradation
  6. Routing for efficiency - Different requests need different agents
  7. Scale is a feature - Production must handle 10-1000x load

📖 Start Reading

First time? → Start with State Management

Need reliability? → Start with Error Recovery

Need visibility? → Start with Observability

Handling high-risk decisions? → Start with Human In The Loop

Reducing costs? → Start with Context Management

Scaling up? → Start with Routing & Escalation


Last Updated: August 9, 2026
Status: ✅ Complete chapter guide (6 comprehensive topic files, 11,500+ words)