Python API: Usage and cost

Tokens and an estimated price for a run. Generated from the docstrings and type hints of criogenai 0.1.7. See also: Python API overview, the same module in TypeScript.

What a run cost: which models answered, with their calls and tokens (from the engine's per-message metadata), the call's wall time, and a cost estimate from prices the caller supplies (they change too often to ship).

from criogenai import estimate_cost

Contents#

NameKindSummary
estimate_costfunctionEstimates what a run cost in USD, from prices you supply.

estimate_cost#

def estimate_cost(
    result: Any,
    prices: Mapping[str, Mapping[str, float]],
) -> Optional[float]

Estimates what a run cost in USD, from prices you supply.

CriogenAI ships no prices (they change too often). Cached input tokens are charged at cache_read (or input when it is missing). Reasoning tokens count as output tokens.

Parameters

  • result (Any): A RunResult or Reply; its models field is read.
  • prices (Mapping[str, Mapping[str, float]]): USD per million tokens for each model, keyed by "provider:model" or by model name: {"input": ..., "output": ..., "cache_read": ...} (cache_read is optional).

Returns: The cost in USD, or None when a model the run used has no price.

Example

from criogenai import Agent, FakeModel, estimate_cost

result = Agent(FakeModel(["Hello."])).run_sync("Hi")
cost = estimate_cost(result, {"fake": {"input": 0.25, "output": 2.0}})
print(f"{cost:.8f} USD")