Integrations / Google Gemini

Trace Google Gemini API calls

To trace Gemini with Tracify, point the OpenAI client at Google's OpenAI-compatible endpoint and wrap it with the Tracify SDK. If you use Google's own google-genai library, record each call as a step instead.

Your code

Google Gemini

Tracify

One run.

  • 01Gemini model name
  • 02Prompt and output token counts
  • 03Latency and errors
  • 04Calls grouped into the run that made them

Setup

5 steps.

  1. 01

    Install

    Gemini speaks the OpenAI chat format on a compatible endpoint, so the OpenAI library is all you need.

    bash
    pip install tracify-sdk openai
  2. 02

    Set your project key

    Create a project in Tracify, copy its API key and put it in the environment where your code runs. The SDK reads it from there, so nothing is hard-coded.

    bash
    export TRACIFY_API_KEY="tracify_sk_live_..."
  3. 03

    Option A: the OpenAI-compatible endpoint

    Create the OpenAI client with your Gemini key and Google's compatible base URL, then wrap it. Calls to chat.completions are recorded automatically.

    python
    import os
    import tracify
    from openai import OpenAI
    
    gemini = tracify.wrap_openai(OpenAI(
        api_key=os.environ["GEMINI_API_KEY"],
        base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
    ))
    
    reply = gemini.chat.completions.create(
        model="gemini-2.5-flash",
        messages=[{"role": "user", "content": "Hello"}],
    )
  4. 04

    Option B: Google's own google-genai library

    There is no automatic wrapper for google-genai yet, so record the call as a step and copy the token counts from the response. The model call is then still visible in the run, with its tokens and time.

    python
    import tracify
    from google import genai
    
    client = genai.Client()
    
    def ask(prompt: str) -> str:
        with tracify.span("gemini", type="llm", input=prompt) as step:
            response = client.models.generate_content(
                model="gemini-2.5-flash", contents=prompt
            )
            usage = response.usage_metadata
            step.set(
                model="gemini-2.5-flash",
                input_tokens=usage.prompt_token_count,
                output_tokens=usage.candidates_token_count,
            )
            step.set_output(response.text)
            return response.text
  5. 05

    Run it once and look

    Run your code one time, then open the Tracing page of your project. Your Gemini call shows up as a run with its steps in order, plus the tokens, time and any error for each call.

Good to know

Shown with the OpenAI label

The wrapper reads the OpenAI call format, so calls are labeled as OpenAI-style in the trace. The model name you send is always the one shown, so you can still tell models apart.

Good to know

What is automatic and what is manual

Option A is fully automatic. Option B is a few lines you write yourself, because the google-genai library is not wrapped by the SDK yet.

Good to know

About cost

Tracify records the tokens the provider returns. A dollar cost is shown when you give the wrapper your own rates with calculate_cost (Python) or calculateCost (TypeScript), so the number always matches your real price plan.

Full details in the documentation. All integrations are listed on the integrations page.

Does tracing Gemini change my requests or responses?
No. The wrapper passes your call straight through and records a copy. Your request, the response and any error reach your code unchanged.
Which Gemini models can I trace?
Any model your key can call. Tracify records the model name you pass, so new Gemini versions need no update on our side.
Can I trace Gemini inside a bigger agent?
Yes. Wrap the agent entry point with trace_agent and the Gemini calls inside it join that run.

Send your first trace.

Free to start.