mamood-llm-cost-estimator
Realtime cost estimator for OpenRouter model calls.
It fetches live pricing from OpenRouter's public models endpoint and calculates input, cached-input, output, and total USD cost for any token usage.
Why this package
- Live pricing: no hardcoded model price tables.
- Accurate token math: supports normal + cached prompt tokens.
- Easy integration: clean Python API and simple CLI.
- Fast repeated use: in-memory cache for model catalog data.
- Flexible input: pass token counts directly or pass raw text and auto-estimate tokens.
Installation
uv add mamood-llm-cost-estimator
From source:
uv sync
Python quick start
from mamood_llm_cost_estimator import OpenRouterClient, TokenUsage
client = OpenRouterClient()
usage = TokenUsage(input_tokens=1500, output_tokens=700, cached_input_tokens=300)
cost = client.estimate_model_cost(model="openai/gpt-4o-mini", usage=usage)
print(cost.as_dict())
Example output:
{
"model": "openai/gpt-4o-mini",
"input_cost_usd": 0.00018,
"cached_input_cost_usd": 0.000045,
"output_cost_usd": 0.00042,
"total_cost_usd": 0.000645,
"currency": "USD"
}
Browse available models
from mamood_llm_cost_estimator import OpenRouterClient
client = OpenRouterClient()
models = client.list_models()
for model in models[:5]:
print(
model.id,
model.provider,
model.context_window,
model.input_price_per_million_usd,
model.output_price_per_million_usd,
)
CLI usage
mamood-cost \
--model openai/gpt-4o-mini \
--input-tokens 1500 \
--output-tokens 700 \
--cached-input-tokens 300
Use text instead of token counts:
mamood-cost \
--model openai/gpt-4o-mini \
--input-text "Write a short summary of this transcript" \
--output-text "Here is your concise summary..." \
--chars-per-token 4
Mix mode (text for input, numeric for output):
mamood-cost \
--model openai/gpt-4o-mini \
--input-text "Tell me 5 ideas for a startup" \
--output-tokens 450
Notes
- Pricing source:
https://openrouter.ai/api/v1/models. - API key is optional for pricing fetch; if needed, set
OPENROUTER_API_KEYor pass--api-key. - Default model-catalog cache TTL is 1 hour (
cache_ttl_seconds=3600). - Text-based token counting supports two backends:
tiktoken(default): model-aware where possible; falls back to a standard encoding.heuristic: fast approximation usingchars_per_token(default4.0).
Use explicit tokenizer selection:
mamood-cost \
--model openai/gpt-4o-mini \
--input-text "hello world" \
--output-text "done" \
--tokenizer tiktoken
Development
uv sync
uv run python -m mamood_llm_cost_estimator --help
uv build
Created by Amirhossein Mahmoudi (@ammahmoudi)
Metadata
Release files for mamood-llm-cost-estimator 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mamood_llm_cost_estimator-0.1.2.tar.gz | 5.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mamood_llm_cost_estimator-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.0 kB
Release files / mamood_llm_cost_estimator-0.1.2.tar.gz
| Download URL | mamood_llm_cost_estimator-0.1.2.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
333b35faf91dd247487bc9ae6bde9c2e0e5f584d76eaf567d15959b11a247ecd
|
|
BLAKE2b-256 checksum How to use checksums |
8252776b22144adebf3b2447b41deef1b8092f4a2fd74bec5c08e4dd291cac3f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 Feb 26, 2026.
Transparency logRelease files / mamood_llm_cost_estimator-0.1.2-py3-none-any.whl
| Download URL | mamood_llm_cost_estimator-0.1.2-py3-none-any.whl |
|---|---|
| Size | 9.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0fbbd09c1f2733e84ba86cc648254d2e1c2afbb0c4ea684ad89f71055c9e92aa
|
|
BLAKE2b-256 checksum How to use checksums |
b2527ffbdd72581739d684148de32bec2df40072fcc673534feb1dd309dd6d67
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 Feb 26, 2026.
Transparency log