Class: Mistri::Providers::Anthropic

Inherits:
Object
  • Object
show all
Defined in:
lib/mistri/providers/anthropic.rb,
lib/mistri/providers/anthropic/assembler.rb,
lib/mistri/providers/anthropic/serializer.rb

Overview

The Anthropic Messages API, streamed. Defaults target the current model generation: adaptive thinking with summarized display (so thinking streams for the UI), prompt caching on, 32k output headroom.

Provider failures fold into the stream as an error turn rather than raising: the loop decides whether to retry, and the host always gets a message back.

Defined Under Namespace

Modules: Serializer Classes: Assembler

Constant Summary collapse

VERSION_HEADER =
"2023-06-01"
DEFAULT_THINKING =
{ type: "adaptive", display: "summarized" }.freeze
PASSTHROUGH =

Messages API parameters passed through verbatim from a stream override.

%i[temperature top_p top_k stop_sequences metadata
tool_choice service_tier].freeze
UNKNOWN_MODEL_MAX_TOKENS =

The ceiling for an uncatalogued model: high enough for headroom, low enough that every current model accepts it. Catalog a model to unlock its real output limit.

64_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, model: "claude-opus-4-8", origin: "https://api.anthropic.com", max_tokens: nil, thinking: DEFAULT_THINKING, cache: true, **transport_options) ⇒ Anthropic

Returns a new instance of Anthropic.



24
25
26
27
28
29
30
31
32
33
# File 'lib/mistri/providers/anthropic.rb', line 24

def initialize(api_key:, model: "claude-opus-4-8", origin: "https://api.anthropic.com",
               max_tokens: nil, thinking: DEFAULT_THINKING, cache: true,
               **transport_options)
  @api_key = api_key
  @model = model
  @max_tokens = max_tokens
  @thinking = thinking
  @cache = cache
  @transport = Transport.new(origin: origin, **transport_options)
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



35
36
37
# File 'lib/mistri/providers/anthropic.rb', line 35

def model
  @model
end

Instance Method Details

#closeObject



52
# File 'lib/mistri/providers/anthropic.rb', line 52

def close = @transport.close

#stream(messages:, system: nil, tools: [], signal: nil, **overrides, &emit) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mistri/providers/anthropic.rb', line 37

def stream(messages:, system: nil, tools: [], signal: nil, **overrides, &emit)
  model = overrides.fetch(:model, @model)
  assembler = Anthropic::Assembler.new(model: model)
  body = build_body(model, messages, system, tools, overrides)
  outcome = @transport.stream_post("/v1/messages", body: body, headers: headers,
                                                   signal: signal) do |record|
    assembler.feed(
      record, &emit
    )
  end
  outcome == :aborted ? assembler.abort(&emit) : assembler.finish(&emit)
rescue Error => e
  assembler.fail_stream(e, &emit)
end