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 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 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¶
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.