-
Notifications
You must be signed in to change notification settings - Fork 337
docs: add agent observability with opik #738
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vincentkoc
wants to merge
3
commits into
google:main
Choose a base branch
from
vincentkoc:docs-opik
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# Agent Observability with Opik | ||
|
||
[Opik](https://www.comet.com/opik/) is Comet's observability, evaluation, and optimization platform for LLM and agent workloads. It captures the full execution context of your Google ADK applications—including agent runs, tool invocations, model calls, and custom business logic—so you can debug faster, monitor cost and latency, and ship production-ready agents with confidence. | ||
|
||
 | ||
|
||
## Why Opik for ADK? | ||
|
||
Opik extends ADK's native OpenTelemetry support with: | ||
|
||
- **Rich span data** – Every agent, tool, and LLM call is logged with prompts, responses, metadata, and token usage for all model calls. | ||
- **Automatic cost tracking** – Built-in cost calculators let you surface spend per span, per agent, or per project without additional code. | ||
- **Multi-agent visualization** – The `track_adk_agent_recursive` helper renders hierarchical graphs of complex agent workflows directly in the UI. | ||
- **Thread-aware tracing** – ADK session IDs are automatically mapped to Opik threads so multi-turn conversations stay grouped together. | ||
- **Error analytics** – Exceptions raised inside agents, models, or tools are captured with stack traces and surfaced in the Opik dashboard. | ||
|
||
## Installation | ||
|
||
Install Opik alongside Google ADK: | ||
|
||
```bash | ||
pip install opik google-adk | ||
``` | ||
|
||
## Configure Opik | ||
|
||
Initialize the Opik Python SDK using whichever method fits your deployment. The quickest way during development is: | ||
|
||
```bash | ||
opik configure | ||
``` | ||
|
||
You can also call `opik.configure()` from code or set the `OPIK_API_KEY`, `OPIK_WORKSPACE`, and `OPIK_PROJECT_NAME` environment variables. See the [Python SDK configuration guide](https://www.comet.com/docs/opik/tracing/sdk_configuration/) for the full matrix covering cloud, enterprise, and self-hosted deployments. | ||
|
||
## Configure Google ADK | ||
|
||
Opik works with any ADK-supported model provider. For the example below we authenticate to Gemini through Google AI Studio: | ||
|
||
```bash | ||
export GOOGLE_GENAI_USE_VERTEXAI=FALSE | ||
export GOOGLE_API_KEY="<your-gemini-api-key>" | ||
``` | ||
|
||
If you deploy on Vertex AI instead, set `GOOGLE_GENAI_USE_VERTEXAI=TRUE` and configure `GOOGLE_CLOUD_PROJECT` plus `GOOGLE_CLOUD_LOCATION` (or use Application Default Credentials). | ||
|
||
## Basic agent setup with OpikTracer | ||
|
||
The `OpikTracer` automatically captures agent execution, tool calls, and model interactions. Attach it to your ADK agents as shown below: | ||
|
||
```python | ||
from google.adk.agents import LlmAgent | ||
from google.adk import runners | ||
from opik.integrations.adk import OpikTracer | ||
|
||
weather_agent = LlmAgent( | ||
name="weather_agent", | ||
instruction="You provide friendly weather updates.", | ||
model="gemini-2.0-flash", | ||
) | ||
|
||
tracer = OpikTracer(project_name="adk-weather-demo") | ||
|
||
runner = runners.Runner( | ||
agent=weather_agent, | ||
app_name="weather_app", | ||
before_agent_callback=tracer.before_agent_callback, | ||
after_agent_callback=tracer.after_agent_callback, | ||
before_model_callback=tracer.before_model_callback, | ||
after_model_callback=tracer.after_model_callback, | ||
) | ||
|
||
response = runner.run(user_request="What's the weather in New York?") | ||
tracer.flush() # ensure spans are delivered before the script exits | ||
``` | ||
|
||
Open the Opik dashboard and navigate to your project to inspect the trace. You will see the agent span, the downstream LLM call, tool invocations (if any), token usage, and cost estimates. | ||
|
||
## Multi-agent and session workflows | ||
|
||
For complex graphs, instrument your root agent with `track_adk_agent_recursive` to automatically trace every nested ADK agent: | ||
|
||
```python | ||
from opik.integrations.adk import track_adk_agent_recursive | ||
|
||
track_adk_agent_recursive(weather_agent) | ||
``` | ||
|
||
When you run ADK sessions, Opik maps the `session_id` to a thread so multi-turn conversations stay grouped. Metadata such as `user_id` and `app_name` is recorded automatically. | ||
|
||
## Hybrid tracing with `@track` | ||
|
||
The Opik tracer is fully compatible with the [`@opik.track`](https://www.comet.com/docs/opik/tracing/log_traces/#track) decorator. You can: | ||
|
||
- Call ADK agents from inside tracked functions and keep parent/child span relationships intact. | ||
- Decorate tool functions to capture custom business logic alongside ADK-managed spans. | ||
|
||
## Agent Graph | ||
Opik automatically generates visual representations of your agent workflows using Mermaid diagrams. This includes agent hierarchy and relationships, tool calling, parallel processing and sub-tasks too. | ||
|
||
 | ||
|
||
This also supports more complex agent graphs too. | ||
|
||
 | ||
|
||
## Troubleshooting | ||
|
||
- Always await all events when using `Runner.run_async`. Exiting early prevents the tracer from closing spans. | ||
- Call `tracer.flush()` before your script exits to guarantee that buffered spans are delivered. | ||
- Keep `opik` and `google-adk` up to date to benefit from the latest integration improvements. | ||
|
||
## Additional resources | ||
|
||
- [Opik ADK integration reference](https://www.comet.com/docs/opik/integrations/adk) | ||
- [Opik tracing overview](https://www.comet.com/docs/opik/tracing/log_traces) | ||
- [Opik self-host installation guide](https://www.comet.com/docs/opik/self-host/overview) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get the following error when I try to run this code sample (and after installing the deps mentioned earlier):
I got a similar error when running the sample code from https://www.comet.com/opik/koverholt/get-started?integration=google-adk.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@koverholt appreciate the review. Could you try a new venv/env and install the packages please, you likley have outdated package somewhere. Team and I have tested this on a clean env and were not able to recreate, if not can you share package versions for us to re-create the particular issue(s).