Key Moments

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

Stanford OnlineStanford Online
Education5 min read77 min video
Nov 14, 2026|12,568 views|225|1
Save to Pod
TL;DR

AI agents are evolving beyond simple workflows with new design patterns like ReAct and CodeAct, but context management remains a critical challenge, impacting agent performance and cost.

Key Insights

1

Workflows offer predictability and control due to logic residing in code, whereas agents provide adaptability and flexibility with control residing in the LLM, making them less predictable.

2

Anthropic's five workflow design patterns include prompt chaining, routing, parallelism, coordinator workers, and an evaluator-refiner loop.

3

The ReAct (Reasoning and Acting) pattern combines chain-of-thought reasoning with tool use, forming the basis for many modern agents.

4

Reflection enables agents to learn from long-term experience by analyzing past interaction paths and reflections for improved future performance.

5

Context management is crucial for LLM efficiency, with strategies like context compression, observation hiding, and context isolation helping to manage lengthy interaction histories.

6

Pie, a simple agent environment, prioritizes adaptability by allowing users to build extensions rather than adapting workflows to Pie, but lacks built-in safety checks.

Distinguishing workflows from agents

The lecture begins by differentiating between workflows and agents. Workflows reside in the code itself, offering predictability and control, making them suitable for structured, repeatable tasks. Agents, on the other hand, have control flow residing within the LLM, offering adaptability and flexibility for exploration and decision-making, though they are less predictable and harder to evaluate. The control flow in workflows is fixed, while agent control flow can differ with each execution. Workflows excel at tasks like classifying support tickets or drafting automated responses, whereas agents are better for problem-solving and exploration.

Anthropic's workflow design patterns

Several design patterns for structuring workflows are presented, based on Anthropic's work. 'Prompt chaining' breaks down problems into sequential LLM calls. 'Routing' directs tasks to different LLMs based on input characteristics, optimizing for cost and capability. 'Parallelism' splits work into independent threads for simultaneous processing, useful for tasks like classifying user input while generating a response or for voting on multiple outputs. 'Coordinator workers' dynamically distribute sub-tasks to workers, especially when the number and nature of sub-tasks are unpredictable. Finally, the 'evaluator-refiner' pattern involves an LLM generating an output, another LLM evaluating it, and feeding back for improvement, creating an iterative refinement loop. These patterns are often combined in real-world systems.

Reasoning and acting patterns for agents

The lecture then delves into agent design patterns, starting with ReAct (Reasoning and Acting). ReAct combines chain-of-thought reasoning with tool use, allowing agents to 'think' before 'acting.' For instance, to answer a query about devices compatible with an Apple remote, an agent using ReAct would first think about needing to search for related information, then perform the search, and then use the search results to formulate an answer. This is contrasted with simple tool use where the agent might directly call tools without intermediate reasoning. Research suggests that while larger models may not always benefit from ReAct with basic prompting, fine-tuning them for this pattern significantly improves performance. The pattern is crucial for agents that need to interact with environments and tools effectively. 'Reflection' is another key pattern, enabling agents to learn from past experiences. An agent generates a trajectory of actions, an evaluator analyzes its success, and a self-reflecting LLM stores insights in long-term memory. This allows the agent to improve performance over time by avoiding past mistakes, as demonstrated in a programming task where an agent learned to correctly check parenthesis matching after initially failing.

CodeAct and planning-then-execution for action

The 'CodeAct' pattern proposes using code itself as a suitable format for actions, treating tools as functions. Instead of defining tool calls via JSON or text prompts, the agent writes code that internally calls these functions. This approach has shown higher success rates and fewer steps compared to other methods, offering greater efficiency and accuracy. The 'plan-then-execute' pattern, exemplified by ReWOO and LLM Compiler, involves generating a plan or a directed acyclic graph (DAG) of operations before execution. This allows for pre-assembly of tool calls and execution logic, potentially reducing the number of LLM calls and improving efficiency. While this pattern can be implemented dynamically, modern approaches often lean towards code generation, where the agent directly generates executable code for the entire task, offering more flexibility.

Context management strategies for LLM efficiency

