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

# Google Generative AI

> Monitor and analyze your Google Gemini API calls with AgentOps

AgentOps provides seamless integration with [Google's Generative AI API](https://ai.google.dev/), allowing you to monitor and analyze all your Gemini model interactions automatically.

## Installation

<CodeGroup>
  ```bash pip theme={null}
  pip install agentops google-genai
  ```

  ```bash poetry theme={null}
  poetry add agentops google-genai
  ```

  ```bash uv theme={null}
  uv pip install agentops google-genai
  ```
</CodeGroup>

## Setting Up API Keys

Before using Google Gemini with AgentOps, you need to set up your API keys. You can obtain:

* **GOOGLE\_API\_KEY**: From the [Google AI Studio](https://aistudio.google.com/app/apikey)
* **AGENTOPS\_API\_KEY**: From your [AgentOps Dashboard](https://app.agentops.ai/)

Then to set them up, you can either export them as environment variables or set them in a `.env` file.

<CodeGroup>
  ```bash Export to CLI theme={null}
  export GOOGLE_API_KEY="your_google_api_key_here"
  export AGENTOPS_API_KEY="your_agentops_api_key_here"
  ```

  ```txt Set in .env file theme={null}
  GOOGLE_API_KEY="your_google_api_key_here"
  AGENTOPS_API_KEY="your_agentops_api_key_here"
  ```
</CodeGroup>

Then load the environment variables in your Python code:

```python theme={null}
from dotenv import load_dotenv
import os

# Load environment variables from .env file
load_dotenv()

# Set up environment variables with fallback values
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY")
```

## Usage

Initialize AgentOps at the beginning of your application to automatically track all Gemini API calls.

<CodeGroup>
  ```python Streaming theme={null}
  import agentops
  from google import genai

  # Initialize AgentOps
  agentops.init()

  # Create a client
  client = genai.Client(api_key="YOUR_GEMINI_API_KEY")

  # Generate streaming content
  for chunk in client.models.generate_content_stream(
      model='gemini-2.0-flash-001',
      contents='Explain quantum computing in simple terms.',
  ):
      print(chunk.text, end="", flush=True)
  ```

  ```python Simple Chat theme={null}
  import agentops
  from google import genai

  # Initialize AgentOps
  agentops.init()

  # Create a client
  client = genai.Client(api_key="YOUR_GEMINI_API_KEY")

  # Start a chat session
  chat = client.chats.create(model='gemini-2.0-flash-001')

  # Send messages and get responses
  response = chat.send_message('Hello, how can you help me with AI development?')
  print(response.text)

  # Continue the conversation
  response = chat.send_message('What are the best practices for prompt engineering?')
  print(response.text)
  ```
</CodeGroup>

## Examples

<CardGroup cols={2}>
  <Card title="Gemini Quickstart Notebook" icon="notebook" href="/v2/examples/google_generative_ai">
    Basic Gemini usage with AgentOps
  </Card>
</CardGroup>

For more information on using the Google Gen AI SDK, refer to the [official documentation](https://googleapis.github.io/python-genai/).

<script type="module" src="/scripts/github_stars.js" />

<script type="module" src="/scripts/scroll-img-fadein-animation.js" />

<script type="module" src="/scripts/button_heartbeat_animation.js" />

<script type="css" src="/styles/styles.css" />
