Step by Step Tutorials with Practical Code Examples
Agentic AI systems go beyond single prompt responses. They reason, delegate, evaluate, and act. Anthropic formalized this thinking through agent design patterns, and Google ADK provides a clean, production-ready framework to implement them.
This tutorial walks you through six core agent patterns, each with clear architecture explanation, when to use, and working Python examples.
Prerequisites
Before starting, ensure you have:
- Python 3.10 or higher
- Basic understanding of LLMs
- Access to a supported LLM (Gemini, Claude, etc.)
- Google ADK installed
pip install google-adk
Project Structure Used in This Guide
agents/
├── base_agent.py
├── chain_agent.py
├── router_agent.py
├── parallel_agent.py
├── orchestrator_agent.py
├── evaluator_agent.py
└── autonomous_agent.py
1. Prompt Chaining Pattern
What It Solves
Large tasks often fail when handled in a single prompt. Prompt chaining divides the task into logical steps.
Use Cases
- Blog writing
- Report generation
- Step-by-step reasoning
Step 1: Create a Base Agent
from google.adk.agents import LlmAgent
writer_agent = LlmAgent(
name="WriterAgent",
model="gemini-2.0-flash",
instruction="Write a clear explanation based on the input topic."
)
Step 2: Create a Refiner Agent
refiner_agent = LlmAgent(
name="RefinerAgent",
model="gemini-2.0-flash",
instruction="Improve clarity, grammar, and structure of the given text."
)
Step 3: Chain the Output
draft = writer_agent.run("Explain agentic AI for beginners")
final_output = refiner_agent.run(draft)
print(final_output)
Why This Works
Each agent focuses on a single responsibility. Output quality improves and hallucination risk reduces.
2. Routing Agent Pattern
What It Solves
Different user queries need different expertise.
Step 1: Create Specialized Agents
tech_agent = LlmAgent(
name="TechAgent",
instruction="Answer technical programming questions."
)
business_agent = LlmAgent(
name="BusinessAgent",
instruction="Answer business and strategy questions."
)
Step 2: Create a Router Agent
router_agent = LlmAgent(
name="RouterAgent",
instruction="""
Classify the user query as 'tech' or 'business'.
Return only the category.
"""
)
Step 3: Route Dynamically
query = "How do microservices scale in cloud?"
category = router_agent.run(query)
if "tech" in category:
response = tech_agent.run(query)
else:
response = business_agent.run(query)
print(response)
Benefit
Scales well for chatbots, support systems, and internal assistants.
3. Parallelization Pattern
What It Solves
Improve speed or quality by running agents simultaneously.
Example: Multiple Opinions on the Same Question
agents = [
LlmAgent(name="Agent1", instruction="Give a concise answer"),
LlmAgent(name="Agent2", instruction="Give a detailed answer"),
LlmAgent(name="Agent3", instruction="Give examples")
]
responses = [agent.run("What is agentic AI?") for agent in agents]
Voting or Aggregation
final_answer = "\n".join(responses)
print(final_answer)
Use Case
Research, brainstorming, validation, comparison analysis.
4. Orchestrator–Worker Pattern
What It Solves
Complex tasks that cannot be predefined fully.
Step 1: Worker Agents
research_agent = LlmAgent(
name="ResearchAgent",
instruction="Collect factual information."
)
writer_agent = LlmAgent(
name="WriterAgent",
instruction="Write content based on research."
)
Step 2: Orchestrator Agent
orchestrator = LlmAgent(
name="Orchestrator",
instruction="""
Break the task into research and writing steps.
Decide which agent to use.
"""
)
Step 3: Execution Logic
task = "Write an article on AI agents"
research = research_agent.run(task)
article = writer_agent.run(research)
print(article)
Why It Matters
This pattern enables dynamic workflows without hard-coding every step.
5. Evaluator–Optimizer Pattern
What It Solves
Improves output quality iteratively.
Step 1: Generator Agent
generator = LlmAgent(
name="Generator",
instruction="Generate a product description."
)
Step 2: Evaluator Agent
evaluator = LlmAgent(
name="Evaluator",
instruction="""
Review the text and suggest improvements.
Focus on clarity and persuasiveness.
"""
)
Step 3: Loop Execution
content = generator.run("AI Automation Tool")
for _ in range(2):
feedback = evaluator.run(content)
content = generator.run(feedback)
print(content)
Ideal For
SEO writing, marketing copy, policy documents.
6. Autonomous Agent Pattern
What It Solves
Agents that plan, decide, and act independently.
Autonomous Agent Setup
autonomous_agent = LlmAgent(
name="AutoAgent",
instruction="""
You are an autonomous agent.
Decide steps, execute them, and summarize results.
"""
)
Execution
goal = "Research agentic AI trends and summarize findings"
result = autonomous_agent.run(goal)
print(result)
Real-World Use
- Personal AI assistants
- Monitoring systems
- Automated research bots
Best Practices for Production Use
- Use prompt chaining before autonomy
- Log every agent decision
- Limit tool permissions
- Add human checkpoints for critical flows
- Combine evaluator loops with routing for quality control
Final Thoughts
Anthropic’s agent patterns provide thinking structures, while Google ADK provides execution infrastructure. Together, they enable scalable, intelligent, and maintainable AI systems.
More: Implementing Anthropic’s Agent Design Patterns with Google ADK
