Skip to main content

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

Project description

tokvera

Python SDK for Tokvera AI cost and trace telemetry.

Current version: 0.2.7

What It Tracks

  • OpenAI
  • Anthropic
  • Gemini

Tracked calls emit normalized telemetry to Tokvera ingest (/v1/events) with:

  • latency, status, model, token usage
  • trace context (trace_id, run_id, span_id, parent_span_id, conversation_id)
  • evaluation signals (outcome, retry_reason, fallback_reason, quality_label, feedback_score)
  • optional v2 trace fields (span/tool metadata, payload refs/blocks, per-step metrics, routing decisions)

Install

pip install tokvera

For local development:

pip install -e .[dev]

Quick Start

from openai import OpenAI
from tokvera import track_openai

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

tracked = track_openai(
    openai_client,
    api_key="tkv_project_key",
    feature="support_bot",
    tenant_id="acme",
    environment="production",
    step_name="draft_reply",
    emit_lifecycle_events=True,
)

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

Enable emit_lifecycle_events=True when you want /dashboard/traces/live to show a run immediately at call start and keep it marked as processing until the terminal event lands.

Set ingest URL:

export TOKVERA_INGEST_URL="https://api.tokvera.org/v1/events"

Integration Helpers

  • FastAPI middleware:
    • create_fastapi_tracking_middleware(...)
    • get_fastapi_track_kwargs(...)
  • Django middleware:
    • create_django_tracking_middleware(...)
    • get_django_track_kwargs(...)
  • Background jobs:
    • create_background_job_context(...)
    • get_background_track_kwargs(...)
  • Celery:
    • create_celery_task_context(...)
    • get_celery_track_kwargs(...)
  • LangChain:
    • create_langchain_callback_handler(...)
  • LlamaIndex:
    • create_llamaindex_callback_handler(...)

Trace Schema Support

  • v1 schema version: 2026-02-16
  • v2 schema version: 2026-04-01

Contract references:

  • tokvera-api/docs/event-envelope-v1.contract.json
  • tokvera-api/docs/event-envelope-v2.contract.json
  • tokvera-api/docs/SCHEMA_COMPATIBILITY_POLICY.md

Privacy Behavior

  • Tracking is fire-and-forget and non-blocking
  • Prompt/output content is not required
  • If v2 content capture is enabled, payload hashes/blocks are included based on options

Examples

  • examples/fastapi_middleware.py
  • examples/django_middleware.py
  • examples/background_jobs.py
  • examples/celery_task.py

Realtime Tracing

  • /dashboard/traces is the main engineering workspace for execution, payload, and optimization debugging.
  • /dashboard/traces/live is the realtime feed for active and recently completed runs.
  • Lifecycle start events are additive. They do not replace the normal terminal success/failure event.

Test

pytest

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.7.tar.gz (29.5 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.7-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tokvera-0.2.7.tar.gz
  • Upload date:
  • Size: 29.5 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.7.tar.gz
Algorithm Hash digest
SHA256 1aef3e79a45d11b70c032cf2a46a02c835e309dc2689ddd02ba627ae67765093
MD5 97842a48ee252ea8980c9635604c6289
BLAKE2b-256 7c0adc08eaff34319fb6b50ab7c554c187c330de1e16fc4743c5b2de9d535bad

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tokvera-0.2.7-py3-none-any.whl
  • Upload date:
  • Size: 26.0 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.7-py3-none-any.whl
Algorithm Hash digest
SHA256 11951d2e2bbf5eecf8c1e3b9b49d3eabacf38d549cb2769132330933704a700a
MD5 b887d5658e54dc18c4fd0d1ddef489f3
BLAKE2b-256 cfbab55bb7f78c9897d5e26ed73d91aff81dd780b5378f6c381ee562b89ff963

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