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

# Google Gemini (TypeScript)

> Trace Google Gemini API calls in TypeScript

## Installation

```bash theme={null}
npm install @fallom/trace @google/generative-ai
```

## Quick Start

```typescript theme={null}
import fallom from "@fallom/trace";
import { GoogleGenerativeAI } from "@google/generative-ai";

// Initialize Fallom once at app startup
await fallom.init({ apiKey: process.env.FALLOM_API_KEY });

// Create a session for this conversation/request
const session = fallom.session({
  configKey: "my-app",
  sessionId: "session-123",
  customerId: "user-456",
});

// Create and wrap the model
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY!);
const model = session.wrapGoogleAI(
  genAI.getGenerativeModel({ model: "gemini-1.5-pro" })
);

// Use as normal - automatically traced!
const result = await model.generateContent("Hello!");

console.log(result.response.text());
```

## With System Instruction

```typescript theme={null}
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY!);

const model = session.wrapGoogleAI(
  genAI.getGenerativeModel({
    model: "gemini-1.5-pro",
    systemInstruction: "You are a helpful assistant.",
  })
);

const result = await model.generateContent("Tell me about TypeScript.");
```

## Chat Sessions

```typescript theme={null}
const chat = model.startChat({
  history: [
    { role: "user", parts: [{ text: "Hello!" }] },
    { role: "model", parts: [{ text: "Hi there! How can I help?" }] },
  ],
});

// Each message is traced
const result = await chat.sendMessage("What's the weather like?");
```

## Streaming

```typescript theme={null}
const result = await model.generateContentStream("Write a poem about coding.");

for await (const chunk of result.stream) {
  process.stdout.write(chunk.text());
}
```

## Model A/B Testing

```typescript theme={null}
import fallom from "@fallom/trace";
import { GoogleGenerativeAI } from "@google/generative-ai";

await fallom.init({ apiKey: "your-fallom-api-key" });

const session = fallom.session({
  configKey: "my-experiment",
  sessionId: "session-123",
});

const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY!);

// Get assigned model for this session
const modelId = await session.getModel({ fallback: "gemini-1.5-flash" });

const model = session.wrapGoogleAI(genAI.getGenerativeModel({ model: modelId }));

const result = await model.generateContent("Hello!");
```

## What Gets Traced

| Field       | Description                          |
| ----------- | ------------------------------------ |
| Model       | `gemini-1.5-pro`, `gemini-1.5-flash` |
| Duration    | Total request time (ms)              |
| Tokens      | Input, output tokens                 |
| Cost        | Calculated from token usage          |
| Prompts     | Input content                        |
| Completions | Model response                       |
| Session     | Your config key + session ID         |

## Next Steps

<CardGroup cols={2}>
  <Card title="Model A/B Testing" icon="flask" href="/model-testing">
    Compare Gemini models in production.
  </Card>

  <Card title="Vercel AI SDK" icon="bolt" href="/integrations/typescript/vercel-ai">
    Use Gemini with Vercel AI SDK.
  </Card>
</CardGroup>
