Skip to content

ATOM00blue/tokenmeter

Repository files navigation

tokenmeter

Estimate LLM token counts and dollar cost for any text, file, or whole directory — across OpenAI, Anthropic, and Google.

CI PyPI Python License: MIT


tokenmeter answers the questions you ask a dozen times a day while building with LLMs:

How many tokens is this prompt? This file? This whole repo? And what will it cost me on GPT-5.4 vs Claude vs Gemini?

It works offline-first — no API keys, no network calls required. OpenAI counts are exact (via tiktoken); Anthropic and Google counts are well-calibrated estimates, with optional exact counts via the provider APIs when you have a key. It's pipe-friendly, ships a polished CLI and a clean importable library, and maintains an up-to-date pricing table for the models you actually use in 2026.

$ tokenmeter "Summarize the quarterly report in three bullet points." -m gpt-4o -m claude-opus-4-7 -m gemini-3-pro
tokenmeter  input: 53 chars
┌──────────────────────────┬───────────┬────────┬───────────┐
│ Model                    │ Provider  │ Tokens │   Input $ │
├──────────────────────────┼───────────┼────────┼───────────┤
│ gpt-4o                   │ openai    │     11 │ $0.000028 │
│ claude-opus-4-7          │ anthropic │    ~13 │ $0.000065 │
│ gemini-3-pro (cheapest)  │ google    │    ~10 │ $0.000020 │
└──────────────────────────┴───────────┴────────┴───────────┘
~ = offline estimate (Anthropic/Google). Use --exact with an API key for official counts.

Why tokenmeter?

tokenmeter tokencost ttok
First-class CLI
File / glob / directory inputs
Reads stdin (pipe-friendly)
Token counts OpenAI only
Dollar cost estimates
OpenAI + Anthropic + Google
Works offline / no API key partial
Pretty tables + --json
--compare across models
Importable library API

Install

# pipx (recommended for a CLI)
pipx install tokenmeter

# uv
uv tool install tokenmeter

# pip
pip install tokenmeter

For optional exact counts via provider APIs:

pip install "tokenmeter[exact]"   # adds anthropic + google-genai

Usage

Count a string

tokenmeter "The quick brown fox jumps over the lazy dog."

Count a file or an entire directory

tokenmeter ./prompt.txt
tokenmeter ./src                       # sums tokens across the whole tree
tokenmeter "src/**/*.py"               # glob (quote it so the shell doesn't expand)
tokenmeter ./docs --include "*.md"     # only markdown
tokenmeter ./repo --exclude "*.lock"   # skip noise

Directory mode prints a per-file breakdown plus a grand-total cost table. Binary files (detected by extension and by content), node_modules, .git, build artifacts, and oversized files (>50 MB) are skipped automatically. Symlinks are never followed, so a scanned tree can't pull in content from outside it.

Pipe from stdin

cat essay.md | tokenmeter -m gpt-4o
git diff | tokenmeter -m claude-opus-4-7
echo "hello" | tokenmeter -q          # -> just the number, great for scripts

Compare many models at once

tokenmeter "your prompt here" --compare
tokenmeter  input: 49 chars
┌────────────────────────┬───────────┬────────┬───────────┐
│ Model                  │ Provider  │ Tokens │   Input $ │
├────────────────────────┼───────────┼────────┼───────────┤
│ gpt-5.4                │ openai    │     10 │ $0.000025 │
│ gpt-4o                 │ openai    │     10 │ $0.000025 │
│ gpt-4o-mini (cheapest) │ openai    │     10 │ $0.000002 │
│ o3                     │ openai    │     10 │ $0.000020 │
│ claude-opus-4-7        │ anthropic │    ~12 │ $0.000060 │
│ claude-sonnet-4-6      │ anthropic │    ~12 │ $0.000036 │
│ claude-haiku-4-5       │ anthropic │    ~12 │ $0.000012 │
│ gemini-3-pro           │ google    │    ~10 │ $0.000020 │
│ gemini-2.5-flash       │ google    │    ~10 │ $0.000003 │
└────────────────────────┴───────────┴────────┴───────────┘

Project output cost too

tokenmeter ./prompt.txt -m gpt-4o --output-tokens 800

Adds projected completion cost and a total — handy for budgeting a request before you send it.

JSON output (for tooling / CI)

tokenmeter "hello world" -m gpt-4o --json
{
  "input_chars": 11,
  "output_tokens": 0,
  "results": [
    {
      "model": "gpt-4o",
      "provider": "openai",
      "input_tokens": 2,
      "output_tokens": 0,
      "input_cost": 5e-06,
      "output_cost": 0.0,
      "total_cost": 5e-06,
      "is_estimate": false,
      "method": "tiktoken"
    }
  ]
}

List supported models & prices

tokenmeter models                 # all
tokenmeter models -p anthropic    # one provider
tokenmeter models --json          # machine-readable

Exact counts via provider APIs (optional)

Anthropic and Google counts are offline estimates by default. For official counts:

export ANTHROPIC_API_KEY=sk-ant-...
export GEMINI_API_KEY=...
tokenmeter "your text" -m claude-opus-4-7 -m gemini-3-pro --exact

If a key is missing or the call fails, tokenmeter silently falls back to the offline estimate.

Library API

from tokenmeter import count, cost, estimate, list_models, get_model

# Just the token count (int)
count("hello world", model="gpt-4o")                 # -> 2

# Cost breakdown, optionally projecting an output of N tokens
cb = cost("hello world", model="gpt-4o", output_tokens=50)
cb.input_tokens     # 2
cb.total_cost       # USD float

# Full estimate (tokens + cost + metadata)
est = estimate("bonjour le monde", model="claude-opus-4-7")
est.input_tokens, est.is_estimate, est.method        # (~4, True, "estimate")
est.as_dict()                                         # JSON-ready dict

# Registry
get_model("gpt-4o").input_per_1m                      # 2.5
[m.name for m in list_models(provider="google")]      # ['gemini-1.5-flash', ...]

Scan a directory programmatically:

from tokenmeter import scan
result = scan("./src", model="gpt-4o", include=["*.py"])
result.total_tokens, result.file_count
for f in result.files:
    print(f.path, f.tokens)

Supported models & pricing

tokenmeter ships a maintained pricing table (USD per 1M tokens) covering the current OpenAI, Anthropic, and Google model families, including the GPT-5 / 5.4 / 5.5, Claude Opus / Sonnet / Haiku 4.x and 3.x, and Gemini 3 / 2.5 / 1.5 lineups. Friendly aliases (opus, sonnet, haiku, gemini, gpt4o, …) resolve to the right model. Run tokenmeter models for the full, live list with prices and context windows.

⚠️ Prices change often. The table is reviewed periodically (see PRICING_LAST_UPDATED) but should be treated as an estimate. Verify against the provider for billing-critical use. Anthropic/Google offline counts are estimates; pass --exact (with an API key) for official counts.

How counting works

  • OpenAI — exact, offline, via tiktoken (o200k_base for GPT-4o/4.1/5.x/o-series, cl100k_base for GPT-4/3.5).
  • Anthropic / Google — offline estimate: an exact tiktoken baseline scaled by a per-provider calibration factor. Marked with ~ in tables and is_estimate: true in JSON. Pass --exact with the relevant API key to use the provider's official endpoint.

FAQ

Do I need API keys? No. Everything works offline. Keys are only needed for --exact.

Are the cost numbers exact? Token counts for OpenAI are exact. Costs depend on the pricing table, which can lag provider changes — treat them as close estimates.

Why are Claude/Gemini counts approximate? Their tokenizers aren't fully open-source for offline use, so tokenmeter calibrates against a tiktoken baseline. Use --exact for official counts.

Does it send my text anywhere? Never, unless you opt in with --exact (which calls the provider's token-counting API). Default mode is 100% local.

How do I script the token count only? tokenmeter "text" -q prints just the integer.

Contributing

Contributions welcome — especially pricing updates. See CONTRIBUTING.md.

License

MIT © 2026 ATOM00blue

About

Estimate LLM token counts and $ cost for any text, file, or directory across OpenAI, Anthropic, and Google models.

Topics

Resources

License

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages