Skip to main content

Calcuate costs for LLM Usage based on token count

Project description

Tokonomics

PyPI License Package status Monthly downloads Distribution format Wheel availability Python version Implementation Releases Github Contributors Github Discussions Github Forks Github Issues Github Issues Github Watchers Github Stars Github Repository size Github last commit Github release date Github language count Github commits this month Package status PyUp

Read the documentation!

Calculate costs for LLM usage based on token counts using LiteLLM's pricing data.

Installation

pip install tokonomics

Features

  • Automatic cost calculation for various LLM models
  • Detailed cost breakdown (prompt, completion, and total costs)
  • Caches pricing data locally (24-hour default cache duration)
  • Supports multiple model name formats (e.g., "gpt-4", "openai:gpt-4")
  • Asynchronous API
  • Fully typed with runtime type checking
  • Zero configuration required

Usage

import asyncio
from tokonomics import calculate_token_cost

async def main():
    # Calculate cost with token counts
    costs = await calculate_token_cost(
        model="gpt-4",
        input_tokens=100,    # tokens used in the prompt
        output_tokens=50,  # tokens used in the completion
    )

    if costs:
        print(f"Prompt cost: ${costs.input_cost:.6f}")
        print(f"Completion cost: ${costs.output_cost:.6f}")
        print(f"Total cost: ${costs.total_cost:.6f}")
    else:
        print("Could not determine cost for model")

asyncio.run(main())

You can customize the cache timeout:

from tokonomics import get_model_costs, clear_cache

# Get model costs with custom cache duration (e.g., 1 hour)
costs = await get_model_costs("gpt-4", cache_timeout=3600)
if costs:
    print(f"Input cost per token: ${costs['input_cost_per_token']}")
    print(f"Output cost per token: ${costs['output_cost_per_token']}")

clear_cache()

Pydantic-AI Integration

If you're using pydantic-ai, you can directly calculate costs from its Usage objects:

from tokonomics import calculate_pydantic_cost

# Assuming you have a pydantic-ai Usage object
costs = await calculate_pydantic_cost(
    model="gpt-4",
    usage=usage_object,
)

if costs:
    print(f"Prompt cost: ${costs.input_cost:.6f}")
    print(f"Completion cost: ${costs.output_cost:.6f}")
    print(f"Total cost: ${costs.total_cost:.6f}")

Model Name Support

The library supports multiple formats for model names:

  • Direct model names: "gpt-4"
  • Provider-prefixed: "openai:gpt-4"
  • Provider-path style: "openai/gpt-4"

Names are matched case-insensitively.

Data Source

Pricing data is sourced from LiteLLM's pricing repository and is automatically cached locally using hishel. The cache is updated when pricing data is not found or has expired.

Requirements

  • Python 3.12+
  • httpx
  • platformdirs
  • upath
  • pydantic (≥ 2.0)

License

This project is licensed under the MIT License - see the 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

tokonomics-1.2.18.tar.gz (65.6 kB view details)

Uploaded Source

Built Distribution

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

tokonomics-1.2.18-py3-none-any.whl (103.9 kB view details)

Uploaded Python 3

File details

Details for the file tokonomics-1.2.18.tar.gz.

File metadata

  • Download URL: tokonomics-1.2.18.tar.gz
  • Upload date:
  • Size: 65.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tokonomics-1.2.18.tar.gz
Algorithm Hash digest
SHA256 6b40461c8c8ce71f4bc378c61a4743a55b20b5ddee50d27b9398c85c29279c17
MD5 85b5b3f28413da87549cb7abbe2a65f6
BLAKE2b-256 2e0ac8093f6836f898a9f587aa30786bba25f08e92c9c11f13f9e638b0b97688

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokonomics-1.2.18.tar.gz:

Publisher: build.yml on phil65/tokonomics

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

File details

Details for the file tokonomics-1.2.18-py3-none-any.whl.

File metadata

  • Download URL: tokonomics-1.2.18-py3-none-any.whl
  • Upload date:
  • Size: 103.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tokonomics-1.2.18-py3-none-any.whl
Algorithm Hash digest
SHA256 8ff42a59c16be309a0d8d29e66a079f0b1ed72dce4380a545d8f20c32e4c9ab8
MD5 7e8a13bfcfeffe8620435df1d7b17cb2
BLAKE2b-256 b1c4fc9b68acb9d10a44b2c7121c8e02094c46244f9e7b07fa86ef47ee2d1073

See more details on using hashes here.

Provenance

The following attestation bundles were made for tokonomics-1.2.18-py3-none-any.whl:

Publisher: build.yml on phil65/tokonomics

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