Calculate LLM API call costs from token usage using price catalogs from multiple sources.
Supported pricing sources:
litellm(default)openroutertokencost
python -m pip install modelcostThe 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 500List available models:
modelcost models
modelcost models --source openrouter
modelcost models --filter gpt
modelcost models --jsonCLI help:
modelcost --help
modelcost models --helpfrom 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")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)
openrouter responses are cached in ~/.modelcost_cache.json for 1 hour.
- 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.