Skip to content

rmescandon/modelcost

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

modelcost

Calculate LLM API call costs from token usage using price catalogs from multiple sources.

Supported pricing sources:

  • litellm (default)
  • openrouter
  • tokencost

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_tokens
  • sources: list of SourceCost objects
  • available_sources: only sources with prices found

Each SourceCost includes:

  • source
  • total_cost_usd
  • price_per_million_input
  • price_per_million_output
  • error (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 all option.

About

Calculates the cost of an LLM single use based on the model and used tokens

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages