Skip to main content

CLI tool and library for calculating Claude API token costs

Project description

tokencalc

CLI tool and Python library for calculating Claude API token costs. Supports all current Claude models, session tracking, model comparison, budget planning, and CSV export.

Install

pip install git+https://github.com/dylantirandaz/token-cost-calculator.git

Or clone and install locally:

git clone https://github.com/dylantirandaz/token-cost-calculator.git
cd token-cost-calculator
pip install .

Quick start

One-liner from the terminal:

tokencalc 50000 10000
  Claude Sonnet 4
  Input:     50.0k  tokens  ->  $0.1500
  Output:    10.0k  tokens  ->  $0.1500
  Total:     60.0k  tokens  ->  $0.3000

Specify a model:

tokencalc 50000 10000 opus-4

Compare across all models:

tokencalc --compare 100k 20k
  Cost comparison for 100.0k input + 20.0k output:

  Model                      Input Cost   Output Cost        Total
  --------------------------  ------------ ------------ ------------
  Claude 3.5 Haiku             $0.0800       $0.0800     $0.1600   <- cheapest
  Claude Sonnet 4              $0.3000       $0.3000     $0.6000   (3.8x)
  Claude 3.5 Sonnet            $0.3000       $0.3000     $0.6000   (3.8x)
  Claude Opus 4                $1.5000       $1.5000     $3.0000   (18.8x)
  Claude 3 Opus                $1.5000       $1.5000     $3.0000   (18.8x)

Interactive mode:

tokencalc

This drops you into a REPL where you can run multiple calculations, switch models, track session costs, and more.

Interactive commands

Command Description
calc <in> <out> Calculate cost for given token counts
estimate Paste text to estimate its token count
compare <in> <out> Side-by-side cost across all models
budget <amount> See how many tokens fit in a dollar budget
model <name> Switch the active model
pricing Show the pricing table
session View running session totals
export Export session to CSV
history Show past sessions
reset Reset the current session
help Show all commands

You can also just type two numbers directly (e.g. 50000 10000) and it'll calculate.

Token counts accept shorthand: 10k, 1.5M, 10,000.

Use as a library

from tokencalc import calc_cost, estimate_tokens, MODELS

# calculate cost
total, input_cost, output_cost = calc_cost(
    input_tokens=50_000,
    output_tokens=10_000,
    model="sonnet-4"
)
print(f"Total: ${total:.4f}")

# estimate tokens from text
tokens = estimate_tokens("some long prompt text here...")

# list available models
for key, info in MODELS.items():
    print(f"{info['name']}: ${info['input']}/M in, ${info['output']}/M out")

Session tracking

Each interactive session is saved to ~/.tokencalc_history.json when you exit. Use history to review past sessions and clear-history to wipe the file.

Supported models

Model Input (per 1M) Output (per 1M)
Claude Opus 4 $15.00 $75.00
Claude Sonnet 4 $3.00 $15.00
Claude 3.5 Haiku $0.80 $4.00
Claude 3 Opus $15.00 $75.00
Claude 3.5 Sonnet $3.00 $15.00
Claude 3.5 Sonnet v2 $3.00 $15.00

Pricing pulled from the Anthropic docs. Open an issue or PR if anything's out of date.

License

MIT

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

tokencalc-0.2.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

tokencalc-0.2.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tokencalc-0.2.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for tokencalc-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c926c48ca4b885436d2cacd75e3910573b3044935fd4e08ad3725a93dd9d6483
MD5 93420081e56ea613a0f6dfd4f8fbd87d
BLAKE2b-256 adcb31f2a99d025500ceebe2a0f7e90be5402ca13436a2cee73ddf277a39d799

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tokencalc-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for tokencalc-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a11073926c70219ae1665811d2372eb5b614c2f8e3672c8738a734139dbee2f0
MD5 9a766f36cedd0fc2aa2980ed530b2b2f
BLAKE2b-256 0da12cc722eb874008b8595e14abff825e22260b5c5a0e823350b2d601c300fd

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