Skip to main content

LLM cost calculator for major providers

Project description

llm-costs

LLM cost calculator for major providers. Calculates API costs from token usage data.

Installation

pip install llm-costs

Or with uv:

uv add llm-costs

Usage

from llm_costs import calculate_cost, get_model_pricing

# Calculate cost from LangChain UsageMetadata structure
result = calculate_cost(
    provider="anthropic",
    model="claude-sonnet-4-20250514",
    usage={
        "input_tokens": 1000,
        "output_tokens": 500,
        "total_tokens": 1500,
    },
)

print(f"Cost: ${result['cost']:.6f}")  # Cost: $0.010500
print(f"Input: ${result['breakdown']['input_cost']:.6f}")
print(f"Output: ${result['breakdown']['output_cost']:.6f}")

With Prompt Caching

result = calculate_cost(
    provider="anthropic",
    model="claude-sonnet-4-20250514",
    usage={
        "input_tokens": 1000,
        "output_tokens": 500,
        "total_tokens": 1500,
        "input_token_details": {
            "cache_read": 5000,
            "cache_creation": 0,
        },
    },
)

Batch Pricing

result = calculate_cost(
    provider="anthropic",
    model="claude-sonnet-4-20250514",
    usage={"input_tokens": 1000, "output_tokens": 500, "total_tokens": 1500},
    batch=True,  # 50% discount
)

Get Model Pricing Info

pricing = get_model_pricing("anthropic", "claude-sonnet-4-20250514")
print(f"Input: ${pricing['input']}/MTok")   # Input: $3.0/MTok
print(f"Output: ${pricing['output']}/MTok") # Output: $15.0/MTok

List Available Models

from llm_costs.calculator import list_models, list_providers

providers = list_providers()  # ['anthropic', 'openai', 'google']
models = list_models("anthropic")  # ['claude-opus-4-5-20251101', ...]

Supported Providers

Anthropic

Model Input Output Cache Read
claude-opus-4-5-20251101 $5.00 $25.00 $0.50
claude-opus-4-1-20250414 $15.00 $75.00 $1.50
claude-opus-4-20250514 $15.00 $75.00 $1.50
claude-sonnet-4-5-20250514 $3.00 $15.00 $0.30
claude-sonnet-4-20250514 $3.00 $15.00 $0.30
claude-3-7-sonnet-20250219 $3.00 $15.00 $0.30
claude-haiku-4-5-20250514 $1.00 $5.00 $0.10
claude-3-5-haiku-20241022 $0.80 $4.00 $0.08
claude-3-opus-20240229 $15.00 $75.00 $1.50
claude-3-haiku-20240307 $0.25 $1.25 $0.03

Prices per million tokens. Long context pricing (>200K tokens) applies to Sonnet models.

OpenAI

Model Input Output Cache Read
gpt-5.2 $1.75 $14.00 $0.175
gpt-5.1 $1.25 $10.00 $0.125
gpt-5 $1.25 $10.00 $0.125
gpt-5-mini $0.25 $2.00 $0.025
gpt-5-nano $0.05 $0.40 $0.005
gpt-5.2-pro $21.00 $168.00 -
gpt-5-pro $15.00 $120.00 -
gpt-4.1 $2.00 $8.00 $0.50
gpt-4.1-mini $0.40 $1.60 $0.10
gpt-4.1-nano $0.10 $0.40 $0.025
gpt-4o $2.50 $10.00 $1.25
gpt-4o-mini $0.15 $0.60 $0.075
o1 $15.00 $60.00 $7.50
o1-pro $150.00 $600.00 -
o1-mini $1.10 $4.40 $0.55
o3 $2.00 $8.00 $0.50
o3-pro $20.00 $80.00 -
o3-mini $1.10 $4.40 $0.55
o3-deep-research $10.00 $40.00 $2.50
o4-mini $1.10 $4.40 $0.275
o4-mini-deep-research $2.00 $8.00 $0.50
computer-use-preview $3.00 $12.00 -

Prices per million tokens (Standard tier).

Google

Model Input Output Cache Read
gemini-3-pro-preview $2.00 $12.00 $0.20
gemini-2.5-pro $1.25 $10.00 $0.125
gemini-2.5-flash $0.30 $2.50 $0.03
gemini-2.5-flash-lite $0.10 $0.40 $0.01
gemini-2.0-flash $0.10 $0.40 $0.025
gemini-2.0-flash-lite $0.075 $0.30 -

Prices per million tokens (Paid tier). Long context pricing (>200K tokens) applies to Pro models.

Usage Schema

The library accepts token usage in LangChain's UsageMetadata format:

{
    "input_tokens": int,
    "output_tokens": int,
    "total_tokens": int,
    "input_token_details": {
        "cache_read": int,      # Cached tokens read
        "cache_creation": int,  # Tokens written to cache
    },
    "output_token_details": {
        "reasoning": int,       # Reasoning tokens (o1/thinking models)
    },
}

Return Value

calculate_cost() returns a CostResult:

{
    "cost": 0.0105,           # Total cost in USD
    "currency": "USD",
    "breakdown": {
        "input_cost": 0.003,
        "output_cost": 0.0075,
        "cache_read_cost": 0.0,      # If applicable
        "cache_creation_cost": 0.0,  # If applicable
    },
    "pricing_used": {
        "input_per_mtok": 3.0,
        "output_per_mtok": 15.0,
        "batch_applied": False,
        "long_context_applied": False,
    },
}

License

MIT

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

llm_costs-0.1.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

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

llm_costs-0.1.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llm_costs-0.1.0.tar.gz
  • Upload date:
  • Size: 14.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.9

File hashes

Hashes for llm_costs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 463ac4f415ed224148e0611da20cc70e547f9c7cb6aad3295bf4af329a98b085
MD5 213097107dd6d9b1ec1b64094a3f2b41
BLAKE2b-256 dc63ff65b06eeb79d2deee1a771508e3c6e6fff1cab7c63078406f536aa42eab

See more details on using hashes here.

File details

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

File metadata

  • Download URL: llm_costs-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.9

File hashes

Hashes for llm_costs-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a6faf4fd1e53debae4975aac805a707e85c905eb64bcb0dd79fa26fadd85c37d
MD5 9a042748ec051a592165d634bd978581
BLAKE2b-256 03327f84548e4ea4479aceaef26f92b8c1b77ccca91fc89352a5131a27b48e88

See more details on using hashes here.

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