Cost

ModelPricing

USD-per-million-token rates for a model family.

Usage

Source

ModelPricing(
    input,
    output,
    cache_read,
    cache_write_5m,
    cache_write_1h,
)

Standard service tier, standard (non-long-context) pricing — current Claude 4.x models serve their 1M context at standard rates with no long-context premium. Override via the pricing argument to resolve_pricing for other tiers.

Attributes

input: float

USD per million uncached input tokens.

output: float

USD per million output tokens.

cache_read: float

USD per million tokens served from the cache.

cache_write_5m: float

USD per million tokens written to the 5-minute cache.

cache_write_1h: float
USD per million tokens written to the 1-hour cache.

CostBreakdown

The per-component and total USD cost of a single turn’s token usage.

Usage

Source

CostBreakdown(
    input_cost, output_cost, cache_read_cost, cache_write_cost, total
)

Attributes

input_cost: float

USD cost of the uncached input tokens.

output_cost: float

USD cost of the output tokens.

cache_read_cost: float

USD cost of the cache-read tokens.

cache_write_cost: float

USD cost of the cache-creation tokens, summed across TTL buckets.

total: float
The sum of the four component costs.

cost_of()

Compute the USD cost of a turn’s token usage under a model’s rates.

Usage

Source

cost_of(usage, model)

The cache-creation split prefers the per-TTL usage.cache_creation when present, falling back to the flat usage.cache_creation_input_tokens as 5-minute writes with no 1-hour share.

Parameters

usage: Usage

The turn’s token usage and cache accounting.

model: str
The model id whose rates apply.

Returns

CostBreakdown
The per-component and total cost.

Example

>>> cost_of(usage, "claude-haiku-4-5-20251001").total

0.05759

cost_of_assistant()

Compute the cost of an assistant turn, or None when it carries no usage.

Usage

Source

cost_of_assistant(event)

Parameters

event: AssistantEvent
The assistant turn whose usage is priced.

Returns

CostBreakdown | None
The cost breakdown, or None when event.usage is None.

resolve_pricing()

Return the pricing row whose family key is a substring of model.

Usage

Source

resolve_pricing(model, *, pricing=PRICING)

Family keys are mutually exclusive substrings, so at most one matches — "claude-haiku-4-5-20251001" resolves to haiku, "claude-opus-4-8" to opus, and a bare "sonnet" to sonnet.

Parameters

model: str

The model id to resolve.

pricing: Mapping[str, ModelPricing] = PRICING
The family-keyed pricing table to search.

Raises

KeyError
If no family key is a substring of model.

Example

>>> resolve_pricing("claude-opus-4-8").input

5.0

PRICING

PRICING: Mapping[str, ModelPricing] = {
    "fable": ModelPricing(
        input=10.0,
        output=50.0,
        cache_read=1.0,
        cache_write_5m=12.5,
        cache_write_1h=20.0,
    ),
    "opus": ModelPricing(
        input=5.0, output=25.0, cache_read=0.5, cache_write_5m=6.25, cache_write_1h=10.0
    ),
    "sonnet": ModelPricing(
        input=3.0, output=15.0, cache_read=0.3, cache_write_5m=3.75, cache_write_1h=6.0
    ),
    "haiku": ModelPricing(
        input=1.0, output=5.0, cache_read=0.1, cache_write_5m=1.25, cache_write_1h=2.0
    ),
}