Skip to content

gtesei/agentic_design_patterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

43 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿค– Agentic Design Patterns

Python 3.11+ LangChain LangGraph License: MIT

Transform your AI applications from simple prompts to sophisticated intelligent systems.

A comprehensive, hands-on collection of design patterns for building robust agentic AI systems. Each pattern is implemented with real-world examples and detailed documentation to help you architect scalable, maintainable AI applications.


๐Ÿ“š Foundational Patterns

1๏ธโƒฃ Prompt Chaining

Break complex tasks into sequential, manageable steps

# Transform a monolithic prompt into a chain of specialized prompts
input โ†’ extract_data โ†’ transform โ†’ validate โ†’ final_output

When to use:

  • Multi-step transformations (data extraction โ†’ analysis โ†’ formatting)
  • Tasks requiring intermediate validation
  • Complex workflows that benefit from decomposition

Key benefits:

  • ๐ŸŽฏ Better accuracy through focused prompts
  • ๐Ÿ” Easier debugging with visible intermediate steps
  • ๐Ÿ”„ Reusable components across workflows

๐Ÿ“– Learn More โ†’


2๏ธโƒฃ Routing

Intelligently direct queries to specialized handlers

# Dynamic routing based on query classification
user_query โ†’ classifier โ†’ [technical_expert | sales_agent | support_bot]

When to use:

  • Multi-domain applications (support, sales, technical)
  • Specialized model selection (fast/cheap vs. slow/accurate)
  • Intent-based workflows requiring different processing paths

Key benefits:

  • ๐Ÿ’ฐ Cost optimization (use expensive models only when needed)
  • โšก Performance gains (route simple queries to fast handlers)
  • ๐ŸŽจ Specialized handling (domain experts for domain queries)

๐Ÿ“– Learn More โ†’


3๏ธโƒฃ Parallelization

Execute independent operations simultaneously for dramatic speedups

# Sequential: 15 seconds          # Parallel: 5 seconds
task_a(5s) โ†’                      task_a(5s) โ†˜
task_b(5s) โ†’          vs.         task_b(5s) โ†’ combine โ†’ output
task_c(5s) โ†’ output               task_c(5s) โ†—

When to use:

  • Multiple API calls (search engines, databases, external services)
  • Parallel data processing (analyze multiple documents)
  • Multi-source research or content generation

Key benefits:

  • โšก 2-10x faster execution for I/O-bound tasks
  • ๐Ÿ“ˆ Better resource utilization
  • ๐Ÿš€ Improved user experience through reduced latency

๐Ÿ“– Learn More โ†’


4๏ธโƒฃ Reflection

Iteratively improve outputs through systematic critique and refinement

# Single-shot: 5/10 quality        # With reflection: 8.5/10 quality
input โ†’ generate โ†’ done            input โ†’ generate โ†’ critique โ†’ 
                                          refine โ†’ critique โ†’ final

When to use:

  • High-stakes content (code, legal docs, published articles)
  • Complex reasoning tasks (logic puzzles, strategic planning)
  • Quality-critical applications where "good enough" isn't enough

Key benefits:

  • ๐ŸŽฏ 50-70% higher quality scores
  • ๐Ÿ” Systematic error detection and correction
  • ๐Ÿง  Self-improving outputs without human intervention

Trade-offs:

  • โš ๏ธ 3-5x higher token costs
  • โฑ๏ธ 4-8x longer execution time

๐Ÿ“– Learn More โ†’


5๏ธโƒฃ Tool Use

Enable LLMs to interact with external systems and APIs

# Without tools: Limited to training data
# With tools: Access real-time data and take actions
user_query โ†’ LLM decides โ†’ call_weather_api(location) โ†’ integrate_result โ†’ response

When to use:

  • Real-time data retrieval (weather, stock prices, news)
  • Private/proprietary data access (databases, CRM systems)
  • Precise calculations or code execution
  • External actions (send emails, update records, control devices)

