Skip to content

Index

ipw.cost

Cost tracking and pricing for LLM API providers.

calculate_cost(provider, model, input_tokens, output_tokens)

Calculate API cost in USD for a given provider, model, and token usage.

Parameters:

Name Type Description Default
provider str

Provider name (openai, anthropic, gemini, google).

required
model str

Model identifier as used in the provider's API.

required
input_tokens int

Number of input / prompt tokens.

required
output_tokens int

Number of output / completion tokens.

required

Returns:

Type Description
float

Estimated cost in USD. Returns 0.0 if the provider or model is

float

not found in the pricing tables.

Source code in intelligence-per-watt/src/ipw/cost/pricing.py
def calculate_cost(
    provider: str,
    model: str,
    input_tokens: int,
    output_tokens: int,
) -> float:
    """Calculate API cost in USD for a given provider, model, and token usage.

    Args:
        provider: Provider name (openai, anthropic, gemini, google).
        model: Model identifier as used in the provider's API.
        input_tokens: Number of input / prompt tokens.
        output_tokens: Number of output / completion tokens.

    Returns:
        Estimated cost in USD.  Returns 0.0 if the provider or model is
        not found in the pricing tables.
    """
    pricing_table = PROVIDER_PRICING.get(provider.lower())
    if pricing_table is None:
        return 0.0

    model_pricing = pricing_table.get(model)
    if model_pricing is None:
        return 0.0

    input_cost = (input_tokens / 1_000_000) * model_pricing["input"]
    output_cost = (output_tokens / 1_000_000) * model_pricing["output"]
    return input_cost + output_cost