Skip to main content

Lightweight multi-provider (OpenAI, Anthropic, Gemini) analytics tracking wrapper

Project description

tokvera

tokvera is a lightweight Python SDK that wraps OpenAI, Anthropic, and Gemini clients and emits usage analytics in a fire-and-forget way.

Installation

pip install tokvera

For development:

pip install -e .[dev]

Environment Variable Setup

Set your ingestion endpoint:

# Linux/macOS
export TOKVERA_INGEST_URL="https://api.tokvera.com/v1/events"

# Windows PowerShell
$env:TOKVERA_INGEST_URL = "https://api.tokvera.com/v1/events"

If TOKVERA_INGEST_URL is not set, analytics are skipped automatically.

Quick Start

OpenAI

from openai import OpenAI
from tokvera import track_openai

openai_client = OpenAI(api_key="sk-...")

client = track_openai(
    openai_client,
    api_key="tokvera_project_key",
    feature="support_bot",
    tenant_id="acme",
    plan="pro",
    environment="production",
    template_id="support_v3",
    capture_content=False,
)

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello"}],
)

Anthropic

from anthropic import Anthropic
from tokvera import track_anthropic

anthropic_client = Anthropic(api_key="sk-ant-...")

client = track_anthropic(
    anthropic_client,
    api_key="tokvera_project_key",
    feature="support_bot",
    tenant_id="acme",
    environment="production",
)

client.messages.create(
    model="claude-3-5-sonnet-latest",
    max_tokens=256,
    messages=[{"role": "user", "content": "Hello"}],
)

Gemini

from google import genai
from tokvera import track_gemini

gemini_client = genai.Client(api_key="AIza...")

client = track_gemini(
    gemini_client,
    api_key="tokvera_project_key",
    feature="assistant",
    tenant_id="acme",
    environment="production",
)

client.models.generate_content(
    model="gemini-2.0-flash",
    contents="Hello",
)

Event Schema

Canonical specification: tokvera-api/docs/EVENT_SCHEMA.md

Events include:

  • schema_version: 2026-02-16
  • event_type: openai.request, anthropic.request, or gemini.request
  • provider: openai, anthropic, or gemini
  • endpoint: chat.completions.create, responses.create, messages.create, models.generate_content
  • status: success or failure
  • latency_ms
  • model
  • usage: prompt_tokens, completion_tokens, total_tokens
  • tags: feature, tenant_id, customer_id, plan, environment, template_id
  • error on failure events

Privacy

By default, prompt/response content is not sent.

If capture_content=True, content is hashed (SHA-256) before ingestion. Raw content is never sent by this SDK.

Disable Tracking

You can disable tracking by either:

  1. Using the original OpenAI client directly (do not wrap it), or
  2. Unsetting TOKVERA_INGEST_URL so no events are emitted.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tokvera-0.2.0.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tokvera-0.2.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file tokvera-0.2.0.tar.gz.

File metadata

  • Download URL: tokvera-0.2.0.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for tokvera-0.2.0.tar.gz
Algorithm Hash digest
SHA256 52b6231b5a30b23a7f11a6cfb536462bba167fe959b5d092eefd7be4af53aa3c
MD5 593686d55bcf7252e7a1e5229239ff3f
BLAKE2b-256 9f600ec4f3cf7e0c5187272e9e478c809bd0f77c92a28043952efe9cc1c679da

See more details on using hashes here.

File details

Details for the file tokvera-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tokvera-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for tokvera-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d42ef078addd3ccfa48225b8cd68d07cf4a6e0c41f459c49f52f5b444a8adbb5
MD5 b4440b0e6c8ce697ca75d3338a8e5d50
BLAKE2b-256 035b04c7eb80424d4b09d8c69e95914e31aaa1681a4bae0206accbf4f95fbed5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page