Agentic Design Patterns: A Complete Guide to Building Intelligent Systems with AI Agents

· evoiz · AI Agent / Agentic Design Patterns / Artificial Intelligence / Design Patterns / LangChain / AutoGPT / AutoGen / CrewAI

Agentic Design Patterns: A Complete Guide to Building Intelligent Systems with AI Agents

Project Introduction and Overview

Agentic Design Patterns is an open-source learning repository based on Antonio Gulli's book "Agentic Design Patterns: A Hands-On Guide to Building Intelligent Systems". Created and maintained by evoiz, the project has gained 2.4k Stars and 405 Forks on GitHub, making it a significant learning resource in the AI Agent design and implementation space.

Project Scope

The book contains 424 pages, covering 21 chapters and 7 appendices, forming a comprehensive knowledge system for AI Agent design. Whether you're a beginner or an experienced developer, you can find systematic guidance and practical insights here.

Key Features

  • Charitable Cause: The author donates all royalties to Save the Children, demonstrating social responsibility
  • Progressive Learning Path: From basic concepts to advanced applications, step by step
  • Practice-Oriented: Combines code with theory, supporting Jupyter Notebook interactive learning
  • Wide Framework Coverage: Covers LangChain, AutoGPT, AutoGen, CrewAI and other mainstream frameworks

Core Design Philosophy

What are Agentic Design Patterns?

Agentic Design Patterns are the core methodology for building AI Agent systems. It doesn't just focus on the capabilities of a single model, but explores how to design multiple components, tools, and decision-making processes to work together, enabling AI systems to:

  • Autonomously Execute Complex Tasks: Decompose complex tasks into manageable steps
  • Dynamically Select Optimal Strategies: Make intelligent routing decisions based on context
  • Reflect and Improve: Evaluate their own outputs and continuously optimize
  • Collaboratively Solve Problems: Multiple agents working together

Why are Agentic Design Patterns Important?

As large language models (LLMs) continue to grow in capability, the limitations of single models are becoming increasingly apparent. Agentic Design Patterns provide a systematic approach to help developers:

  1. Break Through Single-Model Bottlenecks: Build more powerful systems by combining multiple specialized capabilities
  2. Automate Complex Tasks: Integrate expert-level reasoning capabilities into automated workflows
  3. Improve System Reliability: Reduce erroneous outputs through reflection and verification mechanisms
  4. Support Enterprise Applications: Provide the security and observability required in production environments

Detailed Learning Path: Four Pattern Categories

Agentic Design Patterns organizes content into four major categories, forming a complete learning path from beginner to expert:

Category Chapters Core Philosophy
Core Patterns Chapters 1-7 Building foundational capabilities: chain processing, routing, parallel execution
Advanced Patterns Chapters 8-11 Enhanced intelligence: memory, learning, protocols, monitoring
Production Patterns Chapters 12-14 Ensuring reliability: exception handling, human-agent collaboration, knowledge retrieval
Enterprise Patterns Chapters 15-21 Scaled deployment: communication, optimization, reasoning, security

Detailed Pattern Explanations

Part 1: Core Patterns (Chapters 1-7)

1. Prompt Chaining

Prompt chaining is one of the most fundamental Agentic patterns. It decomposes complex tasks into multiple simple steps, each driven by a specialized prompt.

How It Works:

Input → Step 1 (Prompt A) → Step 2 (Prompt B) → Step 3 (Prompt C) → Final Output

Application Scenarios:

  • Content Moderation: Classify first, extract keywords, then generate report
  • Document Processing: Parse structure first, extract entities, then perform sentiment analysis
  • Complex Q&A: Understand the question first, retrieve information, then generate answer

Code Example:

from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate

# Step 1: Understand user intent
intent_chain = LLMChain(
    llm=llm,
    prompt=PromptTemplate(
        template="Analyze the intent of this user query: {query}",
        input_variables=["query"]
    )
)

