summaryrefslogtreecommitdiffhomepage
path: root/lib/dispatch/adapter/claude/pricing_table.rb
blob: 7486e788054de8f7c1fac7954fa3b3e14962432c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true

require "json"

module Dispatch
  module Adapter
    class Claude < Base
      module PricingTable
        DATA_PATH = File.expand_path("data/claude_pricing.json", __dir__)

        # Raw Hash loaded once from the bundled JSON file.
        TABLE = begin
          raw = JSON.parse(File.read(DATA_PATH))
          raw.freeze
        end

        module_function

        # Returns a ModelPricing for the given model id, or nil if unknown.
        #
        # @param model_id [String]
        # @return [Dispatch::Adapter::ModelPricing, nil]
        def lookup(model_id)
          entry = TABLE[model_id]
          return nil unless entry

          ModelPricing.new(
            input_per_mtok: entry["input_per_mtok"],
            output_per_mtok: entry["output_per_mtok"],
            cache_read_per_mtok: entry["cache_read_per_mtok"],
            cache_write_per_mtok: entry["cache_write_per_mtok"]
          )
        end

        # Returns the context window size for the given model id, or nil.
        #
        # @param model_id [String]
        # @return [Integer, nil]
        def context_window(model_id)
          TABLE.dig(model_id, "context_window")
        end

        # Returns the max output token count for the given model id, or nil.
        #
        # @param model_id [String]
        # @return [Integer, nil]
        def max_output_tokens(model_id)
          TABLE.dig(model_id, "max_output_tokens")
        end

        # Returns all known model id strings.
        #
        # @return [Array<String>]
        def known_ids
          TABLE.keys
        end
      end
    end
  end
end