Trace Gemini calls from your TypeScript application, then run experiments against Gemini models.Run your app with the import hook:The auto-instrumentation example uses plain JavaScript so Initialize the client and make a request to a Gemini model through the gateway.The wrapper aggregates streamed chunks and records streaming token metrics.Legacy SDK (
The The auto-instrumentation example uses plain JavaScript so
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
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 withwrapGoogleGenAI. 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 thebraintrust and openai packages.Log to a project
To log gateway calls to a specific project, initialize a logger withinitLogger.Streaming
Stream Gemini responses through the gateway.Embeddings
The gateway also supports Gemini’s nativeembedContent 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.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 likegemini-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.What Braintrust traces
Braintrust captures:- Content generation spans (
generate_contentandgenerate_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 foregroundinteractions.createcalls, 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
- Google GenAI SDK reference for the API surface being traced.
braintruston npm for the tracing package.- Trace LLM calls for tracing patterns across providers.
Evals
Evaluate the output of Gemini models by running experiments with BraintrustEval. 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.Legacy SDK (@google/generative-ai)
The @google/generative-ai package (v0.24.x) is the older Google Generative AI SDK for TypeScript, separate from the newer @google/genai SDK documented above. Braintrust supports auto-instrumentation and manual instrumentation for this legacy SDK.Auto-instrumentation
Install the Braintrust and@google/generative-ai packages, then run your app with the import hook. Requires @google/generative-ai v0.24.0 or later.1
Install packages
2
Initialize Braintrust and call the legacy SDK
trace-legacy-gemini-auto.js
3
Run with the import hook
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 legacy SDK clients manually, wrap the client withwrapGoogleGenerativeAI().What Braintrust traces
Braintrust patches the@google/generative-ai SDK and creates a span per call:- Content generation spans (
generate_content,generate_content_stream): input contents, model, generation config, safety settings, and tools as metadata. Output includes response text, token usage (prompt_tokens,completion_tokens,tokens), andtime_to_first_tokenon streaming calls. - Embedding spans (
embed_content,batch_embed_contents): input text and model as metadata. - Chat spans (
send_message,send_message_stream): message input and model as metadata. Streaming calls also capturetime_to_first_token. - Images handled as Braintrust attachments.
- Errors captured on every call.
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
- AI providers for configuring provider keys and scopes.
- Playgrounds for comparing Gemini models against other providers.
- Google AI Studio for managing Gemini API keys.