# Step 2: Generate response
response_chain = LLMChain(
    llm=llm,
    prompt=PromptTemplate(
        template="Based on the intent '{intent}', answer the user question: {query}",
        input_variables=["intent", "query"]
    )
)

# Combine chain calls
intent = intent_chain.run(query)
final_response = response_chain.run(intent=intent, query=query)

2. Routing

The routing pattern distributes requests to different processing paths based on input characteristics. This is a key pattern for achieving specialized processing and efficiency optimization.

Core Value:

  • Specialized Processing: Different types of problems are handled by the most capable processing unit
  • Resource Optimization: Simple problems are handled quickly, complex problems are analyzed in depth
  • Load Balancing: Distribute request pressure and improve system throughput

Routing Strategies:

  1. Rule-Based Routing: Keyword matching, question type classification
  2. Model-Based Routing: Use classification models to determine input type
  3. Embedding-Based Routing: Calculate semantic similarity for matching

3. Parallelization

The parallelization pattern improves efficiency and throughput by executing multiple tasks simultaneously. This is especially effective when handling independent subtasks.

Two Patterns:

a) Divergent Parallelization:

Single Input → Multiple Parallel Processing → Result Aggregation
Example: Simultaneously summarizing, sentiment-analyzing, and keyword-extracting an article

b) Convergent Parallelization:

Multiple Inputs → Single Processing → Aggregated Results
Example: Multi-source information comprehensive judgment, multi-angle analysis integration
from langchain.chains import ParallelChain

# Execute multiple independent tasks in parallel
parallel_result = ParallelChain(
    chains=[summary_chain, sentiment_chain, keyword_chain],
    verbose=True
).run(input_document)

4. Reflection

The reflection pattern enables Agents to evaluate their own outputs, identify errors, and self-improve. This is a key mechanism for achieving high-quality outputs.

Reflection Mechanism:

  1. Self-Check Output: Check the consistency and accuracy of output
  2. Multi-Angle Verification: Verify results from different dimensions
  3. Iterative Improvement: Continuously optimize output based on feedback

Code Framework:

class ReflectiveAgent:
    def __init__(self, llm):
        self.llm = llm
        self.max_iterations = 3

    def generate_with_reflection(self, task):
        # Initial generation
        output = self.generate(task)

        # Reflection loop
        for iteration in range(self.max_iterations):
            # Evaluate output quality
            evaluation = self.evaluate(task, output)

            if evaluation["passed"]:
                return output

            # Improve based on feedback
            output = self.improve(task, output, evaluation["feedback"])

        return output

5. Tool Use

The tool use pattern enables Agents to call external tools and APIs, extending their capability boundaries. This is key to achieving truly intelligent behavior.

Common Tool Types:

  • Search Tools: Google Search, Bing Search, Wikipedia Query
  • Code Execution: Python Interpreter, Code Sandbox
  • Database Queries: SQL Queries, Vector Database Retrieval
  • File Operations: Read, Write, Edit Documents
  • API Calls: Weather Query, Map Services, Payment Interfaces
from langchain.agents import initialize_agent, Tool

# Define tools
tools = [
    Tool(
        name="web_search",
        func=search_api.run,
        description="Tool for searching latest information"
    ),
    Tool(
        name="calculator",
        func=calculate,
        description="Tool for mathematical calculations"
    ),
    Tool(
        name="knowledge_base",
        func=query_kb.run,
        description="Tool for querying internal knowledge base"
    )
]

# Initialize Agent
agent = initialize_agent(
    tools,
    llm,
    agent="zero-shot-react-description",
    verbose=True
)

6. Planning

The planning pattern enables Agents to decompose complex tasks into executable step sequences and execute them according to plan. This is the core capability for achieving autonomous behavior.

Planning Process:

  1. Goal Understanding: Clarify the final goal
  2. Task Decomposition: Decompose the goal into subtasks
  3. Dependency Analysis: Determine dependencies between tasks
  4. Execution Scheduling: Execute tasks according to plan
  5. Dynamic Adjustment: Adjust plan based on execution results
class PlanningAgent:
    def create_plan(self, goal):
        # Use LLM to generate task plan
        prompt = f"""
        Goal: {goal}

        Please decompose this goal into specific execution steps,
        and explain the input, output, and dependencies of each step.
        """

        plan = self.llm.generate(prompt)

        # Parse plan and build execution graph
        return self.build_execution_graph(plan)

    def execute_plan(self, plan):
        for step in plan.steps:
            if self.can_execute(step):
                self.execute(step)
            else:
                # Handle dependencies not being met
                self.wait_for_dependencies(step)

7. Multi-Agent

Multi-Agent is the most advanced core pattern, allowing multiple specialized agents to collaborate and jointly solve complex problems.

Collaboration Modes:

  1. Hierarchical Structure: One main Agent coordinates multiple sub-agents
  2. Equal Collaboration: Multiple Agents work as equals, collaborating to solve problems
  3. Competition Mechanism: Multiple Agents compete for resources or propose the best solution

Framework Example:

# Using CrewAI for multi-agent collaboration
from crewai import Agent, Task, Crew

# Define specialized Agents
researcher = Agent(
    role="Researcher",
    goal="Provide accurate and comprehensive research information",
    backstory="Professional market researcher skilled in data collection and analysis"
)

analyst = Agent(
    role="Analyst",
    goal="Provide strategic recommendations based on research data",
    backstory="Senior strategic analyst with extensive industry experience"
)

writer = Agent(
    role="Writer",
    goal="Transform analysis results into clear reports",
    backstory="Professional business writer skilled in data visualization"
)

# Create tasks
research_task = Task(description="Research market trends", agent=researcher)
analysis_task = Task(description="Analyze competitive landscape", agent=analyst)
writing_task = Task(description="Write report", agent=writer)

# Form team and execute
crew = Crew(
    agents=[researcher, analyst, writer],
    tasks=[research_task, analysis_task, writing_task],
    process="hierarchical"
)

result = crew.kickoff()

Part 2: Advanced Patterns (Chapters 8-11)

8. Memory Management

Memory management enables Agents to maintain context across conversations, remember important information, and effectively utilize historical data.

Memory Types:

  • Short-Term Memory: Current conversation context
  • Long-Term Memory: Persistently stored knowledge points
  • Episodic Memory: Records of specific experiences and events
  • Semantic Memory: Structured and generalized knowledge

9. Learning Adaptation

The learning adaptation pattern enables Agents to learn from experience and continuously improve their performance.

Adaptation Mechanisms:

  • Few-Shot Learning: Quickly learn from a few examples
  • Reinforcement Learning: Optimize behavior through reward signals
  • Active Learning: Selective labeling and learning

10. MCP Protocol (Model Context Protocol)

MCP is a standardized protocol for context exchange and function invocation between Agents and external systems.

Core Concepts:

  • Context Injection: Inject external information into model context
  • Tool Registration: Standardized tool discovery and invocation mechanism
  • Result Callback: Feedback execution results to Agent

11. Goal Monitoring

Goal monitoring enables Agents to track task progress, identify deviations, and correct when drifting from goals.

Monitoring Dimensions:

  • Progress Tracking: Task completion monitoring
  • Quality Monitoring: Output quality evaluation
  • Risk Warning: Identify potential issues and risks

Part 3: Production Patterns (Chapters 12-14)

12. Exception Handling

Exception handling in production environments ensures system stability and reliability.

Exception Classification:

  • Input Exceptions: Format errors, invalid inputs
  • Processing Exceptions: Timeout, resource exhaustion
  • Output Exceptions: Results not meeting expectations
  • System Exceptions: Service unavailable, permission issues

13. Human-Agent Collaboration

The human-agent collaboration pattern finds the optimal balance between automation and human intervention.

