Google Agent Development Kit (ADK) Samples: Complete Tutorial for Multi-Agent Applications
⏱️ Estimated Reading Time: 15 minutes
Introduction to Google Agent Development Kit (ADK)
Google’s Agent Development Kit (ADK) is an open-source framework designed for building intelligent and autonomous multi-agent systems. ADK enables developers to create production-ready agent applications that can interact with tools, communicate with each other, and perform complex tasks across various models and deployment environments.
The google/adk-samples repository provides a comprehensive collection of sample agents built on top of ADK, ranging from simple conversational bots to sophisticated multi-agent workflows. These samples serve as practical examples and starting points for developers looking to leverage ADK’s capabilities.
Key Features of ADK
1. Multi-Agent Architecture
ADK supports the creation of multiple agents that can work together, each with specialized roles and capabilities. This architecture enables complex problem-solving through agent collaboration.
2. Tool Integration
Agents built with ADK can interact with external tools and APIs, extending their capabilities beyond language model responses to perform real-world actions.
3. Flexible Deployment
ADK supports various deployment environments, from local development to cloud-based production systems, making it suitable for different scales of applications.
4. Language Support
The framework provides SDKs for both Python and Java, allowing developers to work in their preferred programming language.
Repository Structure Overview
The ADK samples repository is organized into two main sections:
adk-samples/
├── java/
│ ├── agents/
│ │ ├── software-bug-assistant/
│ │ └── time-series-forecasting/
│ └── README.md
└── python/
├── agents/
│ ├── academic-research/
│ ├── blog-writer/
│ ├── customer-service/
│ ├── data-science/
│ ├── financial-advisor/
│ ├── RAG/
│ └── [20+ more samples]
└── README.md
Installation and Environment Setup
Prerequisites
Before working with ADK samples, ensure you have the following installed:
- Python 3.8+ or Java 11+ (depending on your chosen language)
- Git for cloning the repository
- Google Cloud Project with necessary APIs enabled
Step 1: Clone the Repository
# Clone the ADK samples repository
git clone https://github.com/google/adk-samples.git
cd adk-samples
Step 2: Install ADK
Follow the official ADK installation guide for your chosen language:
For Python:
pip install google-adk
For Java:
# Add ADK dependency to your Maven or Gradle project
# Detailed instructions in the Java README
Step 3: Environment Configuration
Create a .env
file in your project directory and configure necessary environment variables:
# Google Cloud configuration
GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/service-account-key.json
GOOGLE_CLOUD_PROJECT=your-project-id
# API Keys (if required by specific agents)
OPENAI_API_KEY=your-openai-key
ANTHROPIC_API_KEY=your-anthropic-key
Exploring Sample Agents
Python Samples Deep Dive
The Python samples cover a wide range of use cases. Let’s explore some notable examples:
1. Customer Service Agent
The customer service agent demonstrates how to build a conversational AI that can handle customer inquiries, access knowledge bases, and escalate complex issues.
Key Features:
- Natural language understanding
- Knowledge base integration
- Escalation workflows
- Multi-turn conversations
2. Data Science Agent
This agent showcases AI-powered data analysis capabilities, including data preprocessing, visualization, and statistical analysis.
Capabilities:
- Automated data cleaning
- Statistical analysis
- Chart generation
- Report creation
3. RAG (Retrieval-Augmented Generation) Agent
The RAG agent demonstrates how to combine retrieval mechanisms with generation capabilities for knowledge-intensive tasks.
Components:
- Document indexing
- Semantic search
- Context-aware generation
- Source attribution
Java Samples Overview
The Java samples focus on enterprise-grade applications:
1. Software Bug Assistant
A specialized agent for software development teams that can analyze bug reports, suggest solutions, and track issue resolution.
2. Time Series Forecasting
An agent that performs advanced time series analysis and forecasting using machine learning models.
Practical Implementation Guide
Running Your First Agent
Let’s walk through running the customer service agent as an example:
Step 1: Navigate to the Agent Directory
cd python/agents/customer-service
Step 2: Install Dependencies
# Install required packages
pip install -r requirements.txt
Step 3: Configure the Agent
Edit the configuration file to match your environment:
# config.py
AGENT_CONFIG = {
"model": "gemini-pro",
"temperature": 0.7,
"max_tokens": 1000,
"knowledge_base": "customer_kb.json"
}
Step 4: Run the Agent
python main.py
Step 5: Interact with the Agent
Once running, you can interact with the agent through the provided interface:
Customer Service Agent: Hello! How can I help you today?
User: I'm having trouble with my order
Customer Service Agent: I'd be happy to help you with your order. Could you please provide your order number?
Advanced Configuration and Customization
Agent Behavior Customization
You can customize agent behavior by modifying configuration parameters:
from google.adk import Agent, AgentConfig
config = AgentConfig(
name="custom-agent",
model="gemini-pro",
system_prompt="You are a helpful assistant specialized in...",
tools=["web_search", "calculator", "email_sender"],
memory_type="conversation",
max_conversation_length=100
)
agent = Agent(config)
Tool Integration
ADK allows you to integrate custom tools with your agents:
from google.adk.tools import Tool
class CustomTool(Tool):
def __init__(self):
super().__init__(
name="custom_tool",
description="Performs custom business logic"
)
def execute(self, parameters):
# Your custom logic here
return {"result": "Custom tool executed successfully"}
# Register the tool with your agent
agent.add_tool(CustomTool())
Multi-Agent Coordination
For complex workflows, you can create multiple agents that work together:
from google.adk import MultiAgentSystem
# Create specialized agents
research_agent = Agent(research_config)
writing_agent = Agent(writing_config)
review_agent = Agent(review_config)
# Create a multi-agent system
system = MultiAgentSystem([research_agent, writing_agent, review_agent])
# Define workflow
workflow = {
"research": research_agent,
"write": writing_agent,
"review": review_agent
}
# Execute the workflow
result = system.execute_workflow(workflow, initial_input="Write a blog post about AI")
Best Practices and Optimization
1. Prompt Engineering
Effective prompt engineering is crucial for agent performance:
SYSTEM_PROMPT = """
You are an expert {domain} assistant with the following capabilities:
- {capability_1}
- {capability_2}
- {capability_3}
Guidelines:
1. Always provide accurate and helpful information
2. Ask clarifying questions when needed
3. Cite sources when making factual claims
4. Escalate complex issues to human experts
Response Format:
- Be concise but comprehensive
- Use bullet points for lists
- Include relevant examples
"""
2. Error Handling and Resilience
Implement robust error handling for production deployments:
from google.adk.exceptions import ADKException
try:
response = agent.process(user_input)
except ADKException as e:
logger.error(f"Agent processing error: {e}")
response = "I apologize, but I'm experiencing technical difficulties. Please try again later."
3. Performance Monitoring
Monitor agent performance and usage patterns:
from google.adk.monitoring import AgentMetrics
metrics = AgentMetrics(agent)
metrics.track_response_time()
metrics.track_success_rate()
metrics.track_user_satisfaction()
Deployment Strategies
Local Development
For development and testing:
# Run with debug mode
python main.py --debug --port 8000
Cloud Deployment
For production deployments on Google Cloud:
# app.yaml for Google App Engine
runtime: python39
env_variables:
GOOGLE_CLOUD_PROJECT: your-project-id
automatic_scaling:
min_instances: 1
max_instances: 10
Container Deployment
Using Docker for containerized deployments:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8080
CMD ["python", "main.py", "--port", "8080"]
Troubleshooting Common Issues
1. Authentication Errors
Ensure your Google Cloud credentials are properly configured:
# Set up application default credentials
gcloud auth application-default login
# Or export service account key
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
2. Dependency Conflicts
Use virtual environments to avoid package conflicts:
python -m venv adk-env
source adk-env/bin/activate # On Windows: adk-env\Scripts\activate
pip install -r requirements.txt
3. Memory Issues
For memory-intensive agents, configure appropriate limits:
config = AgentConfig(
max_memory_usage="2GB",
cleanup_interval=300, # seconds
conversation_history_limit=50
)
Security Considerations
1. API Key Management
Never hardcode API keys in your source code:
import os
from google.cloud import secretmanager
def get_api_key(secret_name):
client = secretmanager.SecretManagerServiceClient()
name = f"projects/{PROJECT_ID}/secrets/{secret_name}/versions/latest"
response = client.access_secret_version(request={"name": name})
return response.payload.data.decode("UTF-8")
2. Input Validation
Validate and sanitize user inputs:
import re
def validate_user_input(input_text):
# Remove potentially harmful content
cleaned_input = re.sub(r'[<>"\']', '', input_text)
# Limit input length
if len(cleaned_input) > 1000:
cleaned_input = cleaned_input[:1000]
return cleaned_input
3. Rate Limiting
Implement rate limiting to prevent abuse:
from functools import wraps
import time
def rate_limit(max_calls_per_minute=60):
def decorator(func):
last_called = {}
@wraps(func)
def wrapper(*args, **kwargs):
user_id = kwargs.get('user_id', 'anonymous')
now = time.time()
if user_id in last_called:
if now - last_called[user_id] < 60 / max_calls_per_minute:
raise Exception("Rate limit exceeded")
last_called[user_id] = now
return func(*args, **kwargs)
return wrapper
return decorator
Advanced Use Cases
1. Document Processing Pipeline
Create a multi-agent pipeline for document processing:
# Document analysis agent
analyzer_agent = Agent(analyzer_config)
# Information extraction agent
extractor_agent = Agent(extractor_config)
# Summary generation agent
summarizer_agent = Agent(summarizer_config)
# Pipeline execution
def process_document(document_path):
# Step 1: Analyze document structure
analysis = analyzer_agent.process(f"Analyze document: {document_path}")
# Step 2: Extract key information
extraction = extractor_agent.process(f"Extract data from: {analysis}")
# Step 3: Generate summary
summary = summarizer_agent.process(f"Summarize: {extraction}")
return {
"analysis": analysis,
"extracted_data": extraction,
"summary": summary
}
2. Intelligent Customer Support System
Build a comprehensive customer support system:
class CustomerSupportSystem:
def __init__(self):
self.routing_agent = Agent(routing_config)
self.technical_agent = Agent(technical_config)
self.billing_agent = Agent(billing_config)
self.escalation_agent = Agent(escalation_config)
def handle_inquiry(self, customer_inquiry):
# Route inquiry to appropriate agent
routing_decision = self.routing_agent.process(customer_inquiry)
if routing_decision.category == "technical":
return self.technical_agent.process(customer_inquiry)
elif routing_decision.category == "billing":
return self.billing_agent.process(customer_inquiry)
elif routing_decision.escalate:
return self.escalation_agent.process(customer_inquiry)
else:
return self.routing_agent.process(customer_inquiry)
Performance Optimization
1. Caching Strategies
Implement caching to improve response times:
from functools import lru_cache
import hashlib
class CachedAgent:
def __init__(self, agent):
self.agent = agent
self.response_cache = {}
def process(self, input_text):
# Create cache key
cache_key = hashlib.md5(input_text.encode()).hexdigest()
if cache_key in self.response_cache:
return self.response_cache[cache_key]
# Process and cache result
response = self.agent.process(input_text)
self.response_cache[cache_key] = response
return response
2. Asynchronous Processing
Use async processing for better performance:
import asyncio
from google.adk import AsyncAgent
async def process_multiple_requests(requests):
agent = AsyncAgent(config)
# Process requests concurrently
tasks = [agent.process_async(request) for request in requests]
responses = await asyncio.gather(*tasks)
return responses
Testing and Quality Assurance
1. Unit Testing
Create comprehensive tests for your agents:
import unittest
from unittest.mock import Mock, patch
class TestCustomerServiceAgent(unittest.TestCase):
def setUp(self):
self.agent = CustomerServiceAgent(test_config)
def test_greeting_response(self):
response = self.agent.process("Hello")
self.assertIn("Hello", response)
self.assertIn("help", response.lower())
def test_order_inquiry(self):
response = self.agent.process("I need help with order #12345")
self.assertIn("order", response.lower())
@patch('google.adk.llm.generate')
def test_api_error_handling(self, mock_generate):
mock_generate.side_effect = Exception("API Error")
response = self.agent.process("Test input")
self.assertIn("technical difficulties", response.lower())
2. Integration Testing
Test agent interactions and workflows:
def test_multi_agent_workflow():
system = MultiAgentSystem([agent1, agent2, agent3])
test_input = "Process this complex request"
result = system.execute_workflow(workflow, test_input)
assert result.success == True
assert len(result.steps) == 3
assert result.final_output is not None
Monitoring and Analytics
1. Performance Metrics
Track key performance indicators:
from google.adk.analytics import AgentAnalytics
analytics = AgentAnalytics()
# Track response times
analytics.track_metric("response_time", response_time)
# Track success rates
analytics.track_metric("success_rate", success_count / total_count)
# Track user satisfaction
analytics.track_metric("user_satisfaction", satisfaction_score)
2. Logging and Debugging
Implement comprehensive logging:
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('agent.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
class LoggedAgent(Agent):
def process(self, input_text):
logger.info(f"Processing input: {input_text[:100]}...")
try:
response = super().process(input_text)
logger.info(f"Generated response: {response[:100]}...")
return response
except Exception as e:
logger.error(f"Processing error: {e}")
raise
Future Enhancements and Roadmap
1. Upcoming Features
Google continues to enhance ADK with new capabilities:
- Multimodal Support: Integration of vision and audio processing
- Enhanced Tool Ecosystem: Expanded library of pre-built tools
- Improved Orchestration: Better multi-agent coordination mechanisms
- Performance Optimizations: Faster inference and reduced latency
2. Community Contributions
The ADK samples repository welcomes community contributions:
- New Sample Agents: Contribute domain-specific examples
- Tool Integrations: Add new tool implementations
- Documentation: Improve guides and tutorials
- Bug Fixes: Report and fix issues
Conclusion
Google’s Agent Development Kit (ADK) provides a powerful foundation for building intelligent multi-agent systems. The comprehensive samples repository offers practical examples and starting points for various use cases, from simple conversational agents to complex multi-agent workflows.
Key takeaways from this tutorial:
- Start Simple: Begin with basic samples and gradually explore more complex examples
- Customize Thoughtfully: Adapt configurations and prompts to your specific use case
- Test Thoroughly: Implement comprehensive testing strategies for production readiness
- Monitor Continuously: Track performance and user satisfaction metrics
- Secure by Design: Implement proper security measures from the beginning
The ADK ecosystem continues to evolve, and staying engaged with the community through the samples repository will help you leverage the latest capabilities and best practices.
Additional Resources
- Official Documentation: ADK Documentation
- GitHub Repository: google/adk-samples
- Developer Blog: Google Developers Blog - ADK
- Community Forums: ADK Discussions
- API Reference: ADK API Documentation
This tutorial provides a comprehensive introduction to Google’s Agent Development Kit samples. As the framework continues to evolve, make sure to check the official documentation for the latest updates and features.