Skip to main content

Count tokens and estimate cost for prompts before you send them.

Project description

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.

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

tokenmeter_cli-0.2.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

tokenmeter_cli-0.2.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file tokenmeter_cli-0.2.0.tar.gz.

File metadata

  • Download URL: tokenmeter_cli-0.2.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for tokenmeter_cli-0.2.0.tar.gz
Algorithm Hash digest
SHA256 dfb48f5fd80f8f1827785eceb91074a5dbf66143ab9b64ccfb0854aac769718d
MD5 4e0234771f9f4859dc6e7db7e3c36212
BLAKE2b-256 fcddc31f2c59e878de034ebf272fc924db4d6f44c4d449d9abe795e1258949fa

See more details on using hashes here.

File details

Details for the file tokenmeter_cli-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tokenmeter_cli-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for tokenmeter_cli-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43b6e06ff9a4a94c9fd3d8e0796a6cef5d4f52dbf3ebf8b8edaee4ab961ba0ec
MD5 9521f1a75ddb2f52ff172b4817b222ff
BLAKE2b-256 60600d091bd7b3ce69e95517928b683e0b7ad9cc960e0012e01b92403f129e6a

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