CostEstimator

org.llm4s.llmconnect.provider.CostEstimator
object CostEstimator

Centralized cost estimation for LLM completions.

This provides a single source of truth for estimating completion costs based on token usage and model pricing information. It integrates with the ModelRegistry to look up pricing data and applies it to usage statistics.

The estimator:

  • Uses existing ModelPricing logic (no duplication)
  • Returns None if pricing is unavailable
  • Preserves precision of micro-cost values
  • Works uniformly across all providers

Example usage:

 val usage = TokenUsage(promptTokens = 100, completionTokens = 50, totalTokens = 150)
 val cost = CostEstimator.estimate("gpt-4o", usage)
 // cost: Some(0.0015) for gpt-4o pricing

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def estimate(model: String, usage: TokenUsage): Option[Double]

Estimate the cost of a completion based on model and token usage.

Estimate the cost of a completion based on model and token usage.

This method looks up the model's pricing information and applies it to the provided usage statistics. It handles:

  • Standard prompt and completion tokens
  • Thinking tokens (billed at completion token rate)
  • Missing pricing data (returns None)

Value parameters

model

The model identifier (e.g., "gpt-4o", "claude-3-7-sonnet-latest")

usage

Token usage statistics from the completion

Attributes

Returns

Estimated cost in USD, or None if pricing is unavailable

def estimateFromMetadata(metadata: Option[ModelMetadata], usage: TokenUsage): Option[Double]

Estimate cost using pre-fetched model metadata.

Estimate cost using pre-fetched model metadata.

This is useful when the caller already has ModelMetadata (e.g., from a provider implementation that caches metadata lookups).

Value parameters

metadata

Model metadata containing pricing information

usage

Token usage statistics

Attributes

Returns

Estimated cost in USD, or None if pricing is unavailable