Skip to main content

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)
  • 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.

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

modelcost-0.1.0.tar.gz (98.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modelcost-0.1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

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

Hashes for modelcost-0.1.0.tar.gz
Algorithm Hash digest
SHA256 702424220628f6886ca644c1587186e14df1c039f3e125678270b47e6a3851e2
MD5 9818f54c9a6f5ee65e6fdbe714fe7fa3
BLAKE2b-256 5fdfeb2decb544b58032a3e6dd3c3f769039ab9272c876ebb971a41430c98e8e

See more details on using hashes here.

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

Hashes for modelcost-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df1f2d5755ba561f41bcfc69621bed16998310aab13df7bd2da668456603d29f
MD5 bba5dd6a1b6622c7a492c9e40dc2fc98
BLAKE2b-256 251c8deb64065cc6ac90b01d440604c162a8ba4792919e1d6465e2797030e9f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page