Skip to main content

Realtime OpenRouter LLM API call cost estimator

Project description

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_KEY or pass --api-key.
  • Default model-catalog cache TTL is 1 hour (cache_ttl_seconds=3600).
  • Text-based token counting uses an estimator (chars_per_token, default 4.0).

Development

uv sync
uv run python -m mamood_llm_cost_estimator --help
uv build

Created by Amirhossein Mahmoudi (@ammahmoudi)

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

mamood_llm_cost_estimator-0.1.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

mamood_llm_cost_estimator-0.1.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file mamood_llm_cost_estimator-0.1.1.tar.gz.

File metadata

File hashes

Hashes for mamood_llm_cost_estimator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a42a28ff673b213bfcef6fa21b2f1184c253e8c01037bed138ad97ce6dc7d324
MD5 88baebf022b12f42c7f382e7e040e31c
BLAKE2b-256 1fc6574b036432f1d43f8d3bb4b2e71b85506a39c5f402f55ca4500a6fd34324

See more details on using hashes here.

File details

Details for the file mamood_llm_cost_estimator-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mamood_llm_cost_estimator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 58ab2b108e96cc8195fc57e6dab5bc73c5e9b2600c2a8fe84e24e2bc685d778f
MD5 9f902f1eac29dd3f9775d2ce82165288
BLAKE2b-256 6ff1ccae822766df22336161327fd418928331952ad1ddd735069cd22fbe8a02

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