Skip to content

API Reference parrot.observability.cost.calculator

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.observability.cost.calculator

View source

CostCalculator — stateless USD cost estimation for LLM API calls.

FEAT-177 TASK-1232.

Pricing tables are loaded from bundled JSON files once at first CostCalculator() construction and cached at module level. No filesystem I/O occurs in the hot path (cost_usd).

Override path: ObservabilityConfig.pricing_override_path or PARROT_PRICING_PATH env var (resolved by setup_telemetry).

Spec §3 Module 5, §8 D5 (bundled JSON, 90-day stale warning).

Class CostCalculator

Stateless USD cost calculator using bundled or overridden pricing tables.

Pricing tables are loaded once at module level on first construction. All subsequent constructions reuse the cached data — no filesystem I/O.

Parameter Type Default Description
override_path Optional[str] None Optional directory containing <provider>.json files that override bundled pricing via deep-merge (per-model granularity).
stale_warn_days int 90 Emit a WARN at boot for any provider file older than this many days. Default: 90.
today Optional[date] None Reference date for staleness check. Defaults to date.today(). Pass explicitly in tests to avoid time-dependent failures.

Method CostCalculator.cost_usd

Compute USD cost for a single LLM API call.

cost_usd(*, provider: str, model: str, input_tokens: int, output_tokens: int, cached_input_tokens: int = 0, cache_creation_tokens: int = 0, at: Optional[date] = None) -> Optional[float]

Parameter Type Default Description
provider str gen_ai.system value (e.g. "openai", "anthropic"). For Google models use "gemini" (google.json filename stem).
model str Model identifier matching a key in the pricing JSON.
input_tokens int Number of input tokens billed.
output_tokens int Number of output tokens billed.
cached_input_tokens int 0 Number of cached input tokens (discount tier). Default 0.
cache_creation_tokens int 0 Number of cache-creation tokens, billed as an additive tier. They are never subtracted from input tokens.
at Optional[date] None Date for selecting a dated price entry. Defaults to the latest.

Returns: Optional[float] — USD cost as a float rounded to 6 decimal places, or None if the (provider, model) pair is not found in any pricing table.

Method CostCalculator.price_call

Compute a cost together with its pricing revision and effective date.

price_call(*, provider: str, model: str, input_tokens: int, output_tokens: int, cached_input_tokens: int = 0, cache_creation_tokens: int = 0, at: Optional[date] = None) -> PricedCost

Parameter Type Default Description
provider str gen_ai.system value ("gemini" maps to google.json).
model str Model identifier matching a key in the pricing JSON.
input_tokens int Number of input tokens billed.
output_tokens int Number of output tokens billed.
cached_input_tokens int 0 Number of cached input tokens (discount tier).
cache_creation_tokens int 0 Number of cache-creation tokens (additive tier).
at Optional[date] None Date for selecting a dated price entry. Defaults to the latest.

Returns: PricedCost — The priced cost, or an unpriced PricedCost for an unknown model.

Method CostCalculator.revision_for

Return the revision and date for a price entry without computing cost.

revision_for(provider: str, model: str, at: Optional[date] = None) -> tuple[Optional[str], Optional[str]]

Parameter Type Default Description
provider str
model str
at Optional[date] None

Returns: tuple[Optional[str], Optional[str]]

Class PricedCost

One computed cost plus the price revision that produced it (FEAT-783 S9).

Fields:

Field Type Default Description
usd Optional[float] None
price_revision Optional[str] None
valid_from Optional[str] None
priced bool False

Attribute logger

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally