Track costs and monitor usage for Google’s Gemini API by routing your requests through LLM Ops. This guide shows you how to integrate using Python, JavaScript, or cURL.
Security Guarantee: LLM Ops does NOT store your API keys, request prompts, or response content. We only track metadata needed for cost analytics.
import google.generativeai as genai# Configure with LLM Ops proxygenai.configure( api_key="AIzaSy...", # Your Google API key transport="rest", client_options={ "api_endpoint": "https://api.llm-ops.cloudidr.com" })# Create modelmodel = genai.GenerativeModel('gemini-2.0-flash-exp')# Make API call with tracking headersresponse = model.generate_content( "What is the capital of France?", request_options={ "headers": { "X-Cloudidr-Token": "cloudidr_..." } })print(response.text)
import google.generativeai as genaigenai.configure( api_key="AIzaSy...", transport="rest", client_options={ "api_endpoint": "https://api.llm-ops.cloudidr.com" })model = genai.GenerativeModel('gemini-2.0-flash-exp')chat = model.start_chat(history=[])headers = { "X-Cloudidr-Token": "cloudidr_...", "X-Agent": "chat-bot"}# All messages are tracked with costsresponse1 = chat.send_message( "Hello! What's your name?", request_options={"headers": headers})print(response1.text)response2 = chat.send_message( "Can you help me with Python?", request_options={"headers": headers})print(response2.text)
import { GoogleGenerativeAI } from '@google/generative-ai';// Initialize with LLM Ops proxyconst genAI = new GoogleGenerativeAI('AIzaSy...', { baseUrl: 'https://api.llm-ops.cloudidr.com'});const model = genAI.getGenerativeModel({ model: 'gemini-2.0-flash-exp'});// Make API call with tracking headersconst result = await model.generateContent( 'What is the capital of France?', { headers: { 'X-Cloudidr-Token': 'cloudidr_...' } });console.log(result.response.text());
import { GoogleGenerativeAI } from '@google/generative-ai';const genAI = new GoogleGenerativeAI('AIzaSy...', { baseUrl: 'https://api.llm-ops.cloudidr.com'});const model = genAI.getGenerativeModel({ model: 'gemini-2.0-flash-exp'});// Streaming is fully supportedconst result = await model.generateContentStream( 'Write a story about a robot learning to paint', { headers: { 'X-Cloudidr-Token': 'cloudidr_...', 'X-Agent': 'story-generator' } });for await (const chunk of result.stream) { process.stdout.write(chunk.text());}
Gemini supports images, video, and audio - all tracked by LLM Ops:
Copy
import google.generativeai as genaifrom PIL import Imagegenai.configure( api_key="AIzaSy...", transport="rest", client_options={ "api_endpoint": "https://api.llm-ops.cloudidr.com" })model = genai.GenerativeModel('gemini-2.0-flash-exp')# Analyze an imageimg = Image.open('photo.jpg')response = model.generate_content( ["What's in this image?", img], request_options={ "headers": { "X-Cloudidr-Token": "cloudidr_...", "X-Agent": "vision-analyzer" } })print(response.text)
Multimodal Token Tracking:Google converts images/video/audio to tokens and includes them in the input_tokens count. LLM Ops tracks the total input tokens returned by Google.Image/video/audio tokens are NOT tracked separately - they’re included in the total input_tokens count.