Skip to content

firstpixel/agentic_workflow

Repository files navigation

Agentic AI Workflow Framework

A comprehensive Python framework implementing 20 foundational agentic AI design patterns for building sophisticated multi-agent systems with orchestrated workflows.

๐Ÿ“‹ Table of Contents

  1. Overview
  2. 20 Agentic AI Design Patterns
  3. Project Architecture
  4. Installation & Setup
  5. Usage Examples
  6. Pattern Implementations
  7. Core Components
  8. Testing
  9. Contributing

๐ŸŽฏ Overview

This framework provides a production-ready implementation of 20 essential agentic AI design patterns, enabling developers to build complex multi-agent systems with features like:

  • Orchestrated Workflows: Chain agents with complex control flow
  • Memory Management: Short-term, episodic, and long-term storage
  • Error Handling: Retry mechanisms and fallback strategies
  • Safety & Guardrails: Input/output filtering and content moderation
  • Performance Monitoring: Metrics collection and evaluation
  • Human-in-the-Loop: Approval gates and intervention points

๐Ÿ”„ 20 Agentic AI Design Patterns

1. Prompt Chaining

Sequential task decomposition with validation

[User Request] โ†’ [Step A] โ†’ [Step B] โ†’ [Step C] โ†’ [Final Result]

Implementation: Sequential workflow execution via WorkflowManager

  • Files: src/core/workflow_manager.py, tests/test_workflow_basic.py
  • Example: Multi-step document generation pipeline

2. Routing

Intelligent request distribution to specialized agents

[User Request] โ†’ [Router Agent] โ†’ [Billing Agent]
                              โ†’ [Support Agent] 
                              โ†’ [Sales Agent]

Implementation: SwitchAgent with rule-based and LLM-based routing

  • Files: src/agents/switch_agent.py, tests/test_switch_agent.py
  • Example: Customer service request classification

3. Parallelization

Concurrent execution of independent subtasks

[Large Task] โ†’ [Subtask A] โ†’ [Agent A] โ†’ [Result A]
             โ†’ [Subtask B] โ†’ [Agent B] โ†’ [Result B] โ†’ [Combine Results]

Implementation: FanOutAgent and JoinAgent for parallel execution

  • Files: src/agents/fanout_agent.py, src/agents/join_agent.py
  • Example: Parallel document analysis and summarization

4. Reflection

Iterative improvement through criticism and revision

[Agent] โ†’ [Draft] โ†’ [Critic Agent] โ†’ [Feedback] โ†’ [Revision] โ†’ [Final Version]

Implementation: CriticAgent with configurable feedback loops

  • Files: src/agents/critic_agent.py, tests/test_critic_agent.py
  • Example: Iterative content refinement

5. Tool Use

External tool integration for specialized capabilities

[Agent] โ†’ [Tool Selection] โ†’ [Query Tool] โ†’ [Tool Output] โ†’ [Incorporate Results]

Implementation: ToolRunnerAgent with configurable tool specifications

  • Files: src/agents/tool_runner.py, src/tools/duckduckgo_scraper.py
  • Example: Web search integration for real-time information

6. Planning

Multi-step plan generation and execution

[Goal] โ†’ [Planning Agent] โ†’ [Step 1] โ†’ [Step 2] โ†’ [Step 3]
                         โ†’ [Tool: Web Search] โ†’ [Agent: Analyzer] โ†’ [Report Generator]

Implementation: PlannerAgent with decomposition, refinement, and evaluation

  • Files: src/agents/planner_agent.py, src/app/flows_planner.py
  • Example: Research project planning and execution

6.1 Code Execution & File Creation

Automated code generation, file creation, and safe script execution

[Planning Agent] โ†’ [CodeExecutor Agent] โ†’ [File Creation]
                                       โ†’ [Script Execution]
                                       โ†’ [Testing & Validation]

Implementation: CodeExecutorAgent with safe sandboxed execution

  • Files: src/agents/code_executor_agent.py, src/app/flow_planner_coder.py
  • Features:
    • Automated file and folder structure creation
    • Python, JavaScript, and bash script execution
    • Safety checks and path validation
    • Basic syntax testing and validation
  • Example: Complete project generation from high-level requirements

7. Multi-Agent Collaboration

Coordinated teamwork with shared memory

[Manager Agent] โ†’ [Agent A] โ†’ [Common Memory]
               โ†’ [Agent B] โ†—
               โ†’ [Agent C] โ†—