Collaboration Modes:

  1. Human-in-the-loop: Critical decisions confirmed by humans
  2. Human-on-the-loop: Humans monitor system operation
  3. Human-at-the-end: Results finally reviewed by humans

14. RAG (Retrieval-Augmented Generation)

RAG combines the advantages of retrieval and generation, enabling Agents to leverage external knowledge bases.

RAG Process:

User Query → Retrieve Relevant Documents → Add Documents to Context → Generate Response

Part 4: Enterprise Patterns (Chapters 15-21)

Enterprise patterns cover advanced capabilities required for large-scale deployment:

  • Agent Communication: Efficient communication protocols between Agents
  • Resource Optimization: Computing resource and cost optimization strategies
  • Reasoning Techniques: Efficient reasoning and model optimization techniques
  • Security Guardrails: Preventing misuse and harmful outputs
  • Evaluation Monitoring: Continuous system performance monitoring and evaluation

Frameworks and Tools

LangChain

LangChain is one of the most popular Agent building frameworks, providing rich components and tools.

Core Advantages:

  • Modular design, flexible combination
  • Rich tool integrations
  • Powerful chain calling capabilities
  • Active community support

Applicable Scenarios:

  • Rapid prototype development
  • Complex chain processing
  • RAG application building

AutoGPT

AutoGPT is a representative of autonomous Agents, demonstrating AI Agents' ability to autonomously complete complex tasks.

Core Features:

  • Goal-driven autonomous execution
  • Automatic subtask decomposition
  • Introspection mechanism
  • Persistent memory

AutoGen

AutoGen is a multi-agent collaboration framework developed by Microsoft.

Core Advantages:

  • Native multi-agent support
  • Flexible conversation modes
  • Code execution capabilities
  • Human interaction support

CrewAI

CrewAI focuses on multi-agent collaboration, especially suitable for task decomposition and parallel execution.

Core Features:

  • Role-based Agent design
  • Task assignment and dependency management
  • Hierarchical and parallel processing
  • Easy-to-use API

Key Takeaways

Core Points

  1. Value of Design Patterns: Agentic Design Patterns provides a set of proven solutions to help developers avoid reinventing the wheel.

  2. Progressive Complexity: From simple prompt chaining to complex multi-agent systems, the learning path is well-designed with progressive layers.

  3. Theory and Practice Combined: Each pattern has corresponding code implementations and Jupyter Notebooks, supporting learning by doing.

  4. Framework Independence: Although the project uses multiple frameworks for demonstration, core concepts apply to any Agent framework.

  5. Community-Driven: Open-source features enable global developers to contribute code and share experiences.

Practical Recommendations

  • Start Small: Understand core patterns first, then gradually try advanced patterns
  • Hands-on Practice: Use Jupyter Notebooks to run example code
  • Choose the Right Framework: Select the most suitable framework based on project needs
  • Focus on Security: Always consider security guardrails in production environments
  • Continuous Learning: AI is evolving rapidly, keep learning and updating

Quick Start

Environment Setup

# Clone the repository
git clone https://github.com/evoiz/Agentic-Design-Patterns.git
cd Agentic-Design-Patterns

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Linux/Mac
# or
.\venv\Scripts\activate  # Windows

# Install dependencies
pip install jupyter notebook pandas numpy openai langchain

Launch Jupyter Notebook

jupyter notebook

Then open the Notebook in your browser and follow the tutorials to learn and practice step by step.


Conclusion

The Agentic Design Patterns project provides a comprehensive learning guide for AI Agent development. By systematically introducing design patterns from basic to advanced levels, it helps developers build smarter and more reliable AI systems. Whether you're new to AI or an experienced developer, this project is worth exploring in depth.

The charitable nature of the project adds social value — while learning knowledge, you're also contributing to the well-being of children around the world.

Project Link: https://github.com/evoiz/Agentic-Design-Patterns

Reference Book: "Agentic Design Patterns: A Hands-On Guide to Building Intelligent Systems" by Antonio Gulli