Module: TokenHawk::Cost

Defined in:
lib/token_hawk/cost.rb

Class Method Summary collapse

Class Method Details

.calculate(model:, input_tokens:, output_tokens:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/token_hawk/cost.rb', line 5

def self.calculate(model:, input_tokens:, output_tokens:)
  rates = Pricing.for_model(model)

  unless rates
    warn "[TokenHawk] Looks like you may need to add an entry for #{model.inspect} — " \
         "cost recorded as 0 and vendor set to \"unknown\". " \
         "In your initializer: config.pricing[#{model.inspect}] = { vendor: \"anthropic\", input: 0.0, output: 0.0 }"
    return 0
  end

  ((input_tokens * rates[:input] + output_tokens * rates[:output]) / 1000.0).round
end