Skip to content

Gemini

Coralogix's AI Observability integrations provide deep visibility into applications that rely on Google Gemini models. With a dedicated integration for the Google Generative AI SDK, Coralogix delivers consolidated insight into synchronous, streaming, and async calls, enabling teams to monitor performance, cost, and reliability across every Gemini workload.

Overview

This library supplies production-ready OpenTelemetry instrumentation for the Google GenAI SDK. It automatically traces invocations of Gemini text and tool-calling workflows, enriches spans with model metadata, and records usage metrics. The integration accelerates debugging, highlights token consumption, and standardizes observability across Gemini-powered features.

Note

Instrumentation covers synchronous, streaming, and async variants of Models.generate_content while preserving tool-call metadata emitted by the SDK.

Requirements

  • Python version 3.9 and above.
  • Coralogix API keys.
  • google-genai package version 1.0.0 (minimum) and below 2.0.0.

Configure the Gemini SDK with a valid API key before activating the instrumentation.

Installation

Run the following command.

pip install llm-tracekit[gemini]

Authentication

Exporting spans to Coralogix requires configuring OTLP credentials before enabling instrumentation. Use setup_export_to_coralogix or the corresponding environment variables to supply authentication details.

Using setup_export_to_coralogix

from llm_tracekit import setup_export_to_coralogix

setup_export_to_coralogix(
    coralogix_token="<your_coralogix_token>",
    coralogix_endpoint="<your_coralogix_endpoint>",
    application_name="<ai-application>",
    subsystem_name="<ai-subsystem>",
    capture_content=True
)

Using environment variables

If arguments are not passed to setup_export_to_coralogix, the helper reads the following environment variables:

  • CX_TOKEN: Your Coralogix API key.
  • CX_ENDPOINT: ingress.:443, the OpenTelemetry endpoint associated with your Coralogix domain.
  • CX_APPLICATION_NAME: Your application's name.
  • CX_SUBSYSTEM_NAME: Your subsystem's name.

Usage

Set up instrumentation for the Google GenAI SDK.

Set up tracing

Instrument

To instrument all Gemini models, create an instance of GeminiInstrumentor and call the instrument method.

from llm_tracekit import GeminiInstrumentor

instrumentor = GeminiInstrumentor()
instrumentor.instrument()

Uninstrument

To remove instrumentation, call the uninstrument method.

instrumentor.uninstrument()

Full example

from google import genai

from llm_tracekit import setup_export_to_coralogix
from llm_tracekit import GeminiInstrumentor

setup_export_to_coralogix(
    coralogix_token="<your_coralogix_token>",
    coralogix_endpoint="<your_coralogix_endpoint>",
    application_name="<ai-application>",
    subsystem_name="<ai-subsystem>"
)

instrumentor = GeminiInstrumentor()
instrumentor.instrument()

client = genai.Client()

try:
    response = client.models.generate_content(
        model="gemini-2.0-flash-lite",
        contents="What's the temperature in Tel Aviv?"
    )
finally:
    client.close()

print(response)

Enable message content capture

By default, message content - such as prompts, completions, tool call arguments, and tool responses - is not captured.

To capture message content as span attributes, set the environment variable OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to true.

Many Coralogix AI evaluations rely on message content, so enabling capture is strongly recommended.

Key differences from OpenTelemetry

  • User prompts, tool invocations, and model responses are stored as span attributes instead of log events, preserving a single correlated timeline for each request.

Semantic conventions

AttributeTypeDescriptionExample
gen_ai.operation.namestringThe specific name of the Gemini operation being performedchat
gen_ai.systemstringThe provider or framework responsible for the operationgemini
gen_ai.request.modelstringThe name of the model requested by the user or applicationgemini-2.0-flash-lite
gen_ai.request.temperaturefloatThe temperature parameter passed in the request. Higher values increase randomness, while lower values make output more deterministic.0.7
gen_ai.request.top_pfloatThe top_p parameter used for nucleus sampling, limiting token selection to the top probability mass.0.8
gen_ai.prompt.<message_number>.rolestringRole of the author for each input messageuser
gen_ai.prompt.<message_number>.contentstringContents of the input message (captured when content capture is enabled)What's the temperature in Tel Aviv?
gen_ai.prompt.<message_number>.tool_calls.<tool_call_number>.idstringID of a tool call issued from the promptcall_yPIxaozNPCSp1tJ34Hsbdtzg
gen_ai.prompt.<message_number>.tool_calls.<tool_call_number>.typestringType of tool call issued from the promptfunction
gen_ai.prompt.<message_number>.tool_calls.<tool_call_number>.function.namestringFunction name used in the prompt's tool callget_current_temperature
gen_ai.prompt.<message_number>.tool_calls.<tool_call_number>.function.argumentsstringArguments passed to the prompt's tool call{"location": "Tel Aviv"}
gen_ai.completion.<choice_number>.rolestringRole of the author for each returned choicemodel
gen_ai.completion.<choice_number>.finish_reasonstringFinish reason reported for the choiceSTOP
gen_ai.completion.<choice_number>.contentstringText returned by the model for the choice (captured when content capture is enabled)The current temperature in Tel Aviv is 22°C.
gen_ai.completion.<choice_number>.tool_calls.<tool_call_number>.idstringID of a tool call triggered by the modelcall_O8NOz8VlxosSASEsOY7LDUcP
gen_ai.completion.<choice_number>.tool_calls.<tool_call_number>.typestringType of tool call triggered by the modelfunction
gen_ai.completion.<choice_number>.tool_calls.<tool_call_number>.function.namestringFunction name executed by the model's tool callget_current_temperature
gen_ai.completion.<choice_number>.tool_calls.<tool_call_number>.function.argumentsstringArguments supplied to the model's tool call{"location": "Tel Aviv"}
gen_ai.response.modelstringExact model identifier that produced the responsegemini-2.0-flash-lite
gen_ai.response.idstringUnique identifier assigned to the specific Gemini responseresp-20241010-abcdef1234567890
gen_ai.response.finish_reasonsstring[]Ordered list of finish reasons observed across all choices["STOP"]
gen_ai.usage.input_tokensintNumber of tokens consumed by the prompt66
gen_ai.usage.output_tokensintNumber of tokens generated in the response44