Gemini

Auto-instrument the Google Gemini SDK. Every generate_content call inside an active run emits an llm_call event with model, token usage, and latency. Cost is computed server-side from the rate card.

Install / enable

Python:

pip install "agentping-sdk[gemini]"
import agentping
from google import genai

agentping.init()
agentping.instrument_gemini()

client = genai.Client()

with agentping.run("competitor-scan") as r:
    reply = client.models.generate_content(
        model="gemini-2.0-flash",
        contents="summarise the top 5 changes",
    )
    r.score("confidence", 0.91)

instrument_gemini() patches sync and async Models.generate_content, the streaming variant, and embed_content. Idempotent; safe to call once at module load. Supports google-genai >= 1.0.0.

TypeScript:

npm install @agentping/sdk @google/genai
import * as agentping from "@agentping/sdk";
import { GoogleGenAI } from "@google/genai";

agentping.init({ apiKey: process.env.AGENTPING_API_KEY });

const run = agentping.run("competitor-scan");
const genai = agentping.instrumentGemini(
  new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }),
  { run },
);

const reply = await genai.models.generateContent({
  model: "gemini-2.0-flash",
  contents: "summarise the top 5 changes",
});

await run.finish({ status: "success" });

instrumentGemini returns a wrapped client whose return types match the original.

What's captured

Model, input tokens, output tokens, and latency on every call. When cached_content_token_count is set, the cached portion is split into cached_input_tokens so prompt-cache hits price at the lower rate. Embedding calls (embed_content) are auto-captured too, recorded as kind: "embedding" with their own rate-card pricing. The event's provider is gemini, including for Vertex AI (genai.Client(vertexai=True, ...)); the model field carries the model ID you supplied.

Streaming

Automatic. generate_content_stream is wrapped: chunks pass through untouched and one llm_call fires when the stream drains, carrying the final usage_metadata. No manual handling needed.

Source / notes

The rate card carries the current Gemini Flash and Pro model families; unknown models still land, with cost shown as null in the unpriced models report.