Module: Braintrust::Contrib::Anthropic::Instrumentation::Messages::InstanceMethods

Defined in:
lib/braintrust/contrib/anthropic/instrumentation/messages.rb

Constant Summary collapse

METADATA_FIELDS =
%i[
  model max_tokens temperature top_p top_k stop_sequences
  stream tools tool_choice thinking metadata service_tier
].freeze

Instance Method Summary collapse

Instance Method Details

#create(**params) ⇒ Object

Wrap synchronous messages.create



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/braintrust/contrib/anthropic/instrumentation/messages.rb', line 31

def create(**params)
  client = instance_variable_get(:@client)
  tracer = Braintrust::Contrib.tracer_for(client)

  tracer.in_span("anthropic.messages.create") do |span|
     = (params)
    set_input(span, params)

    response = nil
    time_to_first_token = Braintrust::Internal::Time.measure do
      response = super(**params)
    end

    set_output(span, response)
    set_metrics(span, response, time_to_first_token)
    (span, , response)

    response
  end
end

#stream(**params) ⇒ Object

Wrap streaming messages.stream Stores context on stream object for span creation during consumption



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/braintrust/contrib/anthropic/instrumentation/messages.rb', line 54

def stream(**params)
  client = instance_variable_get(:@client)
  tracer = Braintrust::Contrib.tracer_for(client)
   = (params, stream: true)

  stream_obj = super
  Braintrust::Contrib::Context.set!(stream_obj,
    tracer: tracer,
    params: params,
    metadata: ,
    messages_instance: self,
    start_time: Braintrust::Internal::Time.measure)
  stream_obj
end