Files

41 lines
1.8 KiB
Python

"""What a message costs at list prices. They weight how a chunk's recorded cost splits between its messages, and a Spent block records them beside what a stretch spent, so a wrong price stays wrong in every outcome written after it."""
import dataclasses
from claude_code.tokens import tokens_of
@dataclasses.dataclass(frozen=True)
class Price:
"""Dollars per million tokens. A cache write costs 1.25 times the input for a five-minute cache and 2 times for an hour's, as Opus 5.5's $5 and $8 show."""
input: float
read: float
output: float
@property
def write_5m(self) -> float:
return self.input * 1.25
@property
def write_1h(self) -> float:
return self.input * 2
# Anthropic's list prices as the claude-api skill lists them (cached 2026-10-06, and Fable 5.2's on 2026-11-03), with Opus 5.5's cache prices from `docs/decisions.md` → `loop-claude: the worker's claude compacts…`. Haiku 5.5's cache read is assumed at a tenth of its input, and its dearer prompts above 200k tokens are left out. Keep each price current with that list: the prices are archived as the ones in force.
PRICES = {
# Archived chunks that ran on Fable 5.1 are still priced from here.
"claude-fable-5-1": Price(10, 0.25, 50),
"claude-fable-5-2": Price(8, 0.2, 40),
"claude-haiku-5-5": Price(0.1, 0.01, 0.5),
"claude-opus-5-5": Price(4, 0.2, 20),
"claude-sonnet-5-5": Price(2, 0.2, 10),
}
def listed(model: str, usage: object) -> float:
"""The cost of one message's usage at the model's list price; 0 for a model without one."""
price = PRICES.get(model)
if not price:
return 0.0
tokens = tokens_of(usage)
return (tokens.input * price.input + tokens.write_5m * price.write_5m + tokens.write_1h * price.write_1h + tokens.read * price.read + tokens.output * price.output) / 1_000_000