Skip to main content

Token Usage Analytics SDK

Lightweight, non-blocking Python SDK for tracking AI usage metrics across OpenAI, Anthropic, and Google AI providers.

PyPI version Python 3.9+

PyPI: token-usage-analytics · GitHub: jinal09-codaemon/token-usage-sdk · Import: from ai_metering import ...

Installation

pip install token-usage-analytics

With provider-specific extras:

pip install token-usage-analytics[openai]      # OpenAI support
pip install token-usage-analytics[anthropic]   # Anthropic/Claude support
pip install token-usage-analytics[all]         # All providers

Quick Start

from ai_metering import MeteringClient

client = MeteringClient(
    api_key="usage_live_...",
    endpoint="https://tokenusage.transitiv-dev.com",
)

client.track_llm(
    provider="openai",
    model="gpt-4",
    input_tokens=150,
    output_tokens=50,
    feature="chat",
)

Or load from environment (see .env.example):

from ai_metering import AIUsage

usage = AIUsage.from_env()

Features

  • Non-blocking: Usage tracking never delays your AI responses
  • Multi-provider: OpenAI, Anthropic/Claude, Google Gemini
  • Auto-extraction: Automatically extracts tokens from provider responses
  • Reliable: Circuit breaker, retry queue, disk buffer for offline resilience
  • Secure: HTTPS enforced, no PII in payloads

Auto-Tracking with Wrappers

from openai import OpenAI
from ai_metering import AIUsage

usage = AIUsage.from_env()
openai = usage.wrap(OpenAI())

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

Environment Variables

Variable Required Description
USAGE_SDK_API_KEY Yes API key from Token Usage Analytics (usage_live_...)
METERING_BASE_URL Yes (prod) API base URL, e.g. https://tokenusage.transitiv-dev.com
ORGANIZATION_ID No Scope events to an organization
AI_METERING_DEBUG No Debug logging (true / false)

Aliases: AI_METERING_API_KEY, METERING_API_KEY, AI_METERING_ENDPOINT

Copy .env.example to .env for local development.

Development

git clone https://github.com/jinal09-codaemon/token-usage-sdk.git
cd token-usage-sdk
python -m pip install -e ".[dev,openai]"
pytest tests/ -v
python -m build

See PUBLISHING.md and docs/GITHUB_SETUP.md for GitHub + PyPI release steps.

License

Proprietary - Transitiv Technologies. See LICENSE for details.

Support

Download files

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

Source Distribution

token_usage_analytics-1.0.0.tar.gz (29.9 kB view details)

Uploaded Source

Built Distribution

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

token_usage_analytics-1.0.0-py3-none-any.whl (41.3 kB view details)

Uploaded Python 3

File details

Details for the file token_usage_analytics-1.0.0.tar.gz.

File metadata

  • Download URL: token_usage_analytics-1.0.0.tar.gz
  • Upload date:
  • Size: 29.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for token_usage_analytics-1.0.0.tar.gz
Algorithm Hash digest
SHA256 877357a622e303f8e3e67cfef9e0c02950b42e9658e5e6572155c37a8435acfe
MD5 af69b44ba4aaa968a3918523986112cb
BLAKE2b-256 91170449be705793c91bc61a906793d216da4329ad809a07a397c29a0c0bdfc0

See more details on using hashes here.

Provenance

The following attestation bundles were made for token_usage_analytics-1.0.0.tar.gz:

Publisher: publish.yml on jinal09-codaemon/token-usage-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file token_usage_analytics-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for token_usage_analytics-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 378bce0f93987ccf1733d0d655855fa584ce371b6fd02673da12edef61ef1319
MD5 90d9a62dfaa78785763d46bf66038d10
BLAKE2b-256 ecf8f34d1c8f58aecadcf52fbbfaceb4a231d2606be2d60b886b76aebbae91a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for token_usage_analytics-1.0.0-py3-none-any.whl:

Publisher: publish.yml on jinal09-codaemon/token-usage-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page