Skip to content

latent.pricing

Model pricing fetch, cache, and cost calculation.

Fetches pricing data from https://models.dev/api.json with a 24h local cache at .latent/cache/model_pricing.json.

Functions

calculate_cost

calculate_cost(cost_entry: dict | None, input_tokens: int, output_tokens: int) -> float

Calculate cost in dollars from token counts and per-1M-token cost entry.

The cost_entry is expected to have input and output fields with per-1M-token prices as floats (models.dev format).

load_pricing

load_pricing(refresh: bool = False) -> list[dict] | None

Load pricing data, using cache when fresh.

Args: refresh: Force re-fetch even if cache is fresh.

Returns: List of model pricing entries, or None if unavailable.

lookup_model_cost

lookup_model_cost(pricing_data: list | dict | None, model_id: str) -> dict | None

Find cost entry for model_id.

Tries exact match on the id field, then strips a provider prefix (e.g. openai/gpt-4o -> gpt-4o) for a second attempt.

Returns: Cost dict with input and output per-1M-token prices, or None.

Attributes

CACHE_TTL_SECONDS

PRICING_URL