Class: SkillBench::Services::CostCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/skill_bench/services/cost_calculator.rb

Overview

Estimates the USD cost of an LLM run from token usage and a model name.

Prices are approximate, drawn from public OpenAI/Anthropic pricing pages, and expressed in USD per 1,000 tokens. Provider pricing changes over time, so treat the result as a rough estimate and extend PRICES as needed.

Constant Summary collapse

PRICES =

Approximate per-model prices in USD per 1,000 tokens. Keyed by a canonical model prefix; longer prefixes win on lookup so that dated variants (e.g. "claude-sonnet-4-20250514") resolve correctly. Source: public OpenAI and Anthropic pricing pages (approximate).

{
  'gpt-4o-mini' => { input: 0.00015, output: 0.0006 },
  'gpt-4o' => { input: 0.005, output: 0.015 },
  'gpt-4-turbo' => { input: 0.01, output: 0.03 },
  'gpt-4' => { input: 0.03, output: 0.06 },
  'gpt-3.5-turbo' => { input: 0.0005, output: 0.0015 },
  'claude-opus-4' => { input: 0.015, output: 0.075 },
  'claude-sonnet-4' => { input: 0.003, output: 0.015 },
  'claude-3-5-sonnet' => { input: 0.003, output: 0.015 },
  'claude-3-5-haiku' => { input: 0.0008, output: 0.004 },
  'claude-3-opus' => { input: 0.015, output: 0.075 },
  'claude-3-sonnet' => { input: 0.003, output: 0.015 },
  'claude-3-haiku' => { input: 0.00025, output: 0.00125 }
}.freeze
TOKENS_PER_UNIT =

Token count that one priced unit of PRICES covers.

1000.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(usage, model) ⇒ CostCalculator

Returns a new instance of CostCalculator.

Parameters:

  • usage (Hash, nil)

    Token usage hash.

  • model (String, nil)

    The model name.



44
45
46
47
# File 'lib/skill_bench/services/cost_calculator.rb', line 44

def initialize(usage, model)
  @usage = usage || {}
  @model = model
end

Class Method Details

.call(usage:, model:) ⇒ Float?

Estimates the USD cost for a run.

Parameters:

  • usage (Hash, nil)

    Token usage with :prompt_tokens and :completion_tokens.

  • model (String, nil)

    The model name (e.g. "gpt-4o").

Returns:

  • (Float, nil)

    Estimated cost in USD, or nil when the model is unknown.



38
39
40
# File 'lib/skill_bench/services/cost_calculator.rb', line 38

def self.call(usage:, model:)
  new(usage, model).call
end

Instance Method Details

#callFloat?

Estimates the USD cost for the configured usage and model.

Returns:

  • (Float, nil)

    Estimated cost in USD, or nil when the model is unknown.



52
53
54
55
56
57
58
59
# File 'lib/skill_bench/services/cost_calculator.rb', line 52

def call
  price = price_for(@model)
  return nil unless price

  input_cost = units(:prompt_tokens) * price[:input]
  output_cost = units(:completion_tokens) * price[:output]
  (input_cost + output_cost).round(6)
end