Skip to content

Utilities for calculating token costs for LLMs using LiteLLM pricing data.

License

Notifications You must be signed in to change notification settings

phil65/tokonomics

Repository files navigation

Tokonomics

PyPI License Package status Daily downloads Weekly downloads 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 week Github commits this month Github commits this year Package status Code style: black 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",
        prompt_tokens=100,    # tokens used in the prompt
        completion_tokens=50,  # tokens used in the completion
    )

    if costs:
        print(f"Prompt cost: ${costs.prompt_cost:.6f}")
        print(f"Completion cost: ${costs.completion_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()

Getting Model Token Limits

You can retrieve the token limits for a model using get_model_limits:

from tokonomics import get_model_limits

async def main():
    # Get token limit information for a model
    limits = await get_model_limits("gpt-4")

    if limits:
        print(f"Maximum total tokens: {limits.total_tokens}")
        print(f"Maximum input tokens: {limits.input_tokens}")
        print(f"Maximum output tokens: {limits.output_tokens}")
    else:
        print("Could not find limit data for model")

The function returns a TokenLimits object with three fields:

  • total_tokens: Maximum combined tokens (input + output) the model supports
  • input_tokens: Maximum number of input/prompt tokens
  • output_tokens: Maximum number of output/completion tokens

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.prompt_cost:.6f}")
    print(f"Completion cost: ${costs.completion_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.

About

Utilities for calculating token costs for LLMs using LiteLLM pricing data.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages