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

# Gemini (Google)

> Track costs for Gemini 2.0, Gemini 1.5 Pro, Gemini 1.5 Flash, and all Google AI models

This guide covers how to send Google AI usage data to Fenra.

## Gemini API

<CodeGroup>
  ```javascript Node.js theme={null}
  import { GoogleGenerativeAI } from '@google/generative-ai';

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

  async function chat(prompt) {
    const model = genAI.getGenerativeModel({ model: 'gemini-1.5-pro' });
    const result = await model.generateContent(prompt);
    const usage = result.response.usageMetadata;

    // Send to Fenra
    await fetch('https://ingest.fenra.io/usage/transactions', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-Api-Key': process.env.FENRA_API_KEY
      },
      body: JSON.stringify({
        provider: 'gemini',
        model: 'gemini-1.5-pro',
        usage: [{
          type: 'tokens',
          metrics: {
            input_tokens: usage.promptTokenCount,
            output_tokens: usage.candidatesTokenCount,
            total_tokens: usage.totalTokenCount
          }
        }],
        context: {
          billable_customer_id: process.env.BILLABLE_CUSTOMER_ID
        }
      })
    });

    return result.response;
  }
  ```

  ```python Python theme={null}
  import google.generativeai as genai
  import requests
  import os

  genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))

  def chat(prompt):
      model = genai.GenerativeModel('gemini-1.5-pro')
      response = model.generate_content(prompt)
      usage = response.usage_metadata

      # Send to Fenra
      requests.post(
          'https://ingest.fenra.io/usage/transactions',
          headers={
              'Content-Type': 'application/json',
              'X-Api-Key': os.getenv('FENRA_API_KEY')
          },
          json={
              'provider': 'gemini',
              'model': 'gemini-1.5-pro',
              'usage': [{
                  'type': 'tokens',
                  'metrics': {
                      'input_tokens': usage.prompt_token_count,
                      'output_tokens': usage.candidates_token_count,
                      'total_tokens': usage.total_token_count
                  }
              }],
              'context': {
                  'billable_customer_id': os.getenv('BILLABLE_CUSTOMER_ID')
              }
          }
      )

      return response
  ```
</CodeGroup>

## Context Caching

When using context caching, include cached tokens:

```javascript theme={null}
usage: [{
  type: 'tokens',
  metrics: {
    input_tokens: usage.promptTokenCount,
    output_tokens: usage.candidatesTokenCount,
    total_tokens: usage.totalTokenCount,
    cached_tokens: usage.cachedContentTokenCount || 0
  }
}]
```

## Multimodal Inputs

Gemini handles images, video, and audio natively. Token counts include all modalities, so no separate tracking is needed.

## Thinking Tokens

For Gemini 2.5 models with thinking enabled, include thinking tokens:

```javascript theme={null}
usage: [{
  type: 'tokens',
  metrics: {
    input_tokens: usage.promptTokenCount,
    output_tokens: usage.candidatesTokenCount,
    total_tokens: usage.totalTokenCount,
    thinking_tokens: usage.thoughtsTokenCount || 0
  }
}]
```

## Grounding with Google Search

When using Google Search grounding, track the tool invocations:

```javascript theme={null}
const result = await model.generateContent({
  contents: prompt,
  tools: [{ functionDeclarations: [searchTool] }]
});

// Check if grounding was used
const groundingMetadata = result.response.groundingMetadata;
const searchQueries = groundingMetadata?.webSearchQueries || [];

await fetch('https://ingest.fenra.io/usage/transactions', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-Api-Key': process.env.FENRA_API_KEY
  },
  body: JSON.stringify({
    provider: 'gemini',
    model: 'gemini-1.5-pro',
    usage: [
      {
        type: 'tokens',
        metrics: {
          input_tokens: usage.promptTokenCount,
          output_tokens: usage.candidatesTokenCount,
          total_tokens: usage.totalTokenCount
        }
      },
      {
        type: 'requests',
        metrics: {
          count: searchQueries.length,
          request_type: 'google_grounding'
        }
      }
    ],
    context: {
      billable_customer_id: process.env.BILLABLE_CUSTOMER_ID
    }
  })
});
```

## Supported Models

Fenra supports all Google Gemini models. Common models include:

| Model                 | Context Window |
| --------------------- | -------------- |
| `gemini-2.0-flash`    | 1M tokens      |
| `gemini-1.5-pro`      | 2M tokens      |
| `gemini-1.5-flash`    | 1M tokens      |
| `gemini-1.5-flash-8b` | 1M tokens      |

## Next Steps

* [Set up alerts](/guides/alerts) to monitor Gemini spending
* [Compare providers](/product-overview/cost-explorer/breakdown) in Cost Explorer
