Google AI
Auto-instrumentation for Google Gemini API.
Risicare automatically instruments the Google Generative AI SDK.
Installation
pip install risicare google-generativeaiBasic Usage
import google.generativeai as genai
import risicare
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) |
A streaming response is traced via the stream=True parameter on generate_content.
Streaming
A streaming response is traced:
response = model.generate_content(
"Write a poem about coding",
stream=True
)
for chunk in response:
print(chunk.text, end="")google-genai
The SDK also traces google-genai (module google.genai), the current Google SDK. It is not the package google-generativeai of the sections above.
pip install risicare google-genai| Method | Traced |
|---|---|
client.models.generate_content | Yes (sync) |
client.models.generate_content_stream | Yes (sync) |
client.aio.models.generate_content and generate_content_stream | Yes (async) |
client.chats | Yes |
- The Gemini API and Vertex AI (
Client(vertexai=True)) are traced. The provider name (gen_ai.system) isgooglefor the Gemini API andvertexaifor a Vertex AI client. - One call can send more than one request to the model: the automatic function calling loop sends one request for each turn, and the automatic continuation (google-genai 2.30.0 and later) sends one for each hop. The SDK makes one LLM span for each request, with the tokens of that request. A tool that the loop calls runs between two requests, so its span is not in an LLM span.
- The cache and reasoning token counts (
gen_ai.usage.cache_read.input_tokens,gen_ai.usage.reasoning.output_tokens) come from the cached and the thinking tokens of the response, when the response gives them. - Known limit: the SDK wraps private methods of google-genai (
_generate_contentand_generate_content_stream). When a release does not have one of them, the SDK traces the public method in its place: one span for each call.get_instrumented_modules()["google.genai"]["state"]is thenpartially_instrumented, with a WARNING. - Known limit: a resumed stream. google-genai 2.30.0 and later resume a stream that breaks after a continuation token. Your code gets the whole answer and no exception, but the request that broke has an error span. The dashboard shows an error in the trace, and a
trace.errorwebhook fires if you set one. The run itself is not marked failed.
Chat Sessions
The integration does not group a chat into a session: each turn is a separate
trace. To group the turns, wrap the chat in session_context():
from risicare import session_context
chat = model.start_chat()
with session_context(session_id="chat-123"):
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 |