Skip to main content
GitHub

Google AI

Auto-instrumentation for Google Gemini API.

Risicare automatically instruments the Google Generative AI SDK.

Installation

pip install risicare google-generativeai

Basic Usage

import risicare
import google.generativeai as genai
 
risicare.init()
 
genai.configure(api_key="your-api-key")
model = genai.GenerativeModel("gemini-1.5-pro")
 
# Automatically traced
response = model.generate_content("What is the capital of France?")

Supported Methods

MethodTraced
GenerativeModel.generate_contentYes (sync)
GenerativeModel.generate_content_asyncYes (async)

Streaming is supported via the stream=True parameter on generate_content.

Streaming

Streaming responses are fully supported:

response = model.generate_content(
    "Write a poem about coding",
    stream=True
)
 
for chunk in response:
    print(chunk.text, end="")

Chat Sessions

Multi-turn conversations are traced as a session:

chat = model.start_chat()
 
response1 = chat.send_message("Hello!")
response2 = chat.send_message("What did I just say?")

Function Calling

Function declarations are captured:

def get_weather(location: str) -> str:
    return f"Weather in {location}: Sunny"
 
model = genai.GenerativeModel(
    "gemini-1.5-pro",
    tools=[get_weather]
)
 
response = model.generate_content("What's the weather in Paris?")

Captured Attributes

AttributeDescription
gen_ai.systemgoogle
gen_ai.request.modelModel name
gen_ai.request.streamWhether streaming was requested
gen_ai.usage.prompt_tokensInput tokens (from prompt_token_count)
gen_ai.usage.completion_tokensOutput tokens (from candidates_token_count)
gen_ai.usage.total_tokensTotal tokens (from total_token_count)
gen_ai.completion.finish_reasonStop reason
gen_ai.latency_msRequest latency in milliseconds
gen_ai.response.candidatesNumber of response candidates
gen_ai.prompt.partsNumber of prompt parts

Cost Tracking

ModelInput (per 1M)Output (per 1M)
gemini-2.0-pro$1.25$5.00
gemini-2.0-flash$0.10$0.40
gemini-1.5-pro$1.25$5.00
gemini-1.5-flash$0.075$0.30
gemini-1.5-flash-8b$0.0375$0.15

Next Steps