Skip to main content

API pricing math for 1k+ AI models

Project description

tokenpricing (Python SDK)

PyPI version

API pricing math for 1k+ AI models from the canonical tokenpricing database with multi-currency and cache-token pricing support.

Why tokenpricing?

Token pricing for AI models changes frequently across different providers and model types. This library now consumes the canonical database published from this repository, synchronized directly from upstream pricing sources every six hours.

Important: This library does not estimate token counts from strings or messages. Any estimation would be too approximate for anything beyond plain text, and the tokencost package already handles that use case well. tokenpricing focuses solely on providing accurate, current pricing data.

Features

  • Up-to-date AI model pricing from the tokenpricing canonical database
  • Async caching via async-lru with a 6-hour TTL for pricing data
  • Multi-currency conversion via JSDelivr currency API with a 24-hour cached USD rates map
  • Clean, typed data models (Pydantic)

Installation

uv add tokenpricing

Or with pip:

pip install tokenpricing

Usage

The public API exposes both async and sync functions:

Async API:

  • get_pricing(model_id, currency="USD")
  • compute_cost(model_id, input_tokens, output_tokens, currency="USD", cache_read_tokens=0, cache_creation_tokens=0)

Sync API:

  • get_pricing_sync(model_id, currency="USD")
  • compute_cost_sync(model_id, input_tokens, output_tokens, currency="USD", cache_read_tokens=0, cache_creation_tokens=0)

Async Example

import asyncio

from tokenpricing import get_pricing, compute_cost


async def main():
    model_id = "openai/gpt-5.2"

    # Get pricing (cached transparently for ~6 hours)
    pricing = await get_pricing(model_id, currency="EUR")
    print(f"Pricing for {model_id} ({pricing.currency}):")
    print(f"  Input per 1M tokens: €{pricing.input_per_million:.2f}")
    print(f"  Output per 1M tokens: €{pricing.output_per_million:.2f}")

    # Compute total cost for a usage
    total = await compute_cost(model_id, input_tokens=1000, output_tokens=500, currency="EUR", cache_read_tokens=250, cache_creation_tokens=100)
    print(f"Total cost (EUR): €{total:.6f}")


asyncio.run(main())

Sync Example

For simpler scripts, Jupyter notebooks, or environments where async is inconvenient, use the sync versions:

from tokenpricing import get_pricing_sync, compute_cost_sync

model_id = "openai/gpt-5.2"

# Get pricing (cached transparently for ~6 hours)
pricing = get_pricing_sync(model_id, currency="EUR")
print(f"Pricing for {model_id} ({pricing.currency}):")
print(f"  Input per 1M tokens: €{pricing.input_per_million:.2f}")
print(f"  Output per 1M tokens: €{pricing.output_per_million:.2f}")

# Compute total cost for a usage
total = compute_cost_sync(
    model_id,
    input_tokens=1000,
    output_tokens=500,
    currency="EUR",
    cache_read_tokens=250,
    cache_creation_tokens=100,
)
print(f"Total cost (EUR): €{total:.6f}")

Both sync and async APIs share the same underlying cache, so mixing them won't cause duplicate fetches.

Helpful Error Messages

When you make a typo in a model ID or currency code, tokenpricing provides helpful "Did you mean?" suggestions:

from tokenpricing import get_pricing_sync

# Typo in model name: "gpt4" instead of "gpt-4"
get_pricing_sync("openai/gpt4")
# ValueError: Model not found: openai/gpt4. Did you mean 'openai/gpt-4'?

# Typo in currency: "ERU" instead of "EUR"
get_pricing_sync("openai/gpt-4", currency="ERU")
# ValueError: Unsupported currency: ERU. Did you mean 'EUR'?

CLI

Install via UV or pip, then use the tokenpricing command.

# Show price per 1M tokens (USD default)
tokenpricing pricing openai/gpt-5.2

# Convert to another currency (uses cached FX rates)
tokenpricing pricing openai/gpt-5.2 --currency EUR

# JSON output for scripting
tokenpricing pricing openai/gpt-5.2 --json

# Compute total cost for a usage
tokenpricing cost openai/gpt-5.2 --in 1000 --out 500 --cache-read 250 --cache-write 100 --currency EUR

Claude Code skill

The repository also ships a Claude Code marketplace manifest at .claude-plugin/marketplace.json and the canonical hidden skill at skills/tokenpricing/SKILL.md.

The skill wraps the existing tokenpricing CLI for pricing lookups and workload cost calculations. It does not estimate token counts from raw text.

The canonical skill source in this repository is skills/tokenpricing/SKILL.md.

Data Source

Pricing data is sourced from the canonical tokenpricing database generated in this repository from OpenRouter and LiteLLM. The raw data is available at:

https://raw.githubusercontent.com/Atena-IT/tokenpricing/main/database/current/prices.json

Caching uses async-lru with a 6-hour TTL aligned to the canonical sync cadence. Caching is fully transparent to callers of the public API.

Note: Pricing data is denominated in USD; currency conversion uses daily USD base rates from the JSDelivr currency API with a 24h cache (keys uppercased).

Development

This project uses uv as the package manager.

Setup

uv sync

Running Tests

uv run pytest

Quality (pre-commit)

Use pre-commit to run formatting and linting consistently:

# One-time setup (from repo root)
uv run pre-commit install

# Run all hooks on the codebase (from repo root)
uv run pre-commit run -a

This runs ruff-format and ruff with --fix, along with basic repo hygiene checks.

API Surface

The public API includes both async and sync versions:

  • Async: get_pricing, compute_cost
  • Sync: get_pricing_sync, compute_cost_sync

Internal modules and models are not considered public and may change. Both APIs share the same cache, so you can mix async and sync calls without performance penalty.

Credits

License

See LICENSE file for details.

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

tokenpricing-0.2.0.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

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

tokenpricing-0.2.0-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file tokenpricing-0.2.0.tar.gz.

File metadata

  • Download URL: tokenpricing-0.2.0.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tokenpricing-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d67782047d78b18916320aff85c0993f1bb91f396715cba2908e62281d4f7853
MD5 2c7d8453c98e966ed311b771a57bebba
BLAKE2b-256 77796bc855efb2f7d77661eaf68685f1412a0c13540229ed1b14c04573819e09

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokenpricing-0.2.0.tar.gz:

Publisher: python-release.yml on Atena-IT/tokenpricing

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

File details

Details for the file tokenpricing-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tokenpricing-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tokenpricing-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 938be0b5612fa87a46ef4fa98e291d3e3171e35f0d888a71d1ce85eca8798e87
MD5 f4aa1cd4542d88eac6b5b84580beabb3
BLAKE2b-256 96765f2f11fa50f61b13f55c567a492fb4f97e3ce67ae1e221e5557515f0f1c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokenpricing-0.2.0-py3-none-any.whl:

Publisher: python-release.yml on Atena-IT/tokenpricing

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