> ## Documentation Index
> Fetch the complete documentation index at: https://traceroot.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Google Gemini

> Auto-instrument Google Gemini API calls via the GenAI SDK

Automatically capture all Gemini model calls made through the `google-genai` SDK.

## Install

> [tab] Python
>
> ```bash theme={null}
> pip install traceroot google-genai
> ```

## Set up

> [tab] Python
>
> ```python theme={null}
> import traceroot
> from traceroot import Integration
>
> traceroot.initialize(integrations=[Integration.GOOGLE_GENAI])
> ```

## Usage

Once initialized, all Gemini calls are captured automatically — including tool-call requests:

```python theme={null}
import os
from google import genai
from google.genai import types

client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])

weather_tool = types.Tool(function_declarations=[
    types.FunctionDeclaration(
        name="get_weather",
        description="Get current weather for a city",
        parameters=types.Schema(
            type="OBJECT",
            properties={"city": types.Schema(type="STRING", description="City name")},
            required=["city"],
        ),
    )
])

# This call is automatically traced
response = client.models.generate_content(
    model="gemini-3.8-flash",
    contents="What's the weather in Tokyo?",
    config=types.GenerateContentConfig(tools=[weather_tool]),
)

# The model asks to call get_weather; run it and send the result back yourself
print(response.function_calls)
```

## Verify

Run your code and confirm the trace arrives:

1. **In the traceroot UI**: Navigate to **Tracing > Traces** in [traceroot Cloud](https://app.traceroot.ai). You should see a trace with root span `GenerateContent` (or `GenerateContentStream` when streaming).
2. **Via the CLI**: Run `traceroot traces list` to inspect your recent traces from the terminal.

## What gets captured

| Attribute | Description |
| - | - |
| Model | `gemini-3.8-flash`, `gemini-3.5-flash-lite`, `gemini-3.1-pro-preview`, etc. |
| Contents | Input messages |
| Response | Generated text and function calls |
| Tokens | Input and output token counts |
| Cost | Calculated when the model ID matches traceroot's pricing table |
| Latency | Request duration |

## Limitations

* When passing Python functions as `tools`, the SDK's automatic function calling executes the entire tool loop inside a single `generate_content` call, which produces one span priced on the final model call only. For accurate per-step spans and cost, pass function declarations or set `automatic_function_calling=types.AutomaticFunctionCallingConfig(disable=True)` and run the loop manually.

## Run the example

Clone the repo and run a complete agent end-to-end.

> [card] [Python](https://github.com/traceroot-ai/traceroot/tree/main/examples/python/gemini-tool-agent)
>
> Run the Python example

## Next steps

> [card] [Python SDK Reference](https://traceroot.ai/docs/tracing/python-sdk.md)
>
> Explore the full Python SDK reference and instrumentation options.

> [card] [Cost Tracking](https://traceroot.ai/docs/tracing/cost-tracking.md)
>
> Learn how traceroot calculates token costs and tracks model spend.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.