Google Gemini
JournaledGemini — drop-in wrapper around google.generativeai.GenerativeModel.
Usage
from tokenid import JournaledGemini
model = JournaledGemini(
"gemini-1.5-pro", # model name is the first positional arg
session_id="my-session", # groups calls in your dashboard
api_key="…", # optional — falls back to env
)
response = model.generate_content("Hello")
print(response.text)
Unlike the OpenAI and Anthropic wrappers, JournaledGemini is constructed per-model:
the model name is a required positional argument, mirroring the underlying
google.generativeai.GenerativeModel constructor. api_key is optional — if omitted,
the SDK reads GOOGLE_API_KEY or GEMINI_API_KEY from the environment.
Requires the google-generativeai package — pip install google-generativeai.
Streaming
with model.stream("Hello") as stream:
for chunk in stream:
print(chunk.text, end="", flush=True)
Token counts are aggregated across the final stream chunk and journaled on completion.
Async
Use AsyncJournaledGemini for async code:
import asyncio
from tokenid import AsyncJournaledGemini
async def main():
model = AsyncJournaledGemini("gemini-1.5-pro", session_id="my-session")
resp = await model.generate_content_async("Hello")
print(resp.text)
asyncio.run(main())
Cost basis
thoughts_token_count is captured as reasoning_tok for Gemini reasoning models.
All token counts are extracted from response.usage_metadata and priced against the
TokenID pricing table.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
model_name |
string | Yes (positional) | Gemini model name, e.g. gemini-1.5-pro or gemini-2.0-flash. |
session_id |
string | No | Groups calls into a named session in your dashboard. |
api_key |
string | No | Google API key. Falls back to GOOGLE_API_KEY then GEMINI_API_KEY. |
enforcement_config |
dict | No | Local enforcement rules (see Enforcement). |