Module: RubyLLM::Contract::CostCalculator

Defined in:
lib/ruby_llm/contract/cost_calculator.rb

Defined Under Namespace

Classes: RegisteredModel

Class Method Summary collapse

Class Method Details

.calculate(model_name:, usage:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_llm/contract/cost_calculator.rb', line 36

def self.calculate(model_name:, usage:)
  return nil unless model_name && usage.is_a?(Hash)

  model_info = find_model(model_name)
  return nil unless model_info

  compute_cost(model_info, usage)
rescue StandardError
  nil
end

.register_model(model_name, input_per_1m:, output_per_1m:) ⇒ Object

Register pricing for custom or fine-tuned models not in the RubyLLM registry.

CostCalculator.register_model("ft:gpt-4o-custom",
  input_per_1m: 3.0, output_per_1m: 6.0)


16
17
18
19
20
21
22
23
24
# File 'lib/ruby_llm/contract/cost_calculator.rb', line 16

def self.register_model(model_name, input_per_1m:, output_per_1m:)
  validate_price!(:input_per_1m, input_per_1m)
  validate_price!(:output_per_1m, output_per_1m)

  @custom_models[model_name] = RegisteredModel.new(
    input_price_per_million: input_per_1m,
    output_price_per_million: output_per_1m
  )
end

.reset_custom_models!Object

Reset all custom model registrations. Mainly useful in tests.



32
33
34
# File 'lib/ruby_llm/contract/cost_calculator.rb', line 32

def self.reset_custom_models!
  @custom_models.clear
end

.unregister_model(model_name) ⇒ Object

Remove a previously registered custom model. Mainly useful in tests.



27
28
29
# File 'lib/ruby_llm/contract/cost_calculator.rb', line 27

def self.unregister_model(model_name)
  @custom_models.delete(model_name)
end