Skip to main content

tokenwright

A token-cost ledger and a budget cap that refuses BEFORE you spend.

Most cost tools tell you what a run cost after it ran. tokenwright does the two things those tools don't:

  1. Fail closed on an unknown price. If a model has no confirmed price, its cost is not silently treated as zero — CostUnknown is raised. You never under-count spend because a price was missing.
  2. A projective cap. Before the next call, estimate its cost and refuse it when spent + next > cap (BudgetExceeded). The overrun is prevented, not reported.
from tokenwright import prices_from_mapping, ledger_cost_usd, guard_next_call

prices = prices_from_mapping({
    "gemini-2.5-flash": {"usd_per_1m_input": 0.30, "usd_per_1m_output": 2.50},
})

spent = ledger_cost_usd("runs/42/token_usage.jsonl", prices)   # USD so far

guard_next_call(spent, projected_in=1200, projected_out=800,
                model="gemini-2.5-flash", prices=prices, cap_usd=0.05)
# returns the projected USD, or raises:
#   BudgetExceeded  if spent + projected would cross the cap
#   CostUnknown     if the model has no confirmed price (fail-closed)

Or from the shell:

$ tokenwright cost runs/42/token_usage.jsonl --prices prices.json
0.013400
$ tokenwright check --spent 0.04 --in 1200 --out 800 --model gemini-2.5-flash \
      --prices prices.json --cap 0.05   # exit 1 if it would exceed

The model

A ledger row is {"model", "tokens_in", "tokens_out"?, "tokens_total"?}. When tokens_total is present, output is tokens_total - tokens_in — so reasoning tokens (billed as output but not reported in tokens_out) are counted. Rows with no tokens cost 0. Prices are a plain mapping; load them from JSON, YAML or a database — tokenwright stays zero-dependency.

tokenwright
Unknown price CostUnknown (never silently 0)
Reasoning tokens counted via tokens_total - tokens_in
Cap projective — refuses the next call before it runs
Dependencies none (pure stdlib)

API

  • prices_from_mapping(data) -> Prices — keep only models with both prices confirmed
  • estimate_cost_usd(tokens_in, tokens_out, model, prices) -> float
  • row_cost_usd(row, prices) -> float · ledger_cost_usd(path_or_rows, prices) -> float
  • would_exceed(spent, next, cap) -> bool
  • guard_next_call(spent, *, projected_in, projected_out, model, prices, cap_usd) -> float
  • CostUnknown, BudgetExceeded

How it compares

See POSITIONING.md for the evidence-based comparison with tokencost, LiteLLM cost tracking, Langfuse and Helicone — and why "measure after" is not the same as "refuse before".

Install

pip install tokenwright

MIT licensed. Part of the network-agents-setup ecosystem; extracted from its governed runner so any project can cap token spend by evidence.

Metadata

Release files for tokenwright 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tokenwright 0.1.0
File Size Uploaded
tokenwright-0.1.0.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tokenwright 0.1.0
File Interpreter ABI Platform
tokenwright-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.1 kB

Release files / tokenwright-0.1.0.tar.gz

Download URL tokenwright-0.1.0.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b72d39128b901312a7c46752a1533bc76b9b23ed3d853d5dec67bceb29f7ba6d
BLAKE2b-256 checksum
How to use checksums
adc2b19cd546cc36047f6b0bfd3e3301897751f85aef27e983ed08c274270448
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.

Transparency log

Release files / tokenwright-0.1.0-py3-none-any.whl

Download URL tokenwright-0.1.0-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c74439e5bc99638a71429d03657dda16792e5a85a36542a02e6e6aba826c3c78
BLAKE2b-256 checksum
How to use checksums
ebe608b523f04225df902eda729d37cde97b92a429a191076066a40b2d067fb6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 12, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release 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