Gemini SDK Integration
SDKTraccia automatically instruments Google's Gemini SDK for full observability of LLM calls.
Traccia automatically detects and instruments Google's Gemini SDK (google-genai in Python, @google/genaiin TypeScript) when it's installed. No extra code or configuration is required—just call init() and your Gemini calls are traced automatically, with prompt, completion, token usage, and cost captured on every span.
1Install Traccia and the Gemini SDK
Install both packages:
pip install traccia google-genai2Initialize Traccia
Initialize Traccia at the start of your application. Gemini tracing is auto-enabled when google-genai (Python) or @google/genai (TypeScript) is installed:
from traccia import init
# Minimal initializationinit()
# With API keyinit(api_key="tr_live_xxxxxxxxxxxx")
# With custom configinit( endpoint="http://localhost:4318/v1/traces", agent_id="my-agent", sample_rate=0.1)3Call Gemini as Usual
Use the Gemini client's interactions.create() method as usual. Traccia captures the prompt, model, streaming flag, and (on non-streaming calls) usage, completion text, and interaction ID automatically:
from traccia import initfrom google import genai
init() # Automatically enables Gemini tracing
client = genai.Client(api_key="GEMINI_API_KEY")
# This call is automatically tracedresponse = client.interactions.create( model="gemini-2.5-flash", input="Write a haiku about recursion",)print(response.output_text)Traccia monkey-patches GeminiNextGenInteractions.create (sync and async) during init(), wrapping every call in an llm.gemini.interaction span.
What Gets Traced
Every llm.gemini.interaction span captures:
llm.vendor / llm.modelSet to google_gemini and the requested model
llm.promptThe interaction's input field, truncated to 4KB
llm.completionThe response's output_text, truncated to 4KB
llm.usage.prompt_tokens / completion_tokens / total_tokensOpenAI-compatible token aliases for downstream processors
llm.usage.thought_tokens / cached_tokens / tool_use_tokensGemini-specific usage breakdown, when returned
llm.interaction_id / llm.previous_interaction_idFor multi-turn interaction chaining
llm.streamingTrue when stream: true is passed
Streaming calls
create()call resolves almost immediately with a stream object—before usage and output text exist. Traccia records llm.streaming: true but does not populate usage/completion attributes for streaming calls, to avoid recording near-zero duration and missing data.Configuration
Gemini instrumentation is enabled by default when the SDK is installed and is covered by Traccia's global patching switch:
# Disable all auto-patching (including Gemini)init(enable_patching=False)Or via environment variable:
export TRACCIA_ENABLE_PATCHING=falseComplete Example
from traccia import init, span, stop_tracingfrom google import genai
init()
client = genai.Client(api_key="GEMINI_API_KEY")
with span("recommendation_session") as session_span: session_span.set_attribute("user.preference.genre", "sci-fi")
response = client.interactions.create( model="gemini-2.5-flash", input="Recommend 5 sci-fi movies for someone who liked Blade Runner", )
session_span.set_attribute("recommendations.text_length", len(response.output_text))
print(response.output_text)
# Flush traces before exitstop_tracing(flush_timeout=1.0)Next Steps
© 2026 Traccia.