Skip to main content

Overview

The pricing module provides centralized cost calculation for both OpenAI token-based pricing and HuggingFace endpoint hourly pricing. It loads pricing configuration from JSON and resolves total costs using provider-reported costs when available, falling back to estimated calculations.

resolve_total_cost()

Resolve total cost with callback-first and provider-specific fallbacks.

Signature

Parameters

str
required
Provider type (“openai” or “huggingface”)
str
required
Short model name/key (e.g., “gpt-5”, “mediphi”)
str
required
Full model identifier (e.g., “gpt-5”, “microsoft/MediPhi-Instruct”)
int
required
Number of input/prompt tokens consumed
int
required
Number of output/completion tokens generated
Optional[float]
required
Cost reported directly by provider API (if available)
str
required
Source of provider-reported cost for traceability
Optional[float]
Execution time in seconds (used for HuggingFace endpoint pricing)

Returns

Dict[str, Any]
Dictionary containing:
  • total_cost (float): Total cost in USD, rounded to 10 decimal places
  • cost_source (str): Source of cost calculation
  • pricing_context (dict): Detailed pricing metadata

Cost Resolution Priority

  1. Provider-reported cost: If provider_reported_cost is provided and > 0, uses it directly
  2. OpenAI token-based estimation: For “openai” provider, calculates from token counts and rates
  3. HuggingFace endpoint estimation: For “huggingface” provider, calculates from execution time and hourly rates
  4. Missing: Returns 0.0 cost if no pricing information available

Cost Sources

  • provider_reported: Direct cost from provider API
  • estimated_openai_token_pricing: Calculated from OpenAI token rates
  • estimated_hf_endpoint_pricing: Calculated from HuggingFace endpoint hourly rates
  • missing: No pricing information available

Example

OpenAI Token-Based Pricing

Calculation Formula

Pricing Context (Token-Based)

str
Always “token_based” for OpenAI models
float
Input token rate per 1 million tokens (USD)
float
Output token rate per 1 million tokens (USD)
int
Number of input tokens consumed
int
Number of output tokens generated
float
Cost of input tokens (USD, rounded to 10 decimals)
float
Cost of output tokens (USD, rounded to 10 decimals)

Example Configuration (pricing.json)

HuggingFace Endpoint Pricing

Allocation Modes

Two allocation modes are supported:
  1. runtime_proportional (default): Cost based on actual execution time
  2. amortized_window: Cost amortized over a time window and query count

Runtime Proportional Calculation

Amortized Window Calculation

Pricing Context (Endpoint Hourly)

str
Always “endpoint_hourly” for HuggingFace models
str
Cloud provider (e.g., “aws”, “gcp”, “azure”)
str
Instance family (e.g., “p4d”, “g5”)
str
Instance size (e.g., “24xlarge”, “12xlarge”)
str
GPU accelerator type (e.g., “A100”, “A10G”)
int
Number of GPUs per instance
float
GPU VRAM in GB per GPU
float
Hourly rate in USD per replica
int
Number of endpoint replicas
str
Either “runtime_proportional” or “amortized_window”
float
Execution time in seconds (runtime_proportional mode only)
float
Total active hours in window (amortized_window mode only)
int
Total queries processed in window (amortized_window mode only)
str
URL to pricing documentation
str
Date when pricing was last updated

Example Configuration (pricing.json)

get_pricing_config_summary()

Generate a summary of pricing configuration for documentation and traceability.

Signature

Returns

Dict[str, Any]
Dictionary containing:
  • openai_models: Dict of OpenAI model pricing configurations
  • huggingface_endpoints: Dict of HuggingFace endpoint configurations

Example

load_pricing_config()

Load pricing configuration from JSON file with safe defaults.

Signature

Returns

Dict[str, Any]
Pricing configuration dictionary, or empty dict if file not found or invalid

Configuration File Location

  1. If PRICING_CONFIG_PATH environment variable is set, uses that path
  2. Otherwise uses default: {PROJECT_ROOT}/config/pricing.json

Error Handling

  • Returns empty dict {} if file doesn’t exist
  • Returns empty dict {} if file has invalid JSON
  • Never raises exceptions - always returns a valid dict

Configuration File Structure

Complete Example

Usage Example

Complete Cost Tracking Workflow