Class: Llmemory::LLM::Anthropic

Inherits:
Base
  • Object
show all
Defined in:
lib/llmemory/llm/anthropic.rb

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.anthropic.com"
DEFAULT_MODEL =
"claude-sonnet-4-6"

Instance Attribute Summary

Attributes inherited from Base

#last_usage

Instance Method Summary collapse

Methods inherited from Base

#invoke_with_json_schema

Constructor Details

#initialize(api_key: nil, model: nil, base_url: nil) ⇒ Anthropic

Returns a new instance of Anthropic.



13
14
15
16
17
18
# File 'lib/llmemory/llm/anthropic.rb', line 13

def initialize(api_key: nil, model: nil, base_url: nil)
  super()
  @api_key = api_key || config.llm_api_key || ENV["ANTHROPIC_API_KEY"]
  @model = model || config.llm_model || DEFAULT_MODEL
  @base_url = base_url || config.llm_base_url || DEFAULT_BASE_URL
end

Instance Method Details

#invoke(prompt) ⇒ Object



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
# File 'lib/llmemory/llm/anthropic.rb', line 20

def invoke(prompt)
  result = nil
  payload = { provider: :anthropic, model: @model, prompt_chars: prompt.to_s.length }
  Llmemory::Instrumentation.instrument(:llm_invoke, payload) do
    response = connection.post("v1/messages") do |req|
      req.body = {
        model: @model,
        max_tokens: 1024,
        messages: [{ role: "user", content: prompt }]
      }.to_json
      req.headers["Content-Type"] = "application/json"
      req.headers["x-api-key"] = @api_key
      req.headers["anthropic-version"] = "2023-06-01"
    end

    raise Llmemory::LLMError, "Anthropic API error: #{response.body}" unless response.success?

    body = response.body.is_a?(Hash) ? response.body : JSON.parse(response.body.to_s)
    content = body.dig("content", 0, "text")&.strip || ""
    usage = parse_anthropic_usage(body["usage"])
    record_usage(usage)
    payload.merge!(instrumentation_payload(usage, content))
    result = Response.new(content, usage: usage)
  end
  result
end