Google AI
Auto-instrumentation for Google Gemini API.
Risicare automatically instruments the Google Generative AI SDK.
Installation
pip install risicare google-generativeaiBasic 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
| Method | Traced |
|---|---|
GenerativeModel.generate_content | Yes (sync) |
GenerativeModel.generate_content_async | Yes (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
| Attribute | Description |
|---|---|
gen_ai.system | google |
gen_ai.request.model | Model name |
gen_ai.request.stream | Whether streaming was requested |
gen_ai.usage.prompt_tokens | Input tokens (from prompt_token_count) |
gen_ai.usage.completion_tokens | Output tokens (from candidates_token_count) |
gen_ai.usage.total_tokens | Total tokens (from total_token_count) |
gen_ai.completion.finish_reason | Stop reason |
gen_ai.latency_ms | Request latency in milliseconds |
gen_ai.response.candidates | Number of response candidates |
gen_ai.prompt.parts | Number of prompt parts |
Cost Tracking
| Model | Input (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 |