diff options
Diffstat (limited to 'lib/dispatch/adapter/interface/pricing.rb')
| -rw-r--r-- | lib/dispatch/adapter/interface/pricing.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/dispatch/adapter/interface/pricing.rb b/lib/dispatch/adapter/interface/pricing.rb new file mode 100644 index 0000000..a3ec9eb --- /dev/null +++ b/lib/dispatch/adapter/interface/pricing.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true + +module Dispatch + module Adapter + module Pricing + module_function + + # Calculates UsageCost from a Usage and ModelInfo. + # + # NOTE: Reasoning tokens are NOT separately priced here. + # Anthropic bills them as output tokens; OpenAI o-series likewise. + # It is assumed that output_tokens includes reasoning_tokens if applicable. + def calculate(usage, model_info) + return nil unless model_info&.pricing + + p = model_info.pricing + mtok = ->(tokens, rate) { (rate.to_f / 1_000_000.0) * tokens.to_i } + + input = mtok.call(usage.input_tokens, p.input_per_mtok) + output = mtok.call(usage.output_tokens, p.output_per_mtok) + cread = mtok.call(usage.cache_read_tokens, p.cache_read_per_mtok) + cwrite = mtok.call(usage.cache_creation_tokens, p.cache_write_per_mtok) + + UsageCost.new( + input: input, + output: output, + cache_read: cread, + cache_write: cwrite, + total: input + output + cread + cwrite + ) + end + end + end +end |
