Calculate LLM API call costs from token usage
Project description
modelcost
Calculate LLM API call costs from token usage using price catalogs from multiple sources.
Supported pricing sources:
litellm(default)openroutertokencost
Install
python -m pip install modelcost
CLI
The default command calculates cost, so you can omit the cost subcommand.
# Default (cost)
modelcost gpt-4o 1000 500
# Explicit cost (optional)
modelcost cost gpt-4o 1000 500
# All sources in one run
modelcost --source all gpt-4o 1000 500
# JSON output
modelcost --json gpt-4o 1000 500
List available models:
modelcost models
modelcost models --source openrouter
modelcost models --filter gpt
modelcost models --json
CLI help:
modelcost --help
modelcost models --help
Library
from modelcost.calculator import calculate_cost, list_models
result = calculate_cost("gpt-4o", 1000, 500)
for source in result.available_sources:
print(f"{source.source}: ${source.total_cost_usd:.6f}")
litellm_cost = next(s for s in result.sources if s.source == "litellm")
print(litellm_cost.price_per_million_input, litellm_cost.price_per_million_output)
models = list_models("openrouter")
Output details
calculate_cost() returns a CostResult with:
model,input_tokens,output_tokenssources: list ofSourceCostobjectsavailable_sources: only sources with prices found
Each SourceCost includes:
sourcetotal_cost_usdprice_per_million_inputprice_per_million_outputerror(when not available)
Caching
openrouter responses are cached in ~/.modelcost_cache.json for 1 hour.
Notes
- Prices are fetched at runtime from the upstream catalogs.
- If a model is missing in a source, that source is marked as unavailable.
- Network sources are fetched in parallel for the
alloption.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file modelcost-0.1.0.tar.gz.
File metadata
- Download URL: modelcost-0.1.0.tar.gz
- Upload date:
- Size: 98.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
702424220628f6886ca644c1587186e14df1c039f3e125678270b47e6a3851e2
|
|
| MD5 |
9818f54c9a6f5ee65e6fdbe714fe7fa3
|
|
| BLAKE2b-256 |
5fdfeb2decb544b58032a3e6dd3c3f769039ab9272c876ebb971a41430c98e8e
|
File details
Details for the file modelcost-0.1.0-py3-none-any.whl.
File metadata
- Download URL: modelcost-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df1f2d5755ba561f41bcfc69621bed16998310aab13df7bd2da668456603d29f
|
|
| MD5 |
bba5dd6a1b6622c7a492c9e40dc2fc98
|
|
| BLAKE2b-256 |
251c8deb64065cc6ac90b01d440604c162a8ba4792919e1d6465e2797030e9f2
|