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

# Gemini

> Record prompts, models, tokens, and cost for Gemini calls through the Google Gen AI SDK

Bitfab records `generateContent` / `generate_content` requests your code sends to Gemini through the official Google Gen AI SDK (`@google/genai` in TypeScript, `google-genai` in Python): the prompt, the model, token counts, and latency. Bitfab works out the cost from the model and the token counts.

**Canonical signatures:** [TypeScript `wrapGemini`](https://docs.bitfab.ai/reference/typescript#wrapgemini) · [Python `wrap_gemini`](https://docs.bitfab.ai/reference/python#wrap-gemini)

## Language Support

| Language | API | Status |
| - | - | - |
| TypeScript | `wrapGemini()`, plus automatic capture under `withTrace()` with the build transform | ✅ Supported |
| Python | `wrap_gemini()`, plus automatic capture under `trace()` | ✅ Supported |

## Under a trace root

Beneath a trace root, every `models.generateContent` / `models.generate_content` call is recorded with no wrapper, as its own `llm` span named `Models.generateContent` (Python: `Models.generate_content`, or `AsyncModels.generate_content` for `client.aio`). The span attaches under the function that made the call.

```typescript TypeScript theme={null}
import { GoogleGenAI } from "@google/genai"
import { Bitfab } from "@bitfab/sdk"

const bitfab = new Bitfab({ apiKey: process.env.BITFAB_API_KEY })
const ai = new GoogleGenAI({})

async function classify(email: string) {
  const response = await ai.models.generateContent({
    model: "gemini-2.5-flash",
    contents: email,
    config: { systemInstruction: "You classify email." },
  })
  return response.text
}

export const detect = bitfab.withTrace("inbox-detection", (email: string) =>
  classify(email),
)
```

```python Python theme={null}
from google import genai
from google.genai import types
from bitfab import Bitfab

bitfab = Bitfab()
ai = genai.Client()


def classify(email: str) -> str:
    response = ai.models.generate_content(
        model="gemini-2.5-flash",
        contents=email,
        config=types.GenerateContentConfig(system_instruction="You classify email."),
    )
    return response.text


@bitfab.trace("inbox-detection", type="llm")
def detect(email: str) -> str:
    return classify(email)
```

The trace shows `detect`, then `classify`, then an `llm` span named `Models.generateContent` (`Models.generate_content` in Python) carrying the prompt, tokens, and cost.

In TypeScript, the [build transform](https://docs.bitfab.ai/typescript-sdk.md#experimental-subtree-tracing) rewrites each `.models.generateContent(...)` and `.models.generateContentStream(...)` call site in your code, so it works in bundled apps and the SDK never loads or patches `@google/genai`. In Python, the SDK patches the `google-genai` models classes when a `trace()` root is set up.

`exclude` on the root matches the method name (`"generateContent"`, `"generateContentStream"` / `"generate_content"`, `"generate_content_stream"`) or the span name, and `withNode()` / `node()` on a wrapper (below) configures the one span.

## Streaming

A `generateContentStream` / `generate_content_stream` call is recorded as one `llm` span named `Models.generateContentStream` (`Models.generate_content_stream` in Python) when the stream finishes, is closed, or fails, with usage read from the last chunk. What your code reads from the stream is unchanged. A stream returned from the trace root keeps the trace open until your code reads it. A stream your code drops without reading to the end is recorded when the runtime frees it, with the request and whatever was read but usually no usage, and its trace finishes then. Streamed calls always run for real during a replay.

A `withNode()` / `node()` placed directly on the client method records the stream on its own span instead of opening a second one. That span ends when the stream does, and the node's `finalize` is not used.

## Wrapping a call

Use `wrapGemini()` / `wrap_gemini()` when you trace with `withSpan()` / `span()` instead of a trace root. It wraps the function that sends the request and writes the prompt and usage into the span that is current when the call starts. It opens no span of its own.

```typescript TypeScript theme={null}
const generate = bitfab.wrapGemini(ai.models.generateContent)

const classify = bitfab.withSpan(
  "classify-email",
  { type: "llm" },
  async (email: string) => {
    const response = await generate({ model: "gemini-2.5-flash", contents: email })
    return response.text
  },
)
```

```python Python theme={null}
generate = bitfab.wrap_gemini(ai.models.generate_content)


@bitfab.span("classify-email", type="llm")
def classify(email: str) -> str:
    return generate(model="gemini-2.5-flash", contents=email).text
```

Beneath a trace root, a wrapper around a call that is already recorded records nothing more. `onResponse` / `on_response` receives each response after it is recorded. Wrappers never record a stream.

## No duplicate spans

Each model call is recorded once:

* A wrapper around a call the trace root already recorded adds nothing.
* A call the [LangChain callback handler](https://docs.bitfab.ai/frameworks/langgraph.md) is recording as an LLM run is not recorded a second time, including a call made inside your own LangChain chat model class, streamed or not. A call made alongside a LangChain run in the same trace is still recorded, except beside the first LangChain run in a TypeScript process (see [Limitations](#limitations)).
* In TypeScript, calls made inside framework libraries in `node_modules` (such as `@langchain/google-genai`) are never rewritten by the build transform, so only the framework's own integration records them. In Python, the SDK patches the client classes, so a call another library makes through the client is recorded too, unless that library has a Bitfab integration recording it.

## What gets captured

| Field | Value |
| - | - |
| Prompt | The request's system instruction as a `system` turn, then its `contents` (a string becomes one `user` turn), as a JSON list of `role` and `content` |
| `provider` | `"gemini"` |
| `model` | The response's `modelVersion`, else the request's model, without any `models/` prefix |
| `inputTokens` | `usageMetadata.promptTokenCount` plus `toolUsePromptTokenCount` |
| `outputTokens` | `usageMetadata.candidatesTokenCount` plus `thoughtsTokenCount` |
| `cachedInputTokens` | `usageMetadata.cachedContentTokenCount` |
| `reasoningTokens` | `usageMetadata.thoughtsTokenCount` |
| `durationMs` | How long the request took |

The context fields go in one context entry on the span. A key the response does not carry is left out. Thinking tokens are billed at the output rate, so they are counted in `outputTokens` and also reported on their own as `reasoningTokens`.

## Limitations

* **Only `generateContent` and `generateContentStream`.** Embeddings (`embedContent`), image and video generation, token counting, and the Live API are not recorded.
* **Calls outside any trace are not recorded.** A wrapper used outside a span or trace records nothing and never starts a trace of its own.
* **TypeScript automatic capture needs the build transform.** Code the transform does not compile (for example a call made from a dependency) is not rewritten; wrap it instead.
* **TypeScript: calls beside the first LangChain run in a process.** The SDK hooks into LangChain in the background when the first Bitfab LangChain handler or `withTrace()` root is created. If a LangChain run starts before that finishes, calls made inside it are still recorded once, but a call made at the same time outside it (for example in `Promise.all`) is not recorded while that first run is open. Later runs are not affected.
* **Python automatic capture needs Python 3.12+**, the same as `trace()` subtree capture.


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