Key benefits:

  • ๐ŸŒ Access to live, dynamic information
  • ๐ŸŽฏ Precise calculations and data validation
  • ๐Ÿ”ง Integration with existing enterprise systems
  • ๐Ÿ’ฐ Reduced token costs (fetch vs. embed in prompts)

Trade-offs:

  • โš ๏ธ Added latency per tool call
  • ๐Ÿ”’ Security considerations (authentication, validation)

๐Ÿ“– Learn More โ†’


6๏ธโƒฃ Planning

Decompose complex goals into structured, executable action plans

# Without planning: Reactive, incomplete execution
# With planning: Strategic breakdown and systematic execution
complex_goal โ†’ analyze โ†’ decompose โ†’ plan_steps โ†’ execute_sequentially โ†’ final_result

When to use:

  • Multi-step workflows requiring orchestration (research reports, data pipelines)
  • Tasks with interdependent operations
  • Complex problem-solving requiring strategic thinking
  • Workflow automation (onboarding, procurement, project setup)

Key benefits:

  • ๐ŸŽฏ Structured approach to complex objectives
  • ๐Ÿง  Strategic thinking vs. reactive responses
  • ๐Ÿ”„ Adaptability through dynamic replanning
  • ๐Ÿ“Š Transparency into execution strategy

Trade-offs:

  • โš ๏ธ Planning overhead (+20-40% tokens, 5-15s latency)
  • ๐Ÿ› ๏ธ Requires sophisticated state management

๐Ÿ“– Learn More โ†’


7๏ธโƒฃ Multi-Agent Collaboration

Coordinate multiple specialized agents to solve complex tasks

# Agents as a team: specialize roles + coordinate communication
user_goal โ†’ manager/planner โ†’ [researcher | coder | designer | writer | reviewer] โ†’ synthesize โ†’ final_output

When to use:

  • Complex tasks requiring diverse expertise (research + writing + QA)
  • Workflows with distinct stages (research โ†’ draft โ†’ edit โ†’ package)
  • Tool-specialized roles (web search, code execution, image generation)
  • Quality-critical pipelines (critic/reviewer loops)

Key benefits:

  • ๐Ÿงฉ Modularity: build and improve one role at a time
  • ๐Ÿ›ก๏ธ Robustness: reviewers catch errors / reduce hallucinations
  • โšก Parallelism: split independent workstreams for speed
  • โ™ป๏ธ Reuse: agents can be reused across multiple products

Common collaboration models:

  • Sequential handoffs (linear pipeline)
  • Supervisor/manager orchestration (hierarchical)
  • Parallel workstreams (merge results)
  • Debate/consensus (evaluate options)
  • Criticโ€“reviewer (quality enforcement)
  • Network/all-to-all (exploratory, less predictable)
  • Custom hybrids (fit domain constraints)

๐Ÿ“– Learn More โ†’


๐Ÿš€ Quick Start

Prerequisites

# Python 3.11 or higher
python --version

# Install uv 
curl -LsSf https://astral.sh/uv/install.sh | sh

Installation

# Clone the repository
git clone https://github.com/yourusername/agentic_design_patterns.git
cd agentic_design_patterns

# Set up environment
echo "OPENAI_API_KEY=your_api_key_here" > .env

# Install dependencies (using uv)
uv sync

Run Your First Pattern

# Try prompt chaining
cd foundational_design_patterns/1_prompt_chain
bash run.sh

# Try routing
cd ../2_routing
uv run python src/routing_example.py

# Try parallelization
cd ../3_parallelization
bash run.sh

# Try reflection (stateful loops)
cd ../4_reflection
bash run.sh

๐Ÿ—บ๏ธ Pattern Selection Guide

Choose Your Pattern Based on Your Needs:

Need speed? โ†’ Start with Routing and Parallelization

Need quality? โ†’ Use Prompt Chaining + Reflection

Need cost optimization? โ†’ Implement Routing to avoid expensive models

Need both speed AND quality? โ†’ Combine Parallelization + Prompt Chaining

Complex multi-step workflow? โ†’ Prompt Chaining is your foundation

Independent concurrent tasks? โ†’ Parallelization will give you massive speedups

High-stakes output? โ†’ Reflection is worth the cost

External system integration? โ†’ Tool Use enables real-world interaction

Multi-step automation requiring orchestration? โ†’ Planning provides strategic execution

Need multiple roles working together? โ†’ Multi-Agent Collaboration (specialists + coordinator)


๐Ÿ—๏ธ Repository Structure

agentic_design_patterns/
โ”œโ”€โ”€ foundational_design_patterns/
โ”‚   โ”œโ”€โ”€ 1_prompt_chain/
โ”‚   โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ chain_prompt.py            # Basic chaining
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ advanced_chain.py          # Complex workflows
โ”‚   โ”‚   โ”œโ”€โ”€ README.md                      # Pattern documentation
โ”‚   โ”‚   โ””โ”€โ”€ SKILL.md                       # Implementation guide
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ 2_routing/
โ”‚   โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ routing.py                 # Intent-based routing
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ semantic_routing.py        # Advanced routing
โ”‚   โ”‚   โ”œโ”€โ”€ README.md
โ”‚   โ”‚   โ””โ”€โ”€ SKILL.md
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ 3_parallelization/
โ”‚   โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ parallel_example.py        # LCEL parallelization
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ async_parallel.py          # Async operations
โ”‚   โ”‚   โ”œโ”€โ”€ README.md
โ”‚   โ”‚   โ””โ”€โ”€ SKILL.md
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ 4_reflection/
โ”‚       โ”œโ”€โ”€ src/
โ”‚       โ”‚   โ”œโ”€โ”€ reflection.py               # Single-step reflection
โ”‚       โ”‚   โ””โ”€โ”€ reflection_stateful_loop.py # Iterative refinement
โ”‚       โ”œโ”€โ”€ README.md
โ”‚       โ””โ”€โ”€ SKILL.md
...
...
โ”œโ”€โ”€ .env                                # Environment variables
โ”œโ”€โ”€ LICENSE                             # MIT License
โ””โ”€โ”€ README.md                           # This file

๐ŸŽ“ Learning Path

Beginner โ†’ Intermediate โ†’ Advanced

  1. Start here: Prompt Chaining - Foundation for everything
  2. Next: Routing - Learn to optimize model selection
  3. Then: Parallelization - Scale your applications
  4. Finally: Reflection - Master quality optimization
  5. Advanced: Multi-Agent Collaboration - Build coordinated agent teams

Each pattern builds on concepts from previous ones, so we recommend following this sequence.


๐Ÿ› ๏ธ Tech Stack


๐Ÿ”ฎ Coming Soon

We're actively developing additional patterns:

  • Retrieval-Augmented Generation (RAG) - Knowledge-grounded responses
  • Human-in-the-Loop - Interactive approval and refinement
  • Guardrails - Safety, compliance, and quality enforcement

Want a specific pattern? Open an issue and let us know!


๐Ÿ“– Resources

Official Documentation

Related Projects


๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


๐Ÿ™ Acknowledgments

This repository's structure and approach were inspired by:

Gullรญ, Antonio, Agentic Design Patterns: A Hands-On Guide to Building Intelligent Systems, Springer Nature Switzerland.

Andrew Ng, Agentic AI, DeepLearning.AI.

Special thanks to:

  • The LangChain team for building incredible tools
  • The open-source AI community for pushing the boundaries
  • All contributors who help improve these patterns

โญ Star History

If you find this repository helpful, please consider giving it a star! It helps others discover these patterns.

Star History Chart

Built with โค๏ธ for the AI developer community

โฌ† Back to Top

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •