Skip to main content

tokenmeter

CI PyPI Python License: MIT

Count tokens and estimate cost for prompts before you send them, from the command line or as a CI budget gate.

Prompt templates grow, a few-shot example gets added, a retrieved context balloons, and suddenly every call costs more than you thought. tokenmeter gives you the exact token count and a dollar estimate up front, for a single prompt or a whole directory of templates.

$ tokenmeter count prompts/system.txt --model gpt-4o
input            in tok   out tok   cost (USD)
prompts/system.txt  812         0   $0.002030

$ tokenmeter count prompts/ --model gpt-4o-mini --json

Install

$ pip install tokenmeter-cli                 # from PyPI, once released
$ pip install git+https://github.com/jmweb-org/tokenmeter   # latest, available now

Token counting is exact for the supported OpenAI encodings via tiktoken.

Usage

$ tokenmeter count system.txt -m gpt-4o          # one file
$ tokenmeter count prompts/ -m gpt-4o-mini       # every text file in a directory
$ cat prompt.txt | tokenmeter count - -m gpt-4o  # standard input
$ tokenmeter count p.txt --output-tokens 500     # include an assumed completion
$ tokenmeter models                              # list models and prices

As a budget gate

Fail a build when a prompt set would cost more than you allow:

$ tokenmeter budget prompts/ --model gpt-4o --max-cost 0.05
- run: tokenmeter budget prompts/ --model gpt-4o --max-cost 0.05

Cost model

Counts are real tokens. Cost multiplies tokens by a per-model rate from a small, dated price table (tokenmeter models prints it with its "as of" date). By default only input tokens are counted; pass --output-tokens N to add an assumed completion length to the estimate. Prices change, so treat the dollar figures as estimates and update the table when they move.

Exit codes

Code Meaning
0 Counted; under budget (or count was used)
1 budget estimate exceeded --max-cost
2 An input was missing, or the model is unknown

License

MIT. See LICENSE.

Metadata

Release files for tokenmeter-cli 0.2.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 tokenmeter-cli 0.2.0
File Size Uploaded
tokenmeter_cli-0.2.0.tar.gz 9.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tokenmeter-cli 0.2.0
File Interpreter ABI Platform
tokenmeter_cli-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.8 kB

Release files / tokenmeter_cli-0.2.0.tar.gz

Download URL tokenmeter_cli-0.2.0.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
dfb48f5fd80f8f1827785eceb91074a5dbf66143ab9b64ccfb0854aac769718d
BLAKE2b-256 checksum
How to use checksums
fcddc31f2c59e878de034ebf272fc924db4d6f44c4d449d9abe795e1258949fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.20 {"installer":{"name":"uv","version":"0.11.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / tokenmeter_cli-0.2.0-py3-none-any.whl

Download URL tokenmeter_cli-0.2.0-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
43b6e06ff9a4a94c9fd3d8e0796a6cef5d4f52dbf3ebf8b8edaee4ab961ba0ec
BLAKE2b-256 checksum
How to use checksums
60600d091bd7b3ce69e95517928b683e0b7ad9cc960e0012e01b92403f129e6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.20 {"installer":{"name":"uv","version":"0.11.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.2.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