# AI Agent Design Patterns: ReAct, CodeAct & More | Stanford CS329Z Lecture 6 with Michael Ryan

## Executive summary

This lecture provides a deep dive into advanced design patterns for building AI agents and complex workflows. It contrasts traditional workflows (where control flow resides in program code) with agents (where control flow is managed by the LLM). Key agent patterns include ReAct (Reasoning-Acting loop), Reflection (learning from past experience), CodeAct (using code generation for actions), and ReWOO (planning before execution). For context management, the discussion covers techniques like Persistent Event Logging, Observation Masking, and Context Isolation (via subagents), culminating in a review of the minimalist Pi agent harness.

## Key takeaways

- Workflows vs. Agents: Workflows are ideal for tasks with clear, repeatable structure (control flow in code), while agents are better suited for tasks requiring flexible, adaptive decision-making (control flow in the LLM).
- CodeAct Pattern: Using code generation as the action format (CodeAct) is superior to JSON or text tool calls because it is more accurate, more efficient, and accomplishes tasks in fewer turns.
- ReWOO (Plan then Execute): Instead of iterative reasoning (ReAct), generating a full plan (Directed Acyclic Graph) first, and then executing the steps, is a powerful pattern that reduces token usage and latency.
- Context Management Strategies: Effective context management involves techniques like Persistent Event Logging (preserving all actions/observations) and Observation Masking (condensing observations instead of summarizing the entire history) to prevent context window bloat.

## Technical details

- Workflow Design Patterns (Anthropic Taxonomy): Five patterns include: Prompt Chaining (decomposing tasks into sequential LLM calls), Routing (dispatching input to specialized models/workflows), Parallelization (dividing independent work across threads), Orchestrator Workers (dynamically dispatching subtasks), and Evaluator Optimizer (using an LLM to review and provide feedback to a generator in a loop).
- Agent Design Patterns: 1. ReAct: Combines Chain of Thought (reasoning) and Actions (tool calls). 2. Reflection: Improves performance by learning from long-term experience and trajectories. 3. CodeAct: Uses code generation for actions. 4. ReWOO: Plans the entire sequence of actions (DAG) before execution.
- Context Management Patterns: 1. Persistent Event Log: Records every action, observation, and response to allow reconstruction of the context. 2. Memory Offloading: Storing facts (e.g., user details) externally and retrieving them via tool calls (RAG). 3. Compaction: Summarizing long context segments. 4. Observation Masking: Programmatically condensing or truncating observations rather than relying solely on summarization. 5. Context Isolation: Using subagents or Progressive Disclosure (skill files) to limit the scope of context.
- Pi Agent Harness: Pi is a minimalist agent harness designed to be adapted to custom workflows. It features built-in context management via event logs, progressive disclosure, and compaction, but lacks built-in safeguards (e.g., permission checks).

## Practical implications

- When designing an AI system, determine if the task requires predictable, structured execution (use a workflow) or flexible, adaptive decision-making (use an agent).
- For complex agents, prioritize planning patterns (ReWOO) and code-based actions (CodeAct) over simple iterative reasoning (ReAct) to improve efficiency and accuracy.
- Implement robust context management by using Persistent Event Logs to maintain full history and employing Observation Masking or Compaction to manage context window limits.
- Utilize the Pi harness as a baseline for building custom, minimalist, and highly adaptable agent systems.

## Topics

Agent Design Patterns, Workflow Design Patterns, Context Management, LLM Orchestration, Agentic AI, Stanford Online Agentic AI Course, Stanford CS329Z Playlist

Source: https://www.youtube.com/watch?v=JC_pLC5EX_w