Effective context management is vital for LLM performance, especially with increasing context windows. 'Prompt engineering' has evolved into 'context engineering,' focusing on curating relevant information for the LLM. Key principles include 'focus' (keeping context concise and task-relevant) and 'continuity' (maintaining system goals and constraints). Patterns for managing context include 'persistent event logging,' where all interactions are recorded and rebuilt for the LLM's input; 'memory dumping,' where information is stored in memory or files for later retrieval (like in MemGPT); and 'compression,' where the LLM summarizes lengthy contexts. 'Observation hiding,' an alternative to compression, involves selectively omitting or summarizing observations that are less critical to the current task, which can be more efficient and easier to implement programmatically. 'Context isolation' further refines this by using sub-agents with their own contexts or employing progressive disclosure, where the LLM requests information (e.g., skill files) only when needed.

The Pie agent environment and its design philosophy

Pie is presented as a simple, extensible agent environment designed to adapt to workflows rather than vice-versa. It prioritizes simplicity and extensibility, allowing users to build numerous add-ons. Key limitations include the absence of sub-agents, permission checks, planning modes, or background Bash execution; all operations must occur within a single session. Pie's built-in tools are limited to read, Bash, edit, and write, with others accessible via Bash scripting. Its agent loop follows the ReAct pattern, managing context through event logs, progressive disclosure via skill files, and standard compression. Notably, Pie operates with the same permissions as the executing process, lacking built-in safety guarantees, making it inherently automatic but potentially risky depending on the application.

Common Questions

Workflows have control flow logic embedded in code, making them predictable and easier to optimize. Agents, on the other hand, delegate control flow to an LLM, making them more adaptable and flexible but also less predictable and harder to evaluate.

Topics

Mentioned in this video

Software & Apps
Qwen 2.5

One of the LLMs used in the Archon research to generate candidate solutions for mathematical problems.

Singleton

An example of a software design pattern mentioned to illustrate the concept of design patterns in programming.

ReWOO

A research paper and framework presented as an example of the 'Plan and Execute' design pattern, focusing on generating a plan before execution, exemplified by a quality assurance task.

PowerShell

A command-line shell and scripting language available as a tool within the Pie environment.

Storm

A system described as a case study for integrating workflow design patterns, capable of generating detailed Wikipedia-like articles from a given topic, including research and citations.

LLM Compiler

Another research paper that applies the 'Plan and Execute' principle, focusing on generating a directed acyclic graph for execution and dynamic replanning if execution fails.

MemGPT

A research paper discussed in the context of memory dumping, where agents can recall past interactions and store them in a file system for retrieval, demonstrating memory management for agents.

ChatGPT

Mentioned in the context of cost savings, with a question about whether one could use an open-source model instead of proprietary ones like ChatGPT.

Claude

Mentioned as an LLM used to evaluate and re-rank solutions generated by other models in the Archon framework.

LangChain

A blog post from LangChain is referenced for a taxonomy of context management strategies, categorizing them into writing, selection, compression, and isolation contexts.

Open Hands

A project from Carnegie Mellon University that uses an event stream to manage agent context, recording all interactions for rebuilding the LLM's input.

DaVinci 3

Mentioned in comparison to CodeAct, showing that while text-based actions might have fewer steps in some cases, CodeAct generally leads to more efficient execution.

Claude 3.5 Sonnet

Specifically mentioned as the model used for re-ranking solutions within the Archon framework.

Bash

A command-line interpreter available as a tool within the Pie environment, used for executing code and scripts.

Archon

A framework for searching software architecture for inference-time techniques, which helps in automatically building workflows based on specified criteria and available LLMs.

Llama 3.1

One of the LLMs used in the Archon research to generate candidate solutions for mathematical problems.

Front Row Media Center

A program mentioned in the ReAct example; the agent searches for it to determine which devices it can control besides the Apple remote.

More from Stanford Online

View all 178 summaries

Ask anything from this episode.

Save it, chat with it, and connect it to Claude or ChatGPT. Get cited answers from the actual content — and build your own knowledge base of every podcast and video you care about.

Get Started Free