Skip to main content

Tokonomics

PyPI License Package status Monthly downloads Distribution format Wheel availability Python version Implementation Releases Github Contributors 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 LLM usage costs from token counts using LiteLLM's pricing data, and discover available models (pricing, context window, modalities) across many providers.

Installation

pip install tokonomics
# optional: exact token counting via tiktoken
pip install "tokonomics[tiktoken]"

Quickstart

import asyncio

from tokonomics import calculate_token_cost


async def main():
    costs = await calculate_token_cost(
        model="gpt-4o",
        input_tokens=100,  # tokens used in the prompt
        output_tokens=50,  # tokens used in the completion
    )
    if costs:
        print(f"Input cost: ${costs.input_cost:.6f}")
        print(f"Output cost: ${costs.output_cost:.6f}")
        print(f"Total cost: ${costs.total_cost:.6f}")
    else:
        print("Could not determine cost for model")


asyncio.run(main())

Features

  • Cost calculation for thousands of models from LiteLLM's pricing JSON (Decimal input/output/total breakdown)
  • Flexible model names: "gpt-4o", "openai:gpt-4o", "openai/gpt-4o" (case-insensitive)
  • Model capabilities: context limits, function calling, vision, audio, prompt caching
  • Model discovery across providers (OpenAI, Anthropic, OpenRouter, Groq, Mistral, Gemini, GitHub Models, Copilot, DeepSeek, xAI, Ollama, models.dev, ...)
  • Token counting with tiktoken, transformers or a heuristic fallback
  • pydantic-ai integration via calculate_pydantic_cost
  • Generated Literal types of known model ids (tokonomics.ModelName)
  • Async API, fully typed, pricing data cached locally

Usage

Model costs and capabilities

from tokonomics import get_model_capabilities, get_model_costs, reset_cache

# Per-token prices; cache_timeout controls how long pricing data is cached (seconds)
costs = await get_model_costs("openai:gpt-4o", 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']}")

caps = await get_model_capabilities("gpt-4o")
if caps:
    print(caps.max_input_tokens, caps.supports_vision, caps.supports_function_calling)

reset_cache()  # clear the in-memory cache

Pydantic-AI integration

from tokonomics import calculate_pydantic_cost

# `usage` is a pydantic-ai usage object (anything with input_tokens / output_tokens)
costs = await calculate_pydantic_cost(model="gpt-4o", usage=result.usage())
if costs:
    print(f"Total cost: ${costs.total_cost:.6f}")

Model discovery

from tokonomics import get_all_models

# Without `providers`, all providers whose API keys are set are queried.
models = await get_all_models(providers=["openai", "anthropic", "openrouter"])
for model in models[:5]:
    print(model.pydantic_ai_id, model.context_window, model.pricing)

Providers read their API keys from the usual environment variables (OPENROUTER_API_KEY, GROQ_API_KEY, MISTRAL_API_KEY, GEMINI_API_KEY, ...). A synchronous variant is available as tokonomics.model_discovery.get_all_models_sync.

Token counting

from tokonomics import count_tokens

count_tokens("Hello world, how are you?", model="gpt-4o")

Uses tiktoken if installed, then transformers, then a rough approximation.

Data sources

Pricing and capability data come from LiteLLM's model_prices_and_context_window.json; model discovery uses the providers' model APIs and models.dev. Responses are cached locally (24 hours by default).

Requirements

  • Python 3.13+

Documentation

Full API reference: https://phil65.github.io/tokonomics/

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for tokonomics 1.5.0

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.5.0
File Size Uploaded Provenance Actions
tokonomics-1.5.0.tar.gz 105.5 kB With provenance Details for tokonomics-1.5.0.tar.gz

Built distribution (wheel)

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

Total release size: 241.8 kB

Release files / tokonomics-1.5.0.tar.gz

Download URL tokonomics-1.5.0.tar.gz
Size 105.5 kB
Tags Source
SHA-256 checksum
How to use checksums
98ea07dbd9c17a7d27b1ff38bea3cf0f8c04bb62ebfe178ef5049621a3eea013
BLAKE2b-256 checksum
How to use checksums
3d0d021c3d03efc97cca4f83e206ee153faf7d17965bad0fb2fe4f1e634863bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 26, 2026.

Transparency log

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

Download URL tokonomics-1.5.0-py3-none-any.whl
Size 136.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aec5824469924aa0c51747ec287c9651f09afa23de3c77357d468e8c46d5b471
BLAKE2b-256 checksum
How to use checksums
0abe1725a5d21621373d9562da15803d56d367f0031e75c65ef69670f379de2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 26, 2026.

Transparency log

Security for tokonomics 1.5.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for tokonomics-1.5.0.tar.gz

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 Nov 26, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.5.0 This release

2 release files Full provenance

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