Module: RubyLLM::Providers::Anthropic::Models

Included in:
RubyLLM::Providers::Anthropic
Defined in:
lib/ruby_llm/providers/anthropic/models.rb

Overview

Models methods of the Anthropic API integration

Class Method Summary collapse

Class Method Details

.extract_cache_creation_tokens(data) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby_llm/providers/anthropic/models.rb', line 46

def extract_cache_creation_tokens(data)
  direct = data.dig('message', 'usage',
                    'cache_creation_input_tokens') || data.dig('usage', 'cache_creation_input_tokens')
  return direct if direct

  breakdown = data.dig('message', 'usage', 'cache_creation') || data.dig('usage', 'cache_creation')
  return unless breakdown.is_a?(Hash)

  breakdown.values.compact.sum
end

.extract_cached_tokens(data) ⇒ Object



42
43
44
# File 'lib/ruby_llm/providers/anthropic/models.rb', line 42

def extract_cached_tokens(data)
  data.dig('message', 'usage', 'cache_read_input_tokens') || data.dig('usage', 'cache_read_input_tokens')
end

.extract_input_tokens(data) ⇒ Object



34
35
36
# File 'lib/ruby_llm/providers/anthropic/models.rb', line 34

def extract_input_tokens(data)
  data.dig('message', 'usage', 'input_tokens')
end

.extract_model_id(data) ⇒ Object



30
31
32
# File 'lib/ruby_llm/providers/anthropic/models.rb', line 30

def extract_model_id(data)
  data.dig('message', 'model')
end

.extract_output_tokens(data) ⇒ Object



38
39
40
# File 'lib/ruby_llm/providers/anthropic/models.rb', line 38

def extract_output_tokens(data)
  data.dig('message', 'usage', 'output_tokens') || data.dig('usage', 'output_tokens')
end

.models_urlObject



12
13
14
# File 'lib/ruby_llm/providers/anthropic/models.rb', line 12

def models_url
  'v1/models'
end

.parse_list_models_response(response, slug, _capabilities) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_llm/providers/anthropic/models.rb', line 16

def parse_list_models_response(response, slug, _capabilities)
  Array(response.body['data']).map do |model_data|
    model_id = model_data['id']

    Model::Info.new(
      id: model_id,
      name: model_data['display_name'] || model_id,
      provider: slug,
      created_at: Time.parse(model_data['created_at']),
      metadata: {}
    )
  end
end