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:
- Python 3.8 or later
- A Google API key for Gemini (get one from Google AI Studio)
- An Instana backend account with Generative AI observability enabled
- Review of Getting Started with Generative AI Observability on agent and agentless modes
Instrumenting your Google Gemini application
-
Install the required packages.
pip install google-genai traceloop-sdk -
Export your Google API key.
export GOOGLE_API_KEY="<your-google-api-key>" -
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) -
Run your application.
python3 gemini_app.pyThe application will send questions to Gemini and display the responses. OpenLLMetry automatically captures traces for each API call and sends them to Instana.
-
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:
- Verify your
GOOGLE_API_KEYis set correctly - Check whether your API key is valid in Google AI Studio
- Make sure that your API key is not expired or revoked
- Verify your API key has access to the Gemini API
Rate limiting errors
If you encounter rate limit errors:
- Check your Google AI Studio account's rate limits
- Add delays between requests if making multiple calls
- Consider upgrading your plan for higher limits
- Implement exponential backoff for retries
Model not found errors
If you encounter model not found errors:
- Verify the model name is correct (for example,
gemini-2.0-flash-exp,gemini-pro) - Check whether the model is available in your region
- Make sure your API key has access to the specified model
- Refer to Google's Gemini documentation for available models
Next steps
- Explore LLM providers supported by Instana
- Learn about cost calculation for your LLM usage
- Set up alerts for your Google Gemini API usage
- Review Google Gemini documentation for model capabilities