summaryrefslogtreecommitdiffhomepage
path: root/lib/dispatch/adapter/interface/pricing.rb
diff options
context:
space:
mode:
authorAdam Malczewski <[email protected]>2026-04-29 21:40:58 +0900
committerAdam Malczewski <[email protected]>2026-04-29 21:40:58 +0900
commit27af03cb3540539f065334c199fdb42c48776fc5 (patch)
tree9dcaecc59f4383d88933519b5b049793e772427b /lib/dispatch/adapter/interface/pricing.rb
parent07277435c0688ad9f5fa682633b86b99ef5bb854 (diff)
downloaddispatch-adapter-interface-27af03cb3540539f065334c199fdb42c48776fc5.tar.gz
dispatch-adapter-interface-27af03cb3540539f065334c199fdb42c48776fc5.zip
update to support claude
Diffstat (limited to 'lib/dispatch/adapter/interface/pricing.rb')
-rw-r--r--lib/dispatch/adapter/interface/pricing.rb34
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