Skip to main content
Google Gemini is Google’s family of multimodal models, including Gemini 3.1 Pro and Gemini 3.6 Flash. Braintrust traces Gemini requests across text, streaming, structured outputs, function calling, and multimodal generation, lets you run experiments against Gemini models, and routes to them from the playground.
Trace Gemini calls from your TypeScript application, then run experiments against Gemini models.

Tracing

Trace Gemini calls to Braintrust with the native Google GenAI SDK, or route them through the Braintrust gateway.

Setup

Install the Braintrust and Google GenAI SDKs, then set your API keys.
1

Install packages

2

Set environment variables

.env

Auto-instrumentation

To trace Gemini calls without modifying your application code, run your app with Braintrust’s import hook. The hook patches the Google GenAI SDK at startup, so calls from an unwrapped client are traced automatically.
trace-gemini-auto.js
Run your app with the import hook:
The auto-instrumentation example uses plain JavaScript so node --import can run the file directly. The Braintrust APIs work the same in TypeScript projects — compile your TypeScript to JavaScript, then run the compiled file with the import hook.
If you’re using a bundler, see Trace LLM calls for plugin and loader setup.

Manual instrumentation

To trace Gemini calls manually, wrap the Google GenAI module yourself with wrapGoogleGenAI. Every call from the wrapped client is then logged to Braintrust.

Gateway

To route Gemini calls through the Braintrust gateway, point the OpenAI SDK at the gateway base URL with your Braintrust API key. Routing and logging happen server-side, so you can reach Gemini through any supported provider’s SDK.Install the braintrust and openai packages.
Initialize the client and make a request to a Gemini model through the gateway.

Log to a project

To log gateway calls to a specific project, initialize a logger with initLogger.

Streaming

Stream Gemini responses through the gateway.

Embeddings

The gateway also supports Gemini’s native embedContent and batchEmbedContents endpoints, including multimodal text and image content. See Generate embeddings for an example.

Streaming

Stream responses from the native Google GenAI client with automatic tracing.
The wrapper aggregates streamed chunks and records streaming token metrics.

Structured outputs

Gemini supports structured JSON outputs using response schemas.

Function calling

Gemini supports function calling for building AI agents with tools.

Multimodal content

Gemini models support multimodal inputs including images, audio, and video.

Reasoning models

Gemini 3 models like gemini-3.6-flash and gemini-3.1-pro have built-in reasoning capabilities enabled by default. Configure reasoning behavior with thinkingConfig.

Context caching

Gemini supports context caching for efficient reuse of large contexts.

Error handling, attachments, and masking sensitive data

To learn more about these topics, check out the customize traces guide.
To learn more about multimodal support, attachments, error handling, and masking sensitive data with Gemini, visit the customize traces guide.

What Braintrust traces

Braintrust captures:
  • Content generation spans (generate_content and generate_content_stream), with the request as input and the full response as output. Streaming adds time to first token.
  • Embedding spans (embed_content), with embedding count and length and token usage.
  • Interaction spans (create_interaction) for foreground interactions.create calls, including streaming, with the interaction input, output, and usage.
  • Token metrics: prompt, completion, total (tokens), cached (prompt_cached_tokens), reasoning (completion_reasoning_tokens), and audio and image modality tokens.
  • Google Search grounding metadata on grounded responses.
  • Binary inputs (images, audio, and documents) uploaded as Braintrust attachments.

Tracing resources

Evals

Evaluate the output of Gemini models by running experiments with Braintrust Eval. Each eval combines a dataset, a task, and one or more scorers. To learn more, see the Experiments guide.

Native SDK

Run experiments that call Gemini through the native Google GenAI SDK.

Gateway

Run experiments that call Gemini through the Braintrust gateway.

Add Gemini as an AI provider

Add your Gemini API key to Braintrust to route requests through the gateway and use Gemini models from the playground.
1

Get a Gemini API key

Get a Gemini API key from Google AI Studio.
2

Add the key to Braintrust

Go to Settings > AI providers and add the Gemini API key as an organization or project AI provider.
API keys are stored as one-way cryptographic hashes, never in plaintext.

Gemini provider resources