Integrate Langfuse with Google’s Agent Development Kit
This notebook demonstrates how to capture detailed traces from a Google Agent Development Kit (ADK) application with Langfuse using the OpenTelemetry (OTel) protocol.
Why Agent Development Kit?
Google’s Agent Development Kit streamlines building, orchestrating, and tracing generative-AI agents out of the box, letting you move from prototype to production far faster than wiring everything yourself.
Why Langfuse?
Langfuse gives you a detailed dashboard and rich analytics for every prompt, model response, and function call in your agent, making it easy to debug, evaluate, and iterate on LLM apps.
Step 1: Install dependencies
%pip install langfuse google-adk -q
Step 2: Set up environment variables
Fill in the Langfuse and OpenTelemetry credentials for your project. Also set your Gemini API key.
import os
import base64
# Get keys for your project from the project settings page: https://cloud.langfuse.com
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-..."
os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-..."
os.environ["LANGFUSE_HOST"] = "https://cloud.langfuse.com" # 🇪🇺 EU region
# os.environ["LANGFUSE_HOST"] = "https://us.cloud.langfuse.com" # 🇺🇸 US region
# Build Basic Auth header.
LANGFUSE_AUTH = base64.b64encode(
f"{os.environ.get('LANGFUSE_PUBLIC_KEY')}:{os.environ.get('LANGFUSE_SECRET_KEY')}".encode()
).decode()
# Configure OpenTelemetry endpoint & headers
os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = os.environ.get("LANGFUSE_HOST") + "/api/public/otel"
os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = f"Authorization=Basic {LANGFUSE_AUTH}"
# Gemini API Key (Get from Google AI Studio: https://aistudio.google.com/app/apikey)
os.environ["GOOGLE_API_KEY"] = "..."
With the environment variables set, we can now initialize the Langfuse client. get_client()
initializes the Langfuse client using the credentials provided in the environment variables.
from langfuse import get_client
langfuse = get_client()
# Verify connection
if langfuse.auth_check():
print("Langfuse client is authenticated and ready!")
else:
print("Authentication failed. Please check your credentials and host.")
Langfuse client is authenticated and ready!
Step 3: Build a hello world agent
Every tool call and model completion is captured as an OpenTelemetry span and forwarded to Langfuse.
from google.adk import Agent, Runner
from google.adk.sessions import InMemorySessionService
from google.genai import types
# 1. ‘say_hello’ tool
def say_hello():
return {"greeting": "Hello Langfuse 👋"}
agent = Agent(
name="hello_agent",
model="gemini-2.0-flash",
instruction="Always greet using the say_hello tool.",
tools=[say_hello],
)
# 2. session service + runner
session_service = InMemorySessionService()
APP_NAME = "hello_app"
USER_ID = "demo-user"
SESSION_ID = "demo-session" # any string; UUIDs work too
# create the session once
session_service.create_session(
app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID
)
runner = Runner(agent=agent, app_name=APP_NAME, session_service=session_service)
# 3. single‑turn run
user_msg = types.Content(role="user", parts=[types.Part(text="hi")])
for event in runner.run(user_id=USER_ID, session_id=SESSION_ID, new_message=user_msg):
if event.is_final_response():
print(event.content.parts[0].text)
Warning: there are non-text parts in the response: ['function_call'], returning concatenated text result from text parts. Check the full candidates.content.parts accessor to get the full model response.
Hello Langfuse 👋!
Step 4: View the trace in Langfuse
Head over to your Langfuse dashboard → Traces. You should see traces including all tool calls and model inputs/outputs.