Monitoring Google Gemini models

Google Gemini is a family of multimodal AI models designed to understand and process information across text, code, images, audio, and video. This guide shows you how to instrument an application using Google Gemini models with OpenLLMetry to send telemetry data to Instana.

Prerequisites

Make sure that the following prerequisites are met:

Instrumenting your Google Gemini application

  1. Install the required packages.

    pip install google-genai traceloop-sdk
  2. Export your Google API key.
    export GOOGLE_API_KEY="<your-google-api-key>"
  3. Create your Google Gemini application. Create a Python file with the following code:

    from google import genai
    from traceloop.sdk import Traceloop
    from traceloop.sdk.decorators import workflow
    
    # Initialize OpenLLMetry
    Traceloop.init(app_name="gemini_chat_app", disable_batch=True)
    
    # Initialize Gemini client
    client = genai.Client()
    
    @workflow(name="gemini_conversation")
    def ask_gemini(question: str):
        """Send a question to Gemini and get a response."""
    
        chat = client.chats.create(model="gemini-2.0-flash-exp")
        response = chat.send_message(question)
    
        return response.text
    
    # Example usage
    if __name__ == "__main__":
        questions = [
            "What is Artificial Intelligence?",
            "How does machine learning work?"
        ]
    
        for question in questions:
            print(f"\nQuestion: {question}")
            answer = ask_gemini(question)
            print(f"Answer: {answer}\n")
            print("-" * 80)
  4. Run your application.

    python3 gemini_app.py

    The application will send questions to Gemini and display the responses. OpenLLMetry automatically captures traces for each API call and sends them to Instana.

  5. View data on Instana.

    After running your application, the following items are displayed on the Instana Gen AI observability dashboard:

    • Model used
    • Token usage (input and output tokens)
    • Response latency
    • Request and response content

Troubleshooting

For common issues such as traces not appearing or connection errors, see Troubleshooting.

Authentication errors

If you encounter authentication errors:

  1. Verify your GOOGLE_API_KEY is set correctly
  2. Check whether your API key is valid in Google AI Studio
  3. Make sure that your API key is not expired or revoked
  4. Verify your API key has access to the Gemini API

Rate limiting errors

If you encounter rate limit errors:

  1. Check your Google AI Studio account's rate limits
  2. Add delays between requests if making multiple calls
  3. Consider upgrading your plan for higher limits
  4. Implement exponential backoff for retries

Model not found errors

If you encounter model not found errors:

  1. Verify the model name is correct (for example, gemini-2.0-flash-exp, gemini-pro)
  2. Check whether the model is available in your region
  3. Make sure your API key has access to the specified model
  4. Refer to Google's Gemini documentation for available models

Next steps