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 supports two backends:
    • tiktoken (default): model-aware where possible; falls back to a standard encoding.
    • heuristic: fast approximation using chars_per_token (default 4.0).

Use explicit tokenizer selection:

mamood-cost \
	--model openai/gpt-4o-mini \
	--input-text "hello world" \
	--output-text "done" \
	--tokenizer tiktoken

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.2.tar.gz (5.9 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.2-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for mamood_llm_cost_estimator-0.1.2.tar.gz
Algorithm Hash digest
SHA256 333b35faf91dd247487bc9ae6bde9c2e0e5f584d76eaf567d15959b11a247ecd
MD5 56cccf125586bd2817f3b1e4ccbf28a6
BLAKE2b-256 8252776b22144adebf3b2447b41deef1b8092f4a2fd74bec5c08e4dd291cac3f

See more details on using hashes here.

Provenance

The following attestation bundles were made for mamood_llm_cost_estimator-0.1.2.tar.gz:

Publisher: workflow.yml on ammahmoudi/mamood-llm-cost-estimator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for mamood_llm_cost_estimator-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0fbbd09c1f2733e84ba86cc648254d2e1c2afbb0c4ea684ad89f71055c9e92aa
MD5 e540e264f4070f0f69b16c790afe1e20
BLAKE2b-256 b2527ffbdd72581739d684148de32bec2df40072fcc673534feb1dd309dd6d67

See more details on using hashes here.

Provenance

The following attestation bundles were made for mamood_llm_cost_estimator-0.1.2-py3-none-any.whl:

Publisher: workflow.yml on ammahmoudi/mamood-llm-cost-estimator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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