Skip to main content

tokensbill

One line. Every AI token tracked. Automatically captures Anthropic / OpenAI / Gemini token usage from your app's outgoing AI calls and streams it to your TokensBill dashboard — so you can see exactly where your AI spend goes.

Install

pip install tokensbill

Use — one line at startup

import tokensbill; tokensbill.init("tl_live_your_project_key")

Put it at the top of your entry file (e.g. main.py), before your app starts. That's it — every AI API call your app makes is now tracked automatically.

Get your project key from TokensBill → your project → Integration.

What it does

  • Patches httpx and requests (used by the Anthropic/OpenAI Python SDKs).
  • Reads token counts and cost from the AI provider's response without altering your call.
  • Sends the numbers to TokensBill in a background thread.
  • Silent-fail by design — it never raises into, blocks, or slows your application. If TokensBill is unreachable, your app is completely unaffected.

Is the key safe to commit?

The tracking key is a label, not a password. It cannot access your AI provider account, read your prompts, or spend money — it only lets your app report usage numbers to your dashboard. You can regenerate it any time from the dashboard.

Options

import tokensbill
tokensbill.init(
    "tl_live_...",
    environment="production",   # optional - auto-detected from ENVIRONMENT / APP_ENV /
                                # FLASK_ENV / DJANGO_ENV, falling back to "unknown".
                                # Set this only to override.
    ingest_base_url="https://tokensbill.aiappsjunction.com",  # override the endpoint
)

Prove a model swap before you make it

Every model-swap recommendation ends by telling you to check output quality yourself. Shadow replay does that check — and it tells you what the check will cost before it runs.

import tokensbill

plan = tokensbill.verify("ScoreDocument", "gpt-4.1-mini", samples=50)
print(plan.describe())
plan.start()

verify() runs nothing. It returns a plan:

Replay ScoreDocument against gpt-4.1-mini - 50 samples, 100 provider calls
  Estimated cost ~$0.5875 ($0.0118 per sample), based on your measured average of
  2,296 input / 650 output tokens for this function.
  Hard cap $1.00. Nothing runs until you call `.start()`.

Only .start() arms it. Two steps on purpose: replay spends your money, and the first thing you see about that should not be the invoice.

After it starts, the next samples calls to that function have their request bodies held in memory. Each is re-sent to the candidate model on your own credentials, the two answers are compared inside your process, and only a score is reported: 188 of 200 agreed (94%). It shows up on the finding in your dashboard.

The cost, precisely. Each sample is two calls — the current model and the candidate — because comparing a fresh candidate answer against a stale cached one would score the passage of time as a model difference. The estimate is priced from token counts this process has actually measured for that function. If it hasn't seen the function yet it says unknown, never $0.00. max_cost_usd is a hard cap: the run stops before the sample that would exceed it.

Want the exact number first? dry_run=True captures real request bodies, prices the run from them, and sends nothing to the provider and no verdict to TokensBill.

Your prompts and responses never leave your infrastructure. The verdict payload is counts, two model names and a comparison method — the TokensBill API has no field that could receive content. The answers that disagreed stay on your machine (plan.result.mismatches) for you to read.

Limits, stated plainly. Comparison is json (parsed, property order ignored) or exact (trimmed text). Neither can tell you whether two differently-worded paragraphs mean the same thing, so this is for structured output. Requests using tools or function calling are never replayed — re-sending one could fire a real action twice — and neither are streaming requests.

License

MIT

Download files

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

Source Distribution

tokensbill-1.0.1.tar.gz (26.5 kB view details)

Uploaded Source

Built Distribution

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

tokensbill-1.0.1-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file tokensbill-1.0.1.tar.gz.

File metadata

  • Download URL: tokensbill-1.0.1.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for tokensbill-1.0.1.tar.gz
Algorithm Hash digest
SHA256 73ff943fe96f0201b585d41d66919905218d31ccc712f9dea632a5df8b532ddb
MD5 4c068645772739bfd45f107ddeb18a74
BLAKE2b-256 d0e582b896583db3d2391cbe9d63158aaf90efeac7f9a4cedec26f399ee368f7

See more details on using hashes here.

File details

Details for the file tokensbill-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: tokensbill-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for tokensbill-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8aab0fdd410a3ad8b965e935d7fd038e2b7c4ac254a69212fe642336ceab8c80
MD5 38491408297558ae12b437fc65061ef9
BLAKE2b-256 9e9e2ffb5de283ba51214184ecd303fe05b1c086adc9af43fb6f2b70db4dab77

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 Sentry Error logging StatusPage Status page