Skip to main content

Out-of-band AI API usage metering SDK

Project description

Tokentify UsageMeter (Python)

Out-of-band AI API usage metering for Python. After setup, use httpx for provider calls; the SDK captures usage automatically.

Install

pip install tokentifyai-usagemeter

Optional virtual environment:

python3 -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install tokentifyai-usagemeter

Setup

1. Environment (API key is global)

Copy .env.example to .env. The API key lives only in .env (or your deployment environment)—never in init() code.

cp .env.example .env
# .env — required
USAGEMETER_API_KEY=your_ingest_api_key
USAGEMETER_BUCKET=your_bucket_name

2. Initialize

import os
import tokentifyai_usagemeter as um

# USAGEMETER_API_KEY from .env; USAGEMETER_BUCKET from argument
um.load_env()
um.setup(os.environ["USAGEMETER_BUCKET"])

# USAGEMETER_API_KEY and USAGEMETER_BUCKET both from .env
um.setup()

# Production (skip health checks after first successful setup)
um.load_env()
um.init(
    os.environ["USAGEMETER_BUCKET"],
    verify_connection=False,
    verify_api_key=False,
)
Function When to use
setup() / setup(USAGEMETER_BUCKET value) First run; verifies collector + USAGEMETER_API_KEY from .env
init() / init(USAGEMETER_BUCKET value) Same; control verify_connection / verify_api_key

Init parameters (API key is not accepted):

Parameter Source
bucket Argument (value of USAGEMETER_BUCKET) or USAGEMETER_BUCKET in .env
app_name Argument or USAGEMETER_APP_NAME in .env
environment Argument or USAGEMETER_ENVIRONMENT in .env (default production)
load_env_file Default True — loads .env before reading env vars
verify_connection Default False (True in setup)
verify_api_key Default True
# Not supported — will raise TypeError:
# um.init(os.environ["USAGEMETER_BUCKET"], api_key="secret")

Call init() / setup() before creating httpx clients.

3. Launch (v0.1.2)

.env (USAGEMETER_API_KEY + USAGEMETER_BUCKET):

USAGEMETER_API_KEY=your_ingest_api_key
USAGEMETER_BUCKET=your_bucket_name

app.py:

import httpx
import tokentifyai_usagemeter as um

um.setup()  # loads .env; reads USAGEMETER_API_KEY and USAGEMETER_BUCKET

client = httpx.Client()
response = client.post(
    "https://api.openai.com/v1/chat/completions",
    json={"model": "gpt-4o", "messages": [{"role": "user", "content": "hi"}]},
    headers={"Authorization": "Bearer YOUR_OPENAI_KEY"},
)
print(response.status_code)

um.flush()
python app.py

4. Develop from this repo

cd tokentify-sdk-python
python -m venv .venv && source .venv/bin/activate
pip install -e .
cp .env.example .env   # set USAGEMETER_API_KEY and USAGEMETER_BUCKET
python -c "import tokentifyai_usagemeter as um; um.setup(); print(um.is_initialized())"

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

tokentifyai_usagemeter-0.1.2.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

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

tokentifyai_usagemeter-0.1.2-py3-none-any.whl (15.4 kB view details)

Uploaded Python 3

File details

Details for the file tokentifyai_usagemeter-0.1.2.tar.gz.

File metadata

  • Download URL: tokentifyai_usagemeter-0.1.2.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for tokentifyai_usagemeter-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ef71cd91f8a0b02cdd462ba66ce5746afdfdd369c3c192f5a99955372bbe59e3
MD5 564c5c28a6a7f15868d4eca442ca8af7
BLAKE2b-256 e1d10d4f1a581f92632125e18123bc87bf296002680a40f37bfa4c61a5ccbaa0

See more details on using hashes here.

File details

Details for the file tokentifyai_usagemeter-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for tokentifyai_usagemeter-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c1ac7db0430dac433a0aa8f191f784e8e434ad135ca402889370ac6847d9af24
MD5 4ab89847e85f738e7bfa928e5c6cba62
BLAKE2b-256 2d5ec6fda82dc2986f78067ce1cd0a21116a2452eedf5cafb0feee290daa2fcb

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