Skip to main content

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

Metadata

Release files for llm-costs 0.1.5

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

Source distribution (sdist)

Source distribution for llm-costs 0.1.5
File Size Uploaded Provenance
llm_costs-0.1.5.tar.gz 15.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-costs 0.1.5
File Interpreter ABI Platform
llm_costs-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 27.2 kB

Release files / llm_costs-0.1.5.tar.gz

Download URL llm_costs-0.1.5.tar.gz
Size 15.3 kB
Tags Source
SHA-256 checksum
How to use checksums
61ad651a23a6e7f2f0de0d6d178738eae4a82928370c95386f71f441193bcc2f
BLAKE2b-256 checksum
How to use checksums
e73530ed726928b5691998131a6f80fcf062f9dad44e4347b043ee193f1ae19f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.9

Release files / llm_costs-0.1.5-py3-none-any.whl

Download URL llm_costs-0.1.5-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
93625ea56a1291b2f289cdc5278e196116111132df1e3459bd198498c8b1db51
BLAKE2b-256 checksum
How to use checksums
9420089fde660b61ca048b8c7cadd652521b1395d0df518c1588623992232dca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.9

Security for llm-costs 0.1.5

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

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