Skip to main content

Cross-provider AI model pricing for Python

Project description

Txaion

Txaion Model Pricing

Lightweight, cross-provider model pricing for Python.

English | 繁體中文

Txaion Model Pricing is a lightweight Python package for querying cross-provider model information and calculating USD costs for input, output, and cached-input tokens.

Developed and maintained by Txaion.

The current version is 0.1.0 and requires Python 3.10 or later.

Installation

Install the package from the project directory:

python -m pip install .

Install the development dependencies for testing and building:

python -m pip install -e ".[dev]"

Quick start

from txaion_model_pricing import calculate_cost, count_models, get_model_details

print(count_models())

input_cost = calculate_cost("gpt-4o", 1_000_000, "input")
output_cost = calculate_cost("gpt-4o", 1_000_000, "output")
cached_cost = calculate_cost("gpt-4o", 1_000_000, "cached")

print(input_cost)   # Decimal("2.5000000")
print(output_cost)  # Decimal("10.00000")
print(cached_cost)  # Decimal("1.25000000")

details = get_model_details("gpt-4o")
print(details["max_input_tokens"])

All costs are returned as decimal.Decimal values. The package does not round results or perform currency conversion.

Public API

count_models() -> int

Returns the number of models in the bundled snapshot. Metadata entries such as sample_spec are excluded.

calculate_cost(model, tokens, token_type) -> Decimal

Calculates the USD cost using the model's per-token price:

  • input maps to input_cost_per_token
  • output maps to output_cost_per_token
  • cached maps to cache_read_input_token_cost

tokens must be a non-negative integer. If the requested price field is unavailable, the package does not treat the cost as zero or fall back to a different price.

get_model_details(model) -> dict

Returns a deep copy of the model data. Modifying the returned value does not affect the package's internal cache.

Error handling

All domain-specific exceptions inherit from ModelPriceError:

from txaion_model_pricing import (
    InvalidTokenCountError,
    InvalidTokenTypeError,
    NotFound,
    PriceUnavailableError,
    calculate_cost,
)

try:
    cost = calculate_cost("unknown-model", 1_000, "input")
except NotFound:
    ...
except PriceUnavailableError:
    ...
except (InvalidTokenCountError, InvalidTokenTypeError):
    ...

Price data

The package includes a pinned snapshot of the LiteLLM model price and context-window data. The source commit, retrieval time, and SHA-256 checksum are recorded in src/txaion_model_pricing/data_source.json. See THIRD_PARTY_NOTICES.md for third-party attribution and licensing information.

Maintainers must specify an immutable commit SHA or tag when updating the snapshot:

python scripts/update_prices.py <commit-or-tag>

The update tool downloads and validates the JSON before atomically replacing the snapshot and its metadata. Moving refs such as main, master, and latest are rejected.

Limitations

  • Version 0.1.0 only calculates token-based input, output, and cached-input costs.
  • Image, audio, search, tool-call, session, and storage pricing are not supported by calculate_cost().
  • Model providers may change their prices at any time. Results depend on the bundled snapshot and may not match current provider pricing. Verify official provider prices before using the results for billing or budget enforcement.

Development

ruff check .
pytest
python -m build

License

This project and the vendored LiteLLM data are redistributed under the MIT License. See LICENSE and THIRD_PARTY_NOTICES.md 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

txaion_model_pricing-0.1.0.tar.gz (90.0 kB view details)

Uploaded Source

Built Distribution

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

txaion_model_pricing-0.1.0-py3-none-any.whl (86.5 kB view details)

Uploaded Python 3

File details

Details for the file txaion_model_pricing-0.1.0.tar.gz.

File metadata

  • Download URL: txaion_model_pricing-0.1.0.tar.gz
  • Upload date:
  • Size: 90.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for txaion_model_pricing-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2f7cea1c07a0d4792344fa092993bbb64ab79d3ee158a3e8aabbddf45f5086e6
MD5 852a03e96fe376ee91f98f025728886a
BLAKE2b-256 3d02c331b021572b1b39641e3de81aa7ec3be8cc19bc5098c9415b1c96e6fbc6

See more details on using hashes here.

Provenance

The following attestation bundles were made for txaion_model_pricing-0.1.0.tar.gz:

Publisher: publish.yml on txaion/txaion-model-pricing

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

File details

Details for the file txaion_model_pricing-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for txaion_model_pricing-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4d65f85de67fda1ccd4f754d696a11aa7724e722076480e16ce8f421eb624da2
MD5 bb6b91e5c90b549783fec73912a5ff27
BLAKE2b-256 d71489c0adf58d413e303138512fa838395802d222400ad0db6e48014d8d0c6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for txaion_model_pricing-0.1.0-py3-none-any.whl:

Publisher: publish.yml on txaion/txaion-model-pricing

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