Google ADK + Opik Integration Cookbook
This notebook demonstrates how to integrate Google’s Agent Development Kit (ADK) with Opik for comprehensive tracing and observability. We’ll cover three key integration patterns:
- Basic Agent Example - Simple single-agent setup with Opik tracing
- Multi-Agent Example - Complex multi-agent workflow showing hierarchical tracing
- Hybrid Tracing - Combining Opik decorators with ADK callbacks for comprehensive observability
You will need:
- A Comet account, for seeing Opik visualizations (free!) - comet.com
- An OpenAI account, for using gpt-4o model - platform.openai.com/settings/organization/api-keys
- Google ADK installed and configured
This example will use:
- google-adk for agent development
- opik for tracing and observability
- OpenAI’s gpt-4o model through LiteLLM
Setup
Install the required packages:
Configure Opik for your session:
Set up your OpenAI API key:
Import Required Libraries
Create Basic Agent
Here we create a single agent with Opik callbacks. The tracer will automatically capture all interactions:
Configure Opik Tracing
Set up the Opik tracer to capture all agent interactions:
Create the LLM Agent
Initialize the Google ADK agent with OpenAI’s gpt-4o model and Opik tracing:
Setup Session and Runner for Basic Example
Helper Functions
Demo: Basic Agent Interactions
Let’s test our basic agent with some weather and time queries:
The trace can now be viewed in the UI:
Example 2: Multi-Agent Setup with Hierarchical Tracing
This example demonstrates a more complex multi-agent setup where we have specialized agents for different tasks. The key insight is that you only need to add Opik callbacks to the top-level agent - all child agent calls will be automatically traced in the same trace tree.
Create Specialized Agents
Now we’ll create specialized agents for different domains. Notice that only the coordinator agent will have Opik callbacks:
Create Coordinator Agent with Opik Tracing
The coordinator agent orchestrates the specialized agents and only needs Opik callbacks here - all child agent calls will be automatically traced:
Setup Multi-Agent Session and Runner
Multi-Agent Helper Functions
Demo: Multi-Agent Interactions
Let’s test our multi-agent setup with complex queries that require coordination between agents:
The trace can now be viewed in the UI:
Example 3: Hybrid Tracing - Combining Opik Decorators with ADK Callbacks
This advanced example shows how to combine Opik’s @opik.track
decorator with ADK’s callback system. This is powerful when you have complex multi-step tools that perform their own internal operations that you want to trace separately, while still maintaining the overall agent trace context.
Define Advanced Tools with Opik Decorators
These tools use the @opik.track
decorator to trace their internal operations, while still being called within the ADK agent’s trace context:
Create Hybrid Agent with Both Decorator and Callback Tracing
This agent uses tools that have internal Opik tracing via decorators, while the agent itself uses ADK callbacks:
Setup Hybrid Session and Runner
Hybrid Example Helper Functions
Demo: Hybrid Tracing in Action
This demo showcases how both decorator-traced tool operations and ADK callback-traced agent operations appear in the same unified trace:
The trace can now be viewed in the UI:
Understanding the Tracing Output
After running all three examples, you can view the traces in your Opik dashboard. Here’s what you’ll see:
Basic Example Traces
- Simple linear trace showing: User Input → Agent Processing → Tool Calls → Model Response
- Clear visibility into tool execution and model interactions
Multi-Agent Example Traces
- Hierarchical trace showing: Coordinator Agent → Sub-Agent Delegation → Specialized Tool Calls
- Key insight: Only the coordinator needed Opik callbacks, but all sub-agent operations are traced
- Shows the complete decision tree of which agents were involved
Hybrid Example Traces
- Most comprehensive: Shows both ADK callback traces AND decorator traces in the same trace tree
- Tool calls contain nested spans from
@opik.track
decorators - Demonstrates how decorator-traced functions (like
validate_location
,process_weather_data
) appear as child spans within the tool call spans - Perfect for debugging complex multi-step operations
Key Benefits of This Integration
- Automatic Tracing: ADK callbacks provide zero-configuration tracing of agent interactions
- Hierarchical Visibility: Multi-agent setups automatically create nested trace structures
- Flexible Granularity: Combine coarse-grained agent tracing with fine-grained function tracing
- Unified Context: All traces (callbacks + decorators) appear in the same trace tree
- Production Ready: Comprehensive observability for debugging and optimization
Next Steps
This notebook demonstrated three powerful integration patterns. You can extend this by:
- Adding custom evaluation metrics using Opik’s evaluation framework
- Implementing real-time monitoring and alerting based on trace data
- Using different LLM models and comparing their performance
- Adding more sophisticated multi-agent workflows
- Implementing custom tracing strategies for specific business logic
- Building evaluation datasets from traced conversations
For more information: