Ai Agent Design Patterns Tutorial
Ai Agent Design Patterns Tutorial
A Comprehensive Tutorial
Introduction
There are many tools and possibilities for AI agents today, creating both exciting opportunities and a lot of noise. To cut through the
confusion, this tutorial provides a framework of six key design patterns you can leverage to build powerful, scalable agentic applications.
Each pattern represents a different approach to designing intelligent systems with specific capabilities and use cases.
Understanding AI agent design patterns can dramatically improve problem-solving, collaboration, and performance, leading to more
sophisticated and reliable AI applications. While prompt engineering is important, systems design matters more for production-ready
applications. These patterns can be used together, not just exclusively, to create powerful AI solutions.
Key Insight: LLMs (Large Language Models) are smart but static. To build truly powerful AI applications, we need to implement
systems that can think, interact, and improve. That's where these design patterns come in.
Table of Contents
1. ReAct Agent Pattern
4. Self-Reflection Pattern
Conclusion
User: "Who won the most recent Super Bowl and what was the score?"
Observation: The most recent Super Bowl was Super Bowl LVIII, held on February 11, 2024, where the
Kansas City Chiefs defeated the San Francisco 49ers with a score of 25-22 in overtime.
Implementation Concepts
ReAct can be implemented in various ways, but typically involves:
Frameworks like LangChain provide built-in functionality to implement ReAct agents with different tools like web search,
calculation, and API calls.
Advantages Limitations
Improves fact-checking and reduces hallucinations by Can get stuck in loops or make inefficient decisions
retrieving real-time information Depends heavily on the quality of the LLM's reasoning
Makes LLM reasoning process explicit and transparent Limited by the tools and APIs available to the agent
Enables interactive problem-solving capabilities May require multiple iterations to solve complex
Used in most AI products today, including basic chat problems
assistants
Real-World Applications
Question-answering systems that require fact retrieval
Customer support assistants
Research assistants
Task-oriented conversational agents
Best Practices
Provide clear, detailed instructions in your prompts about the reasoning process
Include examples (few-shot prompting) to guide the agent's reasoning patterns
Implement safeguards to prevent infinite loops
Ensure tools are reliable and provide meaningful feedback
Result
Problem Analysis Code Generation Code Execution
Interpretation
Agent analyzes the → Agent writes code to solve → Agent runs the → Agent interprets
problem it code
results
User: "Calculate how much money I'd have if I invested $10,000 with 7% annual return for 10 years,
compounded monthly."
# Agent processing
Thought: I need to calculate a compound interest problem with monthly compounding. I'll write a Python
script to do this.
CodeAction:
```python
principal = 10000
annual_rate = 0.07
years = 10
monthly_rate = annual_rate / 12
months = years * 12
Implementation Concepts
Implementing a CodeAct agent requires:
A secure code execution environment/sandbox to protect against malicious code
Clear prompting strategies to guide the LLM in generating correct code
Error handling mechanisms to deal with code that fails to execute properly
A feedback loop to allow the agent to learn from execution results
State management to track variables and computation results between interactions
Advantages Limitations
Dramatically more powerful than text-only agents Security concerns with executing dynamically
Can perform complex calculations and data generated code
transformations Higher computational resources required
Can interact with APIs, databases, and other systems LLMs may generate code with subtle bugs or edge
through code cases
Provides exact, verifiable results for computational tasks Debugging can be complex when code fails silently
Highly flexible - can solve problems that weren't Requires careful sandboxing and resource limitations
anticipated during design
Real-World Applications
Data analysis assistants
Software development companions
Automated research agents
Complex financial calculations
Scientific computing assistants
Best Practices
Implement strict sandboxing with resource limits (CPU, memory, execution time)
Restrict access to sensitive operations and file systems
Provide clear error messages back to the agent when code fails
Use a stateful approach to maintain variables between code executions
Include a mechanism for the agent to test and verify its own code
Unlike traditional tool use (which might be limited to simple API calls), Modern Tool Use often involves:
A large ecosystem of specialized tools
Dynamic selection of appropriate tools for each subtask
Sophisticated formatting and parsing of inputs and outputs
Hierarchical organization of tool selection
↓
Search Tools Knowledge Tools Calculation Tools API Tools
Web, Academic, News Wikipedia, Databases Math, Finance Weather, Stocks
User: "What's the weather in New York today and should I bring an umbrella?"
# Agent processing
Thought: I need to check the weather forecast for New York today to answer this question. I'll use a
weather API tool.
Tool Response: {
"location": "New York, NY",
"date": "2025-05-04",
"conditions": "Partly cloudy with 40% chance of afternoon showers",
"temperature": {"high": 72, "low": 58, "current": 65},
"precipitation": {"probability": 0.4, "type": "rain"}
}
Implementation Concepts
Key components for implementing Modern Tool Use include:
A tool registry that catalogs available tools with their descriptions, input schemas, and capabilities
A tool selection mechanism that helps the LLM choose the most appropriate tool
Structured input/output formats for tool communication (often JSON)
Error handling for tool failures
A prioritization system that prefers specialized tools over general ones
As described in Andrew Ng's framework, Modern Tool Use should follow a hierarchical approach:
"Information seeking should always try to use specialized tools for vertical information like travel, finance, products first, then
fallback to general search if the specialized tools do not return good enough information."
Advantages Limitations
Leverages specialized, optimized services for better Increased system complexity and potential points of
performance failure
Provides access to real-time data and APIs Dependence on external services' availability
More efficient resource utilization by delegating heavy Potential latency issues from API calls
lifting May require specialized knowledge to develop new
Easily extensible by adding new tools tools
Reduces hallucinations by using authoritative external Tool selection might not always be optimal
sources
Real-World Applications
Personal assistants that integrate with multiple services
Research tools that combine multiple information sources
Customer service agents with access to product databases and knowledge bases
Workflow automation systems
Enterprise assistants that interface with internal tools
Best Practices
Provide clear, detailed descriptions of each tool's purpose and usage
Implement a fallback mechanism when tools fail
Use schemas to validate tool inputs and outputs
Create a hierarchy of tools from specialized to general
Log tool usage patterns to identify improvement opportunities
Implement timeouts for external tool calls
4. Self-Reflection Pattern
As Andrew Ng points out in his agentic design patterns series: "Most AI errors happen because this step is missing."
Self-Reflection Workflow
"This self-reflection process allows the LLM to spot gaps and improve its output on a variety of tasks including producing
code, writing text, and answering questions."
User: "Write a short explanation of quantum computing for high school students."
# Initial generation
Initial Response: "Quantum computing uses quantum bits or qubits which can be both 0 and 1 at the same
time due to superposition. This allows quantum computers to solve complex problems much faster than
classical computers that use regular bits that can only be 0 or 1."
# Self-reflection phase
Self-Critique: "My explanation is technically correct but may be too abstract for high school
students. I didn't provide concrete examples or analogies that would make the concept more
approachable. The explanation is also quite brief and doesn't cover key quantum concepts like
entanglement or potential applications that would make the topic more interesting."
This special property is called 'superposition.' It's like being able to check many answers to a
problem all at once instead of one at a time. Another cool quantum feature is 'entanglement,' which is
like having two magical coins that always match each other's result when flipped, even if they're far
apart.
Scientists are excited about quantum computers because they might someday solve problems that would
take regular computers thousands of years, like designing new medicines or creating better materials.
While fully functional quantum computers are still being developed, they represent one of the most
exciting frontiers in technology today!"
Implementation Concepts
Implementing Self-Reflection can be done in several ways:
Single-Agent Reflection: The same LLM instance critiques its own output with specific prompting
Multi-Agent Reflection: One LLM instance generates content, while another is specifically prompted to critique it
Tool-Augmented Reflection: Using external tools to evaluate outputs (e.g., fact-checking against databases)
Structured Rubric-Based Evaluation: Using specific criteria to systematically evaluate different aspects of the output
Advantages Limitations
Significantly reduces errors and hallucinations Increases token usage and processing time
Improves output quality with minimal additional May still miss certain types of errors
prompt engineering Can sometimes be overly critical or focus on superficial
Makes reasoning more transparent and traceable improvements
Can adapt to feedback more effectively LLMs might be biased in their self-evaluation
Relatively simple to implement compared to other Without external references, cannot correct
patterns fundamental knowledge gaps
Real-World Applications
Content generation with quality assurance
Code review assistants
Academic writing and research assistants
Decision support systems
Educational tutoring systems
Best Practices
Provide specific criteria for self-evaluation rather than general instructions
Consider using different system prompts for generation vs. critique phases
Limit the number of reflection cycles to avoid diminishing returns
Maintain a record of both original and improved outputs for quality tracking
Supplement self-reflection with external factual verification when possible
Orchestrator Agent
"Different agents might be built by prompting one LLM (or, if you prefer, multiple LLMs) to carry out different tasks. For
example, to build a software engineer agent, we might prompt the LLM: 'You are an expert in writing clear, efficient code.
Write code to perform the task...'"
User: "Create a simple web application that allows users to track their daily water intake."
# Designer Agent
Designer Agent: "UI Design: Simple dashboard with current day's intake, progress bar showing % towards
daily goal, add intake button with quick-add options (glass, bottle), settings page for goal
adjustment, and history page with chart."
# Developer Agent
Developer Agent: "I'll implement this using React for frontend, with local storage for data
persistence. Components: Header, Dashboard, AddIntakeForm, ProgressBar, HistoryChart, Settings."
# QA Agent
QA Agent: "Test cases: Verify intake logging works correctly, ensure goal tracking updates properly,
check that history is stored and displayed correctly, verify responsive design works on mobile and
desktop."
Implementation Concepts
Key considerations for implementing Multi-Agent Workflows include:
Advantages Limitations
Reduces complexity through specialized agent roles Increased system complexity and overhead
Improves performance through focused attention on Coordination challenges and potential communication
subtasks bottlenecks
Enables parallel processing of different aspects of a task Higher computational and token costs
Creates built-in checks and balances through different Can be unpredictable in complex scenarios
agents' perspectives More difficult to debug when issues arise
More closely models human team collaboration
Real-World Applications
Collaborative software development environments
Research and analysis teams for complex topics
Content creation pipelines (research, writing, editing, fact-checking)
Business decision support systems
Project management assistants
Best Practices
Define clear roles, responsibilities and boundaries for each agent
Create structured communication protocols between agents
Implement a strong orchestration mechanism to coordinate workflows
Include observation and debugging capabilities to track agent interactions
Start simple and add complexity incrementally as the system stabilizes
Create fallback mechanisms when agent collaboration hits roadblocks
User: "What are the best exercises for someone with lower back pain?"
Important safety notes: Start gradually, avoid jerky movements, and stop if pain increases. A physical
therapist can provide personalized recommendations for your specific condition.
Sources: American Academy of Orthopedic Surgeons, Mayo Clinic, and Harvard Health Publishing."
Implementation Concepts
Key components for implementing Agentic RAG include:
Advantages Limitations
More accurate responses through targeted, adaptive More complex to implement than standard RAG
retrieval Can be slower due to multiple retrieval rounds
Works with real-time data, not just model memory Higher computational and token costs
Reduces hallucinations by grounding responses in May struggle with highly specialized or obscure topics
retrieved information Quality depends on both retrieval sources and agent
Can find and synthesize information from multiple reasoning
sources
Creates more transparent, attributable responses
Real-World Applications
Advanced research assistants (like Perplexity)
Medical and legal information systems
Technical documentation assistants
Academic research tools
Knowledge-intensive enterprise applications
Best Practices
Incorporate diverse, high-quality information sources
Implement mechanisms to detect and resolve conflicting information
Use citation tracking to maintain attribution
Create evaluation metrics to assess retrieval quality
Establish thresholds for when additional retrievals are needed
Maintain transparency about information sources in responses
Important Considerations
Design patterns are guidelines, not rigid rules—adapt them to your specific use cases
Start with simpler patterns (like ReAct and Self-Reflection) before attempting more complex ones
Consider computational costs and complexity when combining multiple patterns
Implement proper monitoring and evaluation to ensure patterns work effectively
Remember that even with these patterns, LLMs have fundamental limitations
As stated in the original framework: "𝗨𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱𝗶𝗻𝗴 𝗔𝗜 𝗮𝗴𝗲𝗻𝘁 𝗱𝗲𝘀𝗶𝗴𝗻 𝗽𝗮𝘁𝘁𝗲𝗿𝗻𝘀 𝗰𝗮𝗻 𝗶𝗺𝗽𝗿𝗼𝘃𝗲 𝗽𝗿𝗼𝗯𝗹𝗲𝗺-𝘀𝗼𝗹𝘃𝗶𝗻𝗴,
𝗰𝗼𝗹𝗹𝗮𝗯𝗼𝗿𝗮𝘁𝗶𝗼𝗻, 𝗮𝗻𝗱 𝗽𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲, 𝗹𝗲𝗮𝗱𝗶𝗻𝗴 𝘁𝗼 𝗺𝗼𝗿𝗲 𝘀𝗼𝗽𝗵𝗶𝘀𝘁𝗶𝗰𝗮𝘁𝗲𝗱 𝗮𝗻𝗱 𝗿𝗲𝗹𝗶𝗮𝗯𝗹𝗲 𝗔𝗜 𝗮𝗽𝗽𝗹𝗶𝗰𝗮𝘁𝗶𝗼𝗻𝘀." With these
patterns in your toolkit, you're well-equipped to build the next generation of intelligent systems that can think critically, act
effectively, and solve complex problems.