Skip to main content

Installation

pip install meshai-sdk[gemini]

Usage

from meshai import MeshAI
from meshai.integrations.gemini import wrap_gemini
from google import genai

client = MeshAI(api_key="msh_...", agent_name="my-agent")
client.register(framework="custom", model_provider="google")

# Wrap the Gemini client
gem = wrap_gemini(genai.Client(), meshai=client)

# Use as normal — all calls auto-tracked
response = gem.models.generate_content(
    model="gemini-2.0-flash",
    contents="Hello",
)
# Model, input/output tokens captured automatically

# Works with different models too:
response = gem.models.generate_content(
    model="gemini-2.5-pro",
    contents="Explain AI governance",
)

How It Works

wrap_gemini wraps the Google Gen AI client’s generate_content method. After each call, it:
  1. Extracts the model name from the response usage_metadata
  2. Extracts input and output token counts from usage_metadata
  3. Sends the usage event to MeshAI (buffered, non-blocking)
Works with both the new unified google-genai SDK and the legacy google-generativeai SDK.

Alternative: Proxy (Zero-Code)

If you prefer no SDK at all, route calls through the MeshAI proxy:
export GEMINI_BASE_URL=https://proxy.meshai.dev/v1/gemini/k/msh_YOUR_PROXY_KEY