Implementation: WorkflowManager with shared MemoryManager

  • Files: src/core/workflow_manager.py, src/memory/memory_manager.py
  • Example: Collaborative document creation

8. Memory Management

Multi-tier information storage and retrieval

[Info] โ†’ [Memory Manager] โ†’ [Short-Term (Conversation)]
                         โ†’ [Episodic (Events)]
                         โ†’ [Long-Term (Knowledge)]

Implementation: MemoryManager with MongoDB and Qdrant backends

  • Files: src/memory/memory_manager.py, src/memory/mongo_stm.py, src/memory/qdrant_store.py
  • Example: Persistent conversation and knowledge storage

9. Learning and Adaptation

Continuous improvement through feedback

[Output] โ†’ [Feedback] โ†’ [Update Prompts/Policies] โ†’ [Improved Agent]

Implementation: Feedback collection and prompt updating mechanisms

  • Files: src/eval/evaluation.py, src/core/agent.py (overrides system)
  • Example: Model performance optimization

10. Goal Setting and Monitoring

Progress tracking and plan adjustment

[Goal] โ†’ [Define Metrics] โ†’ [Monitor Progress] โ†’ [Adjust Plan]

Implementation: MetricsCollector and workflow monitoring

  • Files: src/eval/metrics.py, src/core/workflow_manager.py
  • Example: Task completion tracking

11. Exception Handling and Recovery

Robust error management and fallback strategies

[Task] โ†’ [Error] โ†’ [Retry] โ†’ [Fallback Method] โ†’ [Human Intervention]

Implementation: Retry policies and fallback nodes in workflow

  • Files: src/core/workflow_manager.py, src/app/flows_retries.py
  • Example: Network failure recovery with alternative data sources

12. Human in the Loop

Strategic human intervention points

[Workflow] โ†’ [Critical Decision] โ†’ [Human Review] โ†’ [Continue]

Implementation: ApprovalGateAgent with EventBus integration

  • Files: src/agents/approval_gate.py, src/core/event_bus.py
  • Example: Content approval before publication

13. Knowledge Retrieval (RAG)

Grounded responses using external knowledge

[Query] โ†’ [Embeddings] โ†’ [Vector DB] โ†’ [Relevant Docs] โ†’ [Grounded Response]

Implementation: RAGRetrieverAgent with vector similarity search

  • Files: src/agents/rag_retriever.py, tests/test_pattern_rag.py
  • Example: Document-grounded question answering

14. Inter-Agent Communication

Structured messaging between agents

[Agent A] โ†’ [Message (ID, Protocol, Data)] โ†’ [Agent B]

Implementation: EventBus system for agent communication

  • Files: src/core/event_bus.py, message passing in workflows
  • Example: Notification and coordination between agents

15. Resource-Aware Optimization

Cost-effective model selection

[Task] โ†’ [Analyze Complexity] โ†’ [Cheap Model] / [Powerful Model]

Implementation: ModelSelectorAgent with complexity analysis

  • Files: src/agents/model_selector.py, tests/test_pattern_model_selector.py
  • Example: Dynamic model selection based on task complexity

16. Reasoning Techniques

Specialized problem-solving approaches

[Problem] โ†’ [Select Method] โ†’ [Chain-of-Thought] / [Tree-of-Thought] / [Debate]

Implementation: Configurable reasoning prompts and techniques

  • Files: Specialized prompts in prompts/ directory
  • Example: Mathematical problem solving with step-by-step reasoning

17. Evaluation and Monitoring

Continuous performance assessment

[Model] โ†’ [Pre-deployment Tests] โ†’ [Deploy] โ†’ [Continuous Monitoring]

Implementation: EvaluationRunner and MetricsCollector

  • Files: src/eval/evaluation.py, src/eval/metrics.py
  • Example: A/B testing and performance tracking

18. Guardrails and Safety Patterns

Content safety and compliance checking

[Input] โ†’ [Guardrail System] โ†’ [Block/Allow] โ†’ [Agent] โ†’ [Output Validation]

Implementation: GuardrailsAgent with PII detection and content moderation

  • Files: src/agents/guardrails_agent.py, src/guardrails/guardrails.py
  • Example: Content filtering and PII redaction

19. Prioritization

Task ordering and resource allocation

[Tasks] โ†’ [Prioritization Engine] โ†’ [Ordered Queue] โ†’ [Execute by Priority]

Implementation: Task scoring and queue management

  • Files: Workflow priority handling in WorkflowManager
  • Example: Customer support ticket prioritization

20. Exploration and Discovery

Knowledge space exploration and hypothesis generation

[Topic] โ†’ [Exploration Agent] โ†’ [Pattern Identification] โ†’ [Hypothesis Generation]

Implementation: Search agents and pattern analysis

  • Files: src/tools/duckduckgo_scraper.py, exploration workflows
  • Example: Market research and trend analysis

๐Ÿ—๏ธ Project Architecture

src/
โ”œโ”€โ”€ core/                    # Core framework components
โ”‚   โ”œโ”€โ”€ agent.py            # Base agent classes and LLM integration
โ”‚   โ”œโ”€โ”€ types.py            # Message, Result, and control structures
โ”‚   โ”œโ”€โ”€ workflow_manager.py # Orchestration and flow control
โ”‚   โ”œโ”€โ”€ event_bus.py        # Inter-agent communication
โ”‚   โ””โ”€โ”€ utils.py            # Utility functions
โ”œโ”€โ”€ agents/                  # Specialized agent implementations
โ”‚   โ”œโ”€โ”€ approval_gate.py    # Human-in-the-loop approval
โ”‚   โ”œโ”€โ”€ critic_agent.py     # Reflection and feedback
โ”‚   โ”œโ”€โ”€ fanout_agent.py     # Parallel task distribution
โ”‚   โ”œโ”€โ”€ guardrails_agent.py # Safety and content filtering
โ”‚   โ”œโ”€โ”€ join_agent.py       # Result aggregation
โ”‚   โ”œโ”€โ”€ model_selector.py   # Dynamic model selection
โ”‚   โ”œโ”€โ”€ planner_agent.py    # Multi-step planning
โ”‚   โ”œโ”€โ”€ prompt_switcher.py  # Dynamic prompt selection
โ”‚   โ”œโ”€โ”€ rag_retriever.py    # Knowledge retrieval
โ”‚   โ”œโ”€โ”€ switch_agent.py     # Request routing
โ”‚   โ””โ”€โ”€ tool_runner.py      # External tool integration
โ”œโ”€โ”€ memory/                  # Memory management system
โ”‚   โ”œโ”€โ”€ memory_manager.py   # Multi-tier storage coordination
โ”‚   โ”œโ”€โ”€ mongo_stm.py        # Short-term memory (MongoDB)
โ”‚   โ””โ”€โ”€ qdrant_store.py     # Vector storage (Qdrant)
โ”œโ”€โ”€ eval/                    # Evaluation and monitoring
โ”‚   โ”œโ”€โ”€ evaluation.py       # Test case evaluation
โ”‚   โ””โ”€โ”€ metrics.py          # Performance metrics collection
โ”œโ”€โ”€ tools/                   # External tool integrations
โ”‚   โ””โ”€โ”€ duckduckgo_scraper.py # Web search capability
โ””โ”€โ”€ app/                     # Application flows and demos
    โ”œโ”€โ”€ main.py             # Demo applications
    โ”œโ”€โ”€ flows.py            # Workflow definitions
    โ”œโ”€โ”€ flows_planner.py    # Planning demonstrations
    โ””โ”€โ”€ flows_retries.py    # Error handling examples

Key Components

1. WorkflowManager (src/core/workflow_manager.py)

  • Purpose: Orchestrates multi-agent workflows with complex control flow
  • Features:
    • Graph-based workflow execution
    • Retry and fallback mechanisms
    • Node state management
    • Parallel execution support
    • Error recovery strategies

2. BaseAgent & LLMAgent (src/core/agent.py)

  • Purpose: Foundation for all agent implementations
  • Features:
    • Configurable retry logic
    • Prompt management system
    • Model configuration
    • Metrics collection
    • History management

3. MemoryManager (src/memory/memory_manager.py)

  • Purpose: Multi-tier memory system for agents
  • Features:
    • Short-term conversational memory (MongoDB)
    • Long-term knowledge storage (Qdrant vectors)
    • Episodic event memory
    • Automatic memory lifecycle management

4. EventBus (src/core/event_bus.py)

  • Purpose: Enables inter-agent communication and coordination
  • Features:
    • Publish/subscribe messaging
    • Event filtering and routing
    • Asynchronous communication
    • Human-in-the-loop integration

๐Ÿš€ Installation & Setup

Prerequisites

  • Python 3.8+
  • Ollama (for local LLM inference)
  • MongoDB (for short-term memory)
  • Qdrant (for vector storage)

Installation

  1. Clone the repository:
git clone <repository-url>
cd agentic_workflow
  1. Install dependencies:
pip install -r requirements.txt
  1. Set up external services:

Ollama (Local LLM):

curl -fsSL https://ollama.com/install.sh | sh
ollama pull llama3.2:latest

MongoDB (Docker):

docker run -d --name mongodb -p 27017:27017 mongo:latest

Qdrant (Docker):

docker run -d --name qdrant -p 6333:6333 qdrant/qdrant:latest
  1. Configure environment: Create .env file:
OLLAMA_MODEL=llama3.2:latest
MONGODB_URL=mongodb://localhost:27017
QDRANT_URL=http://localhost:6333
PROMPT_DIR=./prompts

๐Ÿ“– Usage Examples

Basic Agent Usage

from src.core.agent import AgentConfig, LLMAgent
from src.core.types import Message

# Create a simple writing agent
writer = LLMAgent(AgentConfig(
    name="Writer",
    prompt_file="tech_writer.md",
    model_config={"model": "llama3.2:latest", "temperature": 0.7}
))

# Execute the agent
message = Message(data={"user_prompt": "Write a brief intro to AI"})
result = writer.execute(message)
print(result.output)

Workflow Orchestration

from src.core.workflow_manager import WorkflowManager
from src.agents.critic_agent import CriticAgent

# Define workflow graph
graph = {
    "Writer": ["Critic"],
    "Critic": ["Writer"],  # Enables reflection loop
    "Writer": []  # Final output
}

# Create agents
writer = LLMAgent(AgentConfig(name="Writer", prompt_file="tech_writer.md"))
critic = CriticAgent(AgentConfig(name="Critic", prompt_file="critic_agent.md"))

# Create workflow
workflow = WorkflowManager(
    graph=graph,
    agents={"Writer": writer, "Critic": critic}
)

# Execute workflow
result = workflow.run(Message(data={"user_prompt": "Write about quantum computing"}))

RAG (Retrieval Augmented Generation)

from src.agents.rag_retriever import RAGRetrieverAgent
from src.memory.memory_manager import MemoryManager

# Setup memory system
memory = MemoryManager()

# Create RAG agent
rag_agent = RAGRetrieverAgent(AgentConfig(
    name="RAG",
    prompt_file="answer_with_context.md"
))

# Store knowledge
memory.store_long_term("AI History", "content", {"text": "AI was founded in 1956..."})

# Query with retrieval
message = Message(data={
    "user_prompt": "When was AI founded?",
    "memory_manager": memory
})
result = rag_agent.execute(message)

Code Execution and File Creation

The CodeExecutorAgent can create files and execute code automatically, with support for both mocked LLM responses (for fast development/testing) and real LLM integration.

Using with Real LLM Integration

from src.agents.code_executor_agent import CodeExecutorAgent
from src.app.flow_planner_coder import build_planner_coder_flow, demo_planner_coder

# Create code executor for automated file creation
executor = CodeExecutorAgent(AgentConfig(
    name="CodeExecutor",
    prompt_file="code_executor.md",
    model_config={
        "project_root": "./my_project",
        "enable_execution": True,
        "allowed_extensions": [".py", ".js", ".html", ".css", ".md"]
    }
))

# Use with planning flow for complete project generation
demo_planner_coder(
    "Create a Python calculator with tests",
    "calculator_project"
)

Demo Script with Dual Mode Support

The demo script supports both mocked (fast) and real LLM modes:

# Default: Use mocked LLM responses (no Ollama required)
python demo_code_executor.py

# Use real LLM via LLMAgent (requires Ollama)
python demo_code_executor.py --use-real-llm

# Or via environment variable
USE_REAL_LLM=1 python demo_code_executor.py

# Show help for all options
python demo_code_executor.py --help

Mock Mode (Default):

  • Fast execution, perfect for development and testing
  • No external dependencies (Ollama not required)
  • Predictable, consistent outputs for CI/CD

Real LLM Mode:

  • Uses actual LLM for dynamic code generation
  • Requires Ollama with a compatible model (e.g., llama3.2:latest)
  • Provides end-to-end validation of the CodeExecutorAgent

Human-in-the-Loop Approval

from src.agents.approval_gate import ApprovalGateAgent
from src.core.event_bus import get_event_bus

# Setup approval system
bus = get_event_bus()
approval_agent = ApprovalGateAgent(AgentConfig(
    name="ApprovalGate",
    prompt_file="approval_request.md"
))

# Request approval
message = Message(data={
    "content": "This content needs review before publication",
    "approval_type": "content_review"
})

# Agent will pause and wait for human decision via EventBus
result = approval_agent.execute(message)

Parallel Processing

from src.agents.fanout_agent import FanOutAgent
from src.agents.join_agent import JoinAgent

# Create parallel workflow
fanout = FanOutAgent(AgentConfig(name="Fanout"))
join = JoinAgent(AgentConfig(name="Join"))

# Define parallel tasks
graph = {
    "Fanout": ["TaskA", "TaskB", "TaskC"],
    "TaskA": ["Join"],
    "TaskB": ["Join"], 
    "TaskC": ["Join"],
    "Join": []
}

workflow = WorkflowManager(graph=graph, agents={...})

๐ŸŽฎ Demo Scripts

The framework includes several demonstration scripts to help you get started:

Pattern Demonstrations (demo_patterns.py)

Interactive demonstrations of the 20 agentic AI design patterns:

# Run all patterns (requires Ollama)
source .venv/bin/activate
python demo_patterns.py

# Run specific pattern
python demo_patterns.py 1    # Prompt Chaining
python demo_patterns.py 2    # Routing
python demo_patterns.py 3    # Parallelization
python demo_patterns.py 4    # Reflection
python demo_patterns.py 5    # Tool Use

# Use helper script (auto-activates .venv)
./run_demo.sh               # All patterns
./run_demo.sh 1            # Specific pattern

Code Executor Demonstration (demo_code_executor.py)

Demonstrates the CodeExecutorAgent without requiring Ollama by using mocked LLM responses:

# Run without LLM (uses mocked responses)
python demo_code_executor.py

# Available demonstrations:
# 1. Python Calculator - Creates and runs a calculator program
# 2. React Todo App - Generates a complete React application
# 3. Node.js API Server - Creates an Express.js REST API
# 4. Static Website - Builds an HTML/CSS/JS website

Features demonstrated:

  • Safe Code Execution: Sandboxed execution within project directories
  • Multi-language Support: Python, JavaScript, HTML/CSS, Node.js
  • File Management: Automatic project structure creation
  • Security: Path validation and execution timeouts
  • Integration: Works with PlannerFlow for automated development

Debug Workflow (debug_workflow.py)

Development and debugging utilities for workflow testing:

python debug_workflow.py

๐Ÿงช Testing

The framework includes comprehensive tests for each pattern:

# Run all tests
pytest tests/

# Test specific patterns
pytest tests/test_pattern_guardrails.py
pytest tests/test_pattern_rag.py
pytest tests/test_pattern_model_selector.py

# Integration tests
pytest tests/test_workflow_basic.py
pytest tests/test_memory_components.py

Test Categories

  • Pattern Tests: Validate each of the 20 design patterns
  • Integration Tests: Test component interactions
  • Performance Tests: Measure system performance
  • Safety Tests: Verify guardrails and error handling

๐Ÿ”ง Configuration

Agent Configuration

@dataclass
class AgentConfig:
    name: str
    retries: int = 0                    # Retry attempts on failure
    retry_backoff_sec: float = 0.0      # Delay between retries
    model_config: Dict[str, Any]        # Model parameters
    prompt_file: str                    # Prompt template path
    tools: List[Any]                    # Available tools
    history_max_messages: int = 8       # Conversation history limit

Memory Configuration

# MongoDB for short-term memory
MONGODB_URL = "mongodb://localhost:27017"

# Qdrant for vector storage
QDRANT_URL = "http://localhost:6333"
QDRANT_COLLECTION = "knowledge_base"

Model Configuration

model_config = {
    "model": "llama3.2:latest",
    "temperature": 0.7,
    "top_p": 0.9,
    "max_tokens": 1000
}

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature-name
  3. Add tests for new functionality
  4. Run the test suite: pytest tests/
  5. Submit a pull request

Development Guidelines

  • Follow the existing code structure and naming conventions
  • Add comprehensive tests for new patterns or agents
  • Update documentation for new features
  • Use type hints and docstrings
  • Ensure all tests pass before submitting

๐Ÿ“ License

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

๐Ÿ™‹โ€โ™‚๏ธ Support

For questions, issues, or contributions:

  • Issues: Use GitHub Issues for bug reports
  • Discussions: Use GitHub Discussions for questions
  • Documentation: Check the tests/ directory for usage examples

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

This framework represents a comprehensive implementation of proven agentic AI patterns, providing a solid foundation for building sophisticated multi-agent systems. Each pattern is carefully implemented with production considerations including error handling, monitoring, and scalability.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors