Skip to main content

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.

Release files for tokonomics 1.2.18

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tokonomics 1.2.18
File Size Uploaded
tokonomics-1.2.18.tar.gz 65.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tokonomics 1.2.18
File Interpreter ABI Platform
tokonomics-1.2.18-py3-none-any.whl Python 3 none any Details

Total release size: 169.5 kB

Release files / tokonomics-1.2.18.tar.gz

Download URL tokonomics-1.2.18.tar.gz
Size 65.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6b40461c8c8ce71f4bc378c61a4743a55b20b5ddee50d27b9398c85c29279c17
BLAKE2b-256 checksum
How to use checksums
2e0ac8093f6836f898a9f587aa30786bba25f08e92c9c11f13f9e638b0b97688
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 25, 2026.

Transparency log

Release files / tokonomics-1.2.18-py3-none-any.whl

Download URL tokonomics-1.2.18-py3-none-any.whl
Size 103.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8ff42a59c16be309a0d8d29e66a079f0b1ed72dce4380a545d8f20c32e4c9ab8
BLAKE2b-256 checksum
How to use checksums
b1c4fc9b68acb9d10a44b2c7121c8e02094c46244f9e7b07fa86ef47ee2d1073
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 25, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.2.18 This release

2 release files

1.2.17

2 release files

1.2.16

2 release files

1.2.15

2 release files

1.2.14

2 release files

1.2.13

2 release files

1.2.12

2 release files

1.2.11

2 release files

1.2.9

2 release files

1.2.7

2 release files

1.2.6

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.1

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.14

2 release files

0.3.13

2 release files

0.3.12

2 release files

0.3.10

2 release files

0.3.9

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page