Skip to content

Complete Agent Example

"Putting it all together: A production-inspired agentic system"

๐Ÿ“– Overview

This example demonstrates how to combine multiple agentic patterns into a cohesive, production-ready system inspired by Codex's architecture.

๐ŸŽฏ Patterns Integrated

This complete example integrates:

  1. โœ… Prompt Chaining - Multi-turn conversation
  2. โœ… Routing - Dynamic tool dispatch
  3. โœ… Parallelization - Concurrent tool execution
  4. โœ… Tool Use - External system integration
  5. โœ… Memory Management - Conversation persistence
  6. โœ… Exception Handling - Retry logic and recovery
  7. โœ… Human-in-the-Loop - Approval workflows
  8. โœ… Guardrails - Safety checks and sandboxing

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    Codex-Inspired Agent                      โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                               โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚            Conversation Manager                        โ”‚  โ”‚
โ”‚  โ”‚  โ€ข Turn-based execution loop                          โ”‚  โ”‚
โ”‚  โ”‚  โ€ข History management                                 โ”‚  โ”‚
โ”‚  โ”‚  โ€ข State persistence                                  โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚                        โ”‚                                     โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚            Tool Router & Safety Layer                  โ”‚  โ”‚
โ”‚  โ”‚  โ€ข Intent classification                              โ”‚  โ”‚
โ”‚  โ”‚  โ€ข Safety assessment                                  โ”‚  โ”‚
โ”‚  โ”‚  โ€ข Approval requests                                  โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚                        โ”‚                                     โ”‚
โ”‚         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                     โ”‚
โ”‚         โ”‚              โ”‚              โ”‚                     โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”           โ”‚
โ”‚  โ”‚  File Ops โ”‚  โ”‚   Shell   โ”‚  โ”‚  Calculator   โ”‚  ...      โ”‚
โ”‚  โ”‚   Tools   โ”‚  โ”‚   Tools   โ”‚  โ”‚     Tools     โ”‚           โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜           โ”‚
โ”‚                                                               โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚            Error Handler & Retry Logic                 โ”‚  โ”‚
โ”‚  โ”‚  โ€ข Exponential backoff                                โ”‚  โ”‚
โ”‚  โ”‚  โ€ข Circuit breaker                                    โ”‚  โ”‚
โ”‚  โ”‚  โ€ข Graceful degradation                               โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚                                                               โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“‚ Files

  • agent_core.py: Core agent implementation
  • tool_system.py: Tool registry and execution
  • safety_layer.py: Safety checks and approvals
  • conversation_manager.py: State and history management
  • error_handling.py: Retry logic and recovery
  • example_usage.py: Demo scenarios

๐Ÿš€ Quick Start

# Install dependencies
pip install openai aiohttp

# Set API key
export OPENAI_API_KEY="your-key-here"

# Run the complete example
python example_usage.py

๐Ÿ’ก Key Features

1. Multi-Turn Conversations

Like Codex, maintains context across turns:

agent = CodexInspiredAgent()
agent.run("Analyze the codebase")
# Turn 1: Reads files
# Turn 2: Analyzes code
# Turn 3: Generates report

2. Safe Tool Execution

Multiple layers of safety:

# Whitelist checking
# Sandboxing simulation
# User approval for dangerous ops
# Output truncation
# Timeout enforcement

3. Intelligent Routing

Automatically routes to appropriate tools:

# "What time is it?" โ†’ time tool
# "Calculate 2+2" โ†’ calculator tool
# "Read main.py" โ†’ file tool

4. Error Recovery

Handles failures gracefully:

# Automatic retry with backoff
# Fallback to alternative approaches
# User notification of issues

5. Conversation Persistence

Saves and resumes sessions:

agent.save_session("session_123.json")
# Later...
agent.load_session("session_123.json")

๐Ÿ“Š Example Scenarios

Scenario 1: Code Analysis Task

query = "Analyze main.py, find bugs, and suggest fixes"

# Turn 1: Read file
# Turn 2: Analyze for bugs (uses analysis tool)
# Turn 3: Generate fixes (uses code generation)
# Turn 4: Present recommendations

Scenario 2: Data Processing

query = "Process sales.csv and create a summary report"

# Turn 1: Read CSV (file tool)
# Turn 2: Calculate statistics (calculator tool)
# Turn 3: Generate visualizations (visualization tool)
# Turn 4: Write report (file tool)

Scenario 3: System Administration

query = "Check disk usage and clean up old logs"

# Turn 1: Check disk (shell tool)
# Approval requested: "Execute 'du -sh'?"
# Turn 2: Identify old files (shell tool)
# Approval requested: "Delete 500MB of logs?"
# Turn 3: Clean up (shell tool)
# Turn 4: Verify space freed

๐Ÿ” Safety Features

Command Whitelisting

SAFE_COMMANDS = ["ls", "cat", "grep", "find"]
DANGEROUS_COMMANDS = ["rm", "dd", "mkfs", "shutdown"]

Approval Workflow

if is_dangerous_operation(tool_call):
    approval = request_user_approval(tool_call)
    if not approval:
        return "Operation denied by user"

Sandboxing

# Network disabled
# File access limited to workspace
# Process timeout: 30s
# Output truncated: 10KB max

๐Ÿ“ˆ Performance Considerations

Parallel Tool Execution

# Some tools run in parallel
results = await asyncio.gather(
    execute_tool("read_file", {"path": "a.py"}),
    execute_tool("read_file", {"path": "b.py"}),
    execute_tool("read_file", {"path": "c.py"}),
)

Context Window Management

# Automatically compress old history
if len(history) > MAX_TURNS:
    history = compress_history(history)

Caching

# Cache file reads
# Cache tool results
# Cache LLM responses (deterministic queries)

๐Ÿงช Testing

# Run unit tests
python -m pytest test_agent.py

# Run integration tests
python -m pytest test_integration.py

# Run safety tests
python -m pytest test_safety.py

๐Ÿ“š Learning Objectives

After studying this example, you should understand:

  1. โœ… How to architect a multi-pattern agent system
  2. โœ… State management across async operations
  3. โœ… Safety-first design principles
  4. โœ… Error handling and recovery strategies
  5. โœ… Tool orchestration and routing
  6. โœ… User interaction patterns
  7. โœ… Performance optimization techniques

๐Ÿ”— Codex Comparison

Feature This Example Codex
Language Python Rust
Sandboxing Simulated Real (Seatbelt/Landlock)
MCP Support No Yes
TUI No Yes (Ratatui)
State Persistence JSON Binary rollout files
Error Handling Basic retry Advanced with backoff
Tool Parallelization Yes Yes
Approval System Yes Yes

๐Ÿš€ Extension Ideas

  1. Add MCP Support: Integrate with external MCP servers
  2. Implement Streaming: Stream tool output in real-time
  3. Add Telemetry: OpenTelemetry integration
  4. Build TUI: Terminal UI like Codex
  5. Enhance Sandboxing: Use Docker or similar
  6. Add Planning: Explicit planning tool like Codex
  7. Multi-Agent: Coordination between multiple agents

๐Ÿ“„ License

Educational example for learning purposes.


Start with: example_usage.py to see it in action!