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



50
51
52
53
54
55
56
57
58
59
# File 'lib/ruby_llm/providers/anthropic/models.rb', line 50

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



46
47
48
# File 'lib/ruby_llm/providers/anthropic/models.rb', line 46

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



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

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

.extract_model_id(data) ⇒ Object



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

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

.extract_output_tokens(data) ⇒ Object



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

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

.models_urlObject



10
11
12
# File 'lib/ruby_llm/providers/anthropic/models.rb', line 10

def models_url
  '/v1/models'
end

.parse_list_models_response(response, slug, capabilities) ⇒ Object



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

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'],
      provider: slug,
      family: capabilities.model_family(model_id),
      created_at: Time.parse(model_data['created_at']),
      context_window: capabilities.determine_context_window(model_id),
      max_output_tokens: capabilities.determine_max_tokens(model_id),
      modalities: capabilities.modalities_for(model_id),
      capabilities: capabilities.capabilities_for(model_id),
      pricing: capabilities.pricing_for(model_id),
      metadata: {}
    )
  end
end