A modular, learning-first repository for implementing and understanding five core LLM agent design patterns. Built with Python, LangGraph, and MCP integration.
| Pattern | Status | Learning Value | Production Ready |
|---|---|---|---|
| IEV (Intelligence-Extraction-Validation) | β COMPLETE | βββ Best starting point | βββ High |
| Evaluator-Optimizer (Draft-Critique-Refine) | π§ Basic | βββ Quality feedback loop | ββ Medium |
| Orchestrator (Task Delegation) | π§ Stub | βββ Complex workflows | β Foundation |
| Agentic RAG (Retrieval-Verification) | π§ Stub | ββ Research & fact-checking | β Foundation |
| System 2 (Thinking-Before-Acting) | π§ Stub | βββ Complex reasoning | β Foundation |
Legend: β = Fully implemented | π§ = Basic/stub implementation | β = Learning/production score
aistack-patterns/
β
βββ README.md # This file
βββ pyproject.toml # Project metadata & dependencies
βββ .env.example # Configuration template
β
βββ src/
β βββ patterns/
β β
β βββ __init__.py # Package exports
β β
β βββ core/ # β
SHARED INFRASTRUCTURE
β β βββ __init__.py
β β βββ llm_client.py # LLM wrapper (OpenAI, Anthropic, local Ollama)
β β βββ mcp_tools.py # Your aistack-mcp connector
β β βββ memory.py # Vector DB + context management
β β βββ types.py # Shared type definitions
β β βββ logger.py # Logging utilities
β β
β βββ patterns/ # β
THE 5 PATTERN IMPLEMENTATIONS
β β βββ __init__.py
β β β
β β βββ iev/ # Pattern 1: Intelligence-Extraction-Validation β
β β β βββ __init__.py
β β β βββ graph.py # State machine definition
β β β βββ prompts.py # Verification-specific prompts
β β β βββ example.py # Minimal working example
β β β βββ abstractions.py
β β β βββ nodes/ # Individual node implementations
β β β βββ strategies/ # JSON repair & validation strategies
β β β βββ workflows/ # SOLID workflow orchestration
β β β
β β βββ evaluator_optimizer/ # Pattern 2: Draft-Critique-Refine π§
β β β βββ __init__.py
β β β βββ graph.py
β β β βββ grader.py # Scoring & feedback logic
β β β βββ prompts.py
β β β βββ example.py
β β β
β β βββ orchestrator/ # Pattern 3: Orchestrator-Workers π§
β β β βββ __init__.py
β β β βββ orchestrator.py # Router logic
β β β βββ workers.py # Worker definitions
β β β βββ prompts.py
β β β βββ example.py
β β β
β β βββ agentic_rag/ # Pattern 4: Iterative Retrieval-Verification π§
β β β βββ __init__.py
β β β βββ retriever.py # Search logic
β β β βββ verifier.py # Sufficiency checker
β β β βββ prompts.py
β β β βββ example.py
β β β
β β βββ system2/ # Pattern 5: Thinking-Before-Acting π§
β β βββ __init__.py
β β βββ flow.py # <thought> generation pipeline
β β βββ parser.py # Extract reasoning
β β βββ prompts.py
β β βββ example.py
β β
β βββ utils/ # β
LEARNING & DEBUGGING
β βββ __init__.py
β βββ test_helpers.py # Mock LLMs for testing
β βββ visualizer.py # Graph state visualization
β βββ prompt_tester.py # Quick prompt iteration
β
βββ tests/ # Unit tests (one per pattern)
β βββ __init__.py
β βββ test_iev.py
β βββ test_evaluator_opt.py
β βββ test_orchestrator.py
β βββ test_agentic_rag.py
β βββ test_system2.py
β
βββ docs/ # Learning materials
β βββ PATTERNS_OVERVIEW.md # Detailed pattern explanations
β βββ GETTING_STARTED.md # Step-by-step setup
β βββ ARCHITECTURE.md # Design decisions
β βββ examples/ # Real-world use cases
β
βββ examples/ # Runnable demonstrations
β βββ simple_iev.py # "Delete this file safely"
β βββ code_review.py # Evaluator-Optimizer for code
β βββ research_project.py # Orchestrator for multi-step project
β βββ fact_checker.py # Agentic RAG example
β βββ math_problem.py # System 2 for complex logic
β
βββ .gitignore
βββ .env.example # Copy to .env for local config
βββ Makefile # Common commands (run, test, docs)
Each pattern is independent and minimal. You can understand Pattern 1 (IEV) completely before touching Pattern 2.
Start with IEV (Intelligence-Extraction-Validation) because:
β Simplest mental model: Explore β Verify β Act
β Direct connection to safety (most intuitive)
β Only 3 nodes in the graph
β Most complete implementation
# Install dependencies
pip install -e ".[openai]"
# Copy environment template
cp .env.example .env
# Edit .env with your OPENAI_API_KEY
# Run the IEV example
python -m patterns.patterns.iev.exampleThis executes a simple scenario like: "Extract deal information from text and verify it."
Open src/patterns/patterns/iev/example.py and:
- Change the verification prompt
- Add a new tool call
- See how the graph behaves
- Experiment with different validation strategies
| Pattern | File | Purpose | Complexity | When to Use | Status |
|---|---|---|---|---|---|
| IEV | patterns/iev/ |
Safety & Precision | β Easy | High-stakes actions | β Ready |
| Evaluator-Optimizer | patterns/evaluator_optimizer/ |
Quality Control | ββ Medium | Content refinement | π§ Basic |
| Orchestrator | patterns/orchestrator/ |
Complex Tasks | βββ Medium | Multi-step projects | π§ Stub |
| Agentic RAG | patterns/agentic_rag/ |
Research & Lookup | ββ Medium | Fact-heavy queries | π§ Stub |
| System 2 | patterns/system2/ |
Hard Logic | βββ Medium | Math/reasoning | π§ Stub |
Unified interface to swap LLM providers:
from patterns.core import create_llm_client
# Use OpenAI
llm = create_llm_client(provider="openai", model="gpt-4")
# Or local Ollama
llm = create_llm_client(provider="ollama", model="mistral:7b")
# Or Anthropic
llm = create_llm_client(provider="anthropic", model="claude-3-5-sonnet-20241022")
# Call the same way regardless
response = llm.generate(system="You are helpful", user="Hello")Your aistack-mcp integration point:
from patterns.core import get_mcp_toolkit
tools = get_mcp_toolkit() # Returns your aistack-mcp client
# Use in any pattern
result = tools.call("file_read", path="/path/to/file")Short-term context management:
from patterns.core import ConversationMemory
memory = ConversationMemory(max_history=10)
memory.add_message(role="user", content="...")
memory.add_message(role="assistant", content="...")
# Used by any pattern to maintain state
context = memory.get_context()[project]
dependencies = [
"pydantic>=2.0.0",
"python-dotenv>=1.0.0",
"httpx>=0.24.0",
]
[project.optional-dependencies]
openai = ["openai>=1.3.0"]
anthropic = ["anthropic>=0.25.0"]
ollama = ["ollama>=0.1.0"]
langgraph = ["langgraph>=0.2.0", "langchain>=0.1.0"]
dev = ["pytest>=7.4.0", "black>=23.0.0", "ruff>=0.1.0"]- Read
docs/PATTERNS_OVERVIEW.md - Run
python -m patterns.patterns.iev.example - Modify the verification prompt
- Add your own validation rule
- Write a test in
tests/test_iev.py
- Build a custom scenario (e.g., "Verify before trading")
- Understand state transitions and error handling
- Experiment with different LLM providers
- Learn when to use different validation strategies
- Run
examples/code_review.py - Understand the critique loop
- Experiment with grading criteria
- Combine with IEV for enhanced workflows
- Use Orchestrator to delegate tasks
- Integrate Agentic RAG for research
- Implement System 2 for logic puzzles
- Build hybrid patterns (e.g., IEV β Evaluator-Optimizer β Action)
- Python 3.10+
- Your aistack-mcp server running (optional, for MCP tests)
# Clone the repo
git clone https://github.com/mjdevaccount/llm-extraction-patterns.git
cd llm-extraction-patterns
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install in development mode
pip install -e ".[openai]"
# Copy environment template
cp .env.example .env
# Edit .env with your API keys (OpenAI, Anthropic, etc.)
# OPENAI_API_KEY=sk-...
# ANTHROPIC_API_KEY=sk-ant-...
# Run the first example
python -m patterns.patterns.iev.examplepytest tests/ -v# Using Anthropic Claude
PROVIDER=anthropic python -m patterns.patterns.iev.example
# Using local Ollama
PROVIDER=ollama MODEL=mistral:7b python -m patterns.patterns.iev.example
# Using OpenAI (default)
PROVIDER=openai python -m patterns.patterns.iev.exampleβ Pre-built production agents (this is for learning, not deployment)
β Complex multi-repo orchestration (keep it simple)
β Web UI or API (focus on patterns, not plumbing)
β Financial-specific logic (patterns are domain-agnostic)
- Clone & setup this repo (5 min)
- Run the IEV example (5 min)
- Read the IEV explanation in
docs/PATTERNS_OVERVIEW.md(20 min) - Modify the prompt in
patterns/iev/prompts.py(10 min) - Write a test for your scenario (15 min)
- Move to Evaluator-Optimizer when ready
Once you've mastered IEV, the others follow naturallyβthey're all variations on the same feedback loop.
To add your own pattern or example:
- Create a new folder in
src/patterns/patterns/{pattern_name}/ - Include:
__init__.py,graph.py,example.py,prompts.py - Add a test in
tests/test_{pattern_name}.py - Update
docs/PATTERNS_OVERVIEW.mdwith an explanation - Document the learning path
MIT
- GitHub Issues: For bugs and feature requests
- Documentation: Check
docs/for detailed explanations
Start with IEV. Master it. Then layer on the others.
Last updated: December 2025