Module: Anthropic::Helpers::Tracing Private
- Defined in:
- lib/anthropic/helpers/tracing.rb,
lib/anthropic/helpers/tracing/config.rb,
lib/anthropic/helpers/tracing/content.rb,
lib/anthropic/helpers/tracing/response.rb,
lib/anthropic/helpers/tracing/recorders.rb,
lib/anthropic/helpers/tracing/attributes.rb,
lib/anthropic/helpers/tracing/tool_spans.rb,
lib/anthropic/helpers/tracing/streamed_message.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
The span attributes that Internal::Tracing cannot take from its tables: the
computed ones, the Message that a stream carries, and the content of requests and responses.
The tool runner's two INTERNAL spans start and end here too.
Defined Under Namespace
Modules: Recorders Classes: StreamedMessage
Constant Summary collapse
- CONTENT_LIMIT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The most bytes the
contentattributes of one span hold, unless the client'smax_content_bytessets another bound. Anthropic::Internal::Tracing::DEFAULT_MAX_CONTENT_BYTES
- GEN_AI_CONVERSATION_ID =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"gen_ai.conversation.id"- GEN_AI_INPUT_MESSAGES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"gen_ai.input.messages"- GEN_AI_OUTPUT_MESSAGES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"gen_ai.output.messages"- GEN_AI_OUTPUT_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"gen_ai.output.type"- GEN_AI_REQUEST_STOP_SEQUENCES =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"gen_ai.request.stop_sequences"- GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"gen_ai.response.time_to_first_chunk"- GEN_AI_SYSTEM_INSTRUCTIONS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"gen_ai.system_instructions"- GEN_AI_TOOL_CALL_ARGUMENTS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"gen_ai.tool.call.arguments"- GEN_AI_TOOL_CALL_ID =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"gen_ai.tool.call.id"- GEN_AI_TOOL_DESCRIPTION =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"gen_ai.tool.description"- GEN_AI_TOOL_NAME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"gen_ai.tool.name"- GEN_AI_TOOL_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"gen_ai.tool.type"- GEN_AI_USAGE_INPUT_TOKENS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"gen_ai.usage.input_tokens"- GEN_AI_OPERATION_NAME_VALUE_EXECUTE_TOOL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"execute_tool"- GEN_AI_OPERATION_NAME_VALUE_INVOKE_AGENT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"invoke_agent"- GEN_AI_TOOL_TYPE_VALUE_FUNCTION =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"function"- GEN_AI_OUTPUT_TYPE_VALUE_JSON =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"json"- ANTHROPIC_CONTENT_TRUNCATED =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"anthropic.content.truncated"- ANTHROPIC_MESSAGE_BATCH_REQUEST_COUNT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"anthropic.message.batch.request_count"- ANTHROPIC_SESSION_THREAD_ID =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"anthropic.session.thread.id"- ANTHROPIC_TOOLSET_NAME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"anthropic.toolset.name"- ANTHROPIC_TOOL_RESULT_CONTENT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"anthropic.tool_result.content"- ANTHROPIC_USAGE_COMPACTION_INPUT_TOKENS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"anthropic.usage.compaction.input_tokens"- ANTHROPIC_USAGE_COMPACTION_OUTPUT_TOKENS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"anthropic.usage.compaction.output_tokens"- GEN_AI_AGENT_ID =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"gen_ai.agent.id"
Class Method Summary collapse
-
.accumulate_streamed_message(stream) ⇒ Anthropic::Models::Message, Anthropic::Models::Beta::BetaMessage
private
Runs the block, in which a stream helper folds one event into the Message it accumulates, and gives that Message to the span that follows
stream, when one does. - .block_type(block) ⇒ String private
- .conversation_id(options) ⇒ String? private
-
.end_tool_runner_span(span, returned:, error:) ⇒ void
private
The status is
ERRORforerror,OKfor a loop that returned, and unset when the application stopped the loop. - .end_tool_use_span(client, span, input, result) ⇒ void private
-
.follow_streamed_message(span, events, params:, start_time:, content:, content_limit: CONTENT_LIMIT) ⇒ Array(Enumerator, Anthropic::Helpers::Tracing::StreamedMessage)
private
The enumerator that reads
eventsand records the Message they carry onspan, and what it has recorded so far. -
.json_output_format?(params) ⇒ Boolean
private
A tool runner's params are the application's own, so the format can still be a model, or a model class that the structured output helper sends as a
json_schemaformat. - .messages(messages) ⇒ Array<Hash{Symbol=>Object}> private
- .part(block) ⇒ Hash{Symbol=>Object} private
- .parts(content) ⇒ Array<Hash{Symbol=>Object}> private
-
.recording(what) ⇒ void
private
A failure to record must not fail the application's code, so an error raised in the block is logged, and the value is then
nil. -
.result_block_content(content) ⇒ Array<Object>, Hash{Symbol=>Object}
private
The
contentof a result block other thantool_result, such asweb_search_tool_result. -
.result_value(value) ⇒ Object
private
valuewithout itsencrypted_*fields, which are opaque, and without image and document data, which is large and binary. - .set_computed_usage_attributes(span, usage) ⇒ void private
-
.set_content_attributes(span, input, others, stop_sequences: nil, limit: CONTENT_LIMIT) ⇒ void
private
Sets each value as one JSON string, and
stop_sequencesas it is. - .set_message_content_attributes(span, body, output, limit: CONTENT_LIMIT) ⇒ void private
-
.start_tool_runner_span(client, params) ⇒ OpenTelemetry::Trace::Span?
private
Returns
nilwhen the client's spans are disabled, or when the span cannot start. -
.start_tool_use_span(client, tool, tool_use, request_options) ⇒ OpenTelemetry::Trace::Span?
private
Returns
nilwhen the client's spans are disabled, or when the span cannot start. -
.symbolize_keys(value) ⇒ Object
private
A deep copy of
valuewhose Hashes have Symbol keys, and whose models are such Hashes. -
.text_array(array) ⇒ Array<String>?
private
nilwhen any item is not text (Tracing.utf8_text). -
.tool_description(tool) ⇒ String?
private
nilfor no description, an empty one, or one that is not text (Tracing.utf8_text): it can come from an MCP server. -
.tool_result_content(content) ⇒ Array<Object>
private
The content blocks of a
tool_resultblock. -
.truncate_parts(lists, excess) ⇒ Boolean
private
Removes parts from
lists, in place, from the first on, untilexcessfewer bytes of JSON remain. -
.utf8_text(string) ⇒ String?
private
nilwhenstringis not text in its encoding: an exporter fails on a string that is not valid UTF-8, which would lose every span it exports with it.
Class Method Details
.accumulate_streamed_message(stream) ⇒ Anthropic::Models::Message, Anthropic::Models::Beta::BetaMessage
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Runs the block, in which a stream helper folds one event into the Message it accumulates,
and gives that Message to the span that follows stream, when one does. An event that the
block fails on leaves the span without the Message.
71 72 73 74 75 76 77 |
# File 'lib/anthropic/helpers/tracing/streamed_message.rb', line 71 def (stream) tracker = stream. if stream.is_a?(Anthropic::Internal::TracedStream) tracker&.snapshot = nil = yield tracker&.snapshot = end |
.block_type(block) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
146 147 148 149 150 151 152 153 |
# File 'lib/anthropic/helpers/tracing/content.rb', line 146 def block_type(block) case block[:type] in String | Symbol => type type.to_s else "unknown" end end |
.conversation_id(options) ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
10 11 12 |
# File 'lib/anthropic/helpers/tracing/config.rb', line 10 def conversation_id() Anthropic::Internal::Tracing.request_option(.to_h[:open_telemetry])[:conversation_id]&.to_s end |
.end_tool_runner_span(span, returned:, error:) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The status is ERROR for error, OK for a loop that returned, and unset when the
application stopped the loop.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/anthropic/helpers/tracing/tool_spans.rb', line 39 def end_tool_runner_span(span, returned:, error:) recording("the tool runner") do if error Anthropic::Internal::Tracing.set_error_status(span, error) elsif returned span.status = OpenTelemetry::Trace::Status.ok end span.finish end end |
.end_tool_use_span(client, span, input, result) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/anthropic/helpers/tracing/tool_spans.rb', line 111 def end_tool_use_span(client, span, input, result) recording("the tool run") do next unless span.recording? && client.open_telemetry_content? set_content_attributes( span, nil, { GEN_AI_TOOL_CALL_ARGUMENTS => input, ANTHROPIC_TOOL_RESULT_CONTENT => (tool_result_content(result[:content]) unless result.nil?) }, limit: client.open_telemetry_max_content_bytes ) end recording("the tool run") do if result&.[](:is_error) span.status = OpenTelemetry::Trace::Status.error span.set_attribute(Anthropic::Internal::Tracing::ERROR_TYPE, "tool_error") elsif result span.status = OpenTelemetry::Trace::Status.ok end span.finish end end |
.follow_streamed_message(span, events, params:, start_time:, content:, content_limit: CONTENT_LIMIT) ⇒ Array(Enumerator, Anthropic::Helpers::Tracing::StreamedMessage)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The enumerator that reads events and records the Message they carry on span, and what
it has recorded so far.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/anthropic/helpers/tracing/streamed_message.rb', line 29 def ( span, events, params:, start_time:, content:, content_limit: CONTENT_LIMIT ) tracker = StreamedMessage.new( span, params: params, start_time: start_time, content: content, content_limit: content_limit ) recording = Enumerator.new do |y| events.each do |event| tracker.observe(event) y << event end tracker.ended_by_server ensure tracker.set_attributes end # The SDK closes a stream by rewinding its enumerator. That does not resume a read # suspended in `Enumerator#next`, whose `ensure` would never run, so the Message is # recorded here too. recording.define_singleton_method(:rewind) do tracker.set_attributes events.rewind self end [recording, tracker] end |
.json_output_format?(params) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A tool runner's params are the application's own, so the format can still be a model, or a
model class that the structured output helper sends as a json_schema format.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/anthropic/helpers/tracing/recorders.rb', line 13 def json_output_format?(params) case params[:output_format] || params[:output_config]&.[](:format) in nil false in Anthropic::JSONOutputFormat | Anthropic::Beta::BetaJSONOutputFormat | Anthropic::Helpers::InputSchema::JsonSchemaConverter | {schema: Anthropic::Helpers::InputSchema::JsonSchemaConverter} true in format symbolize_keys(format)[:type].to_s == "json_schema" end end |
.messages(messages) ⇒ Array<Hash{Symbol=>Object}>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
197 198 199 |
# File 'lib/anthropic/helpers/tracing/content.rb', line 197 def () Array().map { {role: _1[:role].to_s, parts: parts(_1[:content])} } end |
.part(block) ⇒ Hash{Symbol=>Object}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/anthropic/helpers/tracing/content.rb', line 158 def part(block) case block_type(block) in "text" {type: "text", content: block[:text].to_s} in "thinking" {type: "reasoning", content: block[:thinking].to_s} in "tool_use" | "server_tool_use" => type call = {type: "tool_call", id: block[:id], name: block[:name], arguments: block[:input]} # Only the `tool_use` of a toolset member has the name, and a `nil` one is left out. toolset_name = block[:toolset_name] if type == "tool_use" toolset_name.is_a?(String) ? {**call, anthropic_toolset_name: toolset_name} : call in "mcp_tool_use" { type: "tool_call", id: block[:id], name: block[:name], arguments: block[:input], anthropic_mcp_server_name: block[:server_name] } in "tool_result" { type: "tool_call_response", id: block[:tool_use_id], response: tool_result_blocks(block[:content]) } in /_tool_result\z/ { type: "tool_call_response", id: block[:tool_use_id], response: result_block_content(block[:content]) } in type {type: "anthropic_#{type}"} end end |
.parts(content) ⇒ Array<Hash{Symbol=>Object}>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/anthropic/helpers/tracing/content.rb', line 62 def parts(content) case content in String [{type: "text", content: content}] in Array content.map { part(_1) } else [] end end |
.recording(what) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A failure to record must not fail the application's code, so an error raised in the block
is logged, and the value is then nil.
11 12 13 14 15 16 |
# File 'lib/anthropic/helpers/tracing/streamed_message.rb', line 11 def recording(what) yield rescue StandardError => e OpenTelemetry.logger.debug("anthropic: could not record #{what} on its span: #{e.class}") nil end |
.result_block_content(content) ⇒ Array<Object>, Hash{Symbol=>Object}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The content of a result block other than tool_result, such as web_search_tool_result.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/anthropic/helpers/tracing/content.rb', line 104 def result_block_content(content) case content in nil [] in String [{type: "text", text: content}] in Array content.map do type = block_type(_1) %w[text search_result web_search_result].include?(type) ? result_value(_1) : {type: type} end else result_value(content) end end |
.result_value(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
value without its encrypted_* fields, which are opaque, and without image and document
data, which is large and binary.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/anthropic/helpers/tracing/content.rb', line 126 def result_value(value) case value in Hash | Anthropic::Internal::Type::BaseModel type = block_type(value) return {type: type} if %w[image document].include?(type) fields = value.to_h.reject { |name, _| name.to_s.start_with?("encrypted_") } view = type == "text_editor_code_execution_view_result" fields = fields.except(:content) if view && %w[image pdf].include?(fields[:file_type].to_s) fields.transform_values { result_value(_1) } in Array value.map { result_value(_1) } else value end end |
.set_computed_usage_attributes(span, usage) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/anthropic/helpers/tracing/response.rb', line 9 def set_computed_usage_attributes(span, usage) return if usage.nil? unless usage[:input_tokens].nil? # The API's `input_tokens` leaves out the cached tokens, which the attribute counts. cached = usage[:cache_creation_input_tokens].to_i + usage[:cache_read_input_tokens].to_i span.set_attribute(GEN_AI_USAGE_INPUT_TOKENS, usage[:input_tokens] + cached) end compactions = usage[:iterations].to_a.select { _1[:type].to_sym == :compaction } return if compactions.empty? input_tokens = compactions.sum do _1[:input_tokens] + _1[:cache_creation_input_tokens] + _1[:cache_read_input_tokens] end span.set_attribute(ANTHROPIC_USAGE_COMPACTION_INPUT_TOKENS, input_tokens) span.set_attribute(ANTHROPIC_USAGE_COMPACTION_OUTPUT_TOKENS, compactions.sum { _1[:output_tokens] }) end |
.set_content_attributes(span, input, others, stop_sequences: nil, limit: CONTENT_LIMIT) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets each value as one JSON string, and stop_sequences as it is. When they total more than
limit, content is removed, and the span gets anthropic.content.truncated.
First, stop_sequences, or the gen_ai.tool.call.arguments of a tool's span, is left out
whole when it is over limit by itself.
Then message parts are removed whole (truncate_parts): those of
gen_ai.system_instructions, then of each input message, oldest first, then of each output
message. On a tool's span, anthropic.tool_result.content loses content blocks instead, the
last one first.
Emptied lists stay, so what is left can still be over a small limit.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/anthropic/helpers/tracing/content.rb', line 216 def set_content_attributes(span, input, others, stop_sequences: nil, limit: CONTENT_LIMIT) values = {**others, GEN_AI_INPUT_MESSAGES => input}.compact jsons = values.transform_values { JSON.generate(_1) } stop_sequences = nil if stop_sequences&.empty? stop_bytes = stop_sequences.to_a.sum(&:bytesize) reduced = false if stop_bytes > limit stop_sequences = nil stop_bytes = 0 reduced = true end if jsons[GEN_AI_TOOL_CALL_ARGUMENTS].to_s.bytesize > limit jsons.delete(GEN_AI_TOOL_CALL_ARGUMENTS) reduced = true end excess = jsons.sum { _2.bytesize } + stop_bytes - limit blocks = others[ANTHROPIC_TOOL_RESULT_CONTENT] if excess.positive? && blocks&.any? jsons[ANTHROPIC_TOOL_RESULT_CONTENT] = JSON.generate(blocks.first(kept_items(blocks, excess))) reduced = true elsif excess.positive? fitted = values.slice(GEN_AI_SYSTEM_INSTRUCTIONS).transform_values(&:dup) [GEN_AI_INPUT_MESSAGES, GEN_AI_OUTPUT_MESSAGES].each do |name| fitted[name] = values[name].map { {**_1, parts: _1[:parts].dup} } if values.key?(name) end lists = fitted.flat_map do |name, value| name == GEN_AI_SYSTEM_INSTRUCTIONS ? [value] : value.map { _1[:parts] } end if truncate_parts(lists, excess) fitted.each { jsons[_1] = JSON.generate(_2) } reduced = true end end jsons.each { span.set_attribute(_1, _2) } span.set_attribute(GEN_AI_REQUEST_STOP_SEQUENCES, stop_sequences) unless stop_sequences.nil? # Absent, never `false`, on a span that keeps all of its content. span.set_attribute(ANTHROPIC_CONTENT_TRUNCATED, true) if reduced end |
.set_message_content_attributes(span, body, output, limit: CONTENT_LIMIT) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/anthropic/helpers/tracing/content.rb', line 15 def (span, body, output, limit: CONTENT_LIMIT) body = symbolize_keys(body.slice(:messages, :system, :stop_sequences)) output = [{role: "assistant", parts: parts(output)}] unless output.nil? stop_sequences = body[:stop_sequences] set_content_attributes( span, (body[:messages]), { GEN_AI_SYSTEM_INSTRUCTIONS => (parts(body[:system]) unless body[:system].nil?), GEN_AI_OUTPUT_MESSAGES => output }, stop_sequences: (text_array(stop_sequences) if stop_sequences.is_a?(Array)), limit: limit ) end |
.start_tool_runner_span(client, params) ⇒ OpenTelemetry::Trace::Span?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns nil when the client's spans are disabled, or when the span cannot start. A failure
to read params is logged, and the span then starts without attributes.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/anthropic/helpers/tracing/tool_spans.rb', line 14 def start_tool_runner_span(client, params) tracer = client.open_telemetry_tracer return nil if tracer.nil? names = Anthropic::Internal::Tracing attributes = recording("the tool runner") do { **client.open_telemetry_attributes, names::GEN_AI_OPERATION_NAME => GEN_AI_OPERATION_NAME_VALUE_INVOKE_AGENT, names::GEN_AI_REQUEST_MODEL => params[:model]&.to_s, GEN_AI_CONVERSATION_ID => conversation_id(params[:request_options]), GEN_AI_OUTPUT_TYPE => (GEN_AI_OUTPUT_TYPE_VALUE_JSON if json_output_format?(params)) }.compact end recording("the tool runner") do tracer.start_span("anthropic.messages.tool_runner", kind: :internal, attributes: attributes) end end |
.start_tool_use_span(client, tool, tool_use, request_options) ⇒ OpenTelemetry::Trace::Span?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns nil when the client's spans are disabled, or when the span cannot start.
end_tool_use_span ends the span.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/anthropic/helpers/tracing/tool_spans.rb', line 59 def start_tool_use_span(client, tool, tool_use, ) tracer = client.open_telemetry_tracer return nil if tracer.nil? mcp = tool.is_a?(Anthropic::Helpers::Tools::Mcp::Tool) operation = mcp ? "anthropic.mcp_tool_use" : "anthropic.custom_tool_use" attributes = { **client.open_telemetry_attributes, Anthropic::Internal::Tracing::GEN_AI_OPERATION_NAME => GEN_AI_OPERATION_NAME_VALUE_EXECUTE_TOOL, GEN_AI_TOOL_NAME => tool_use.name, GEN_AI_TOOL_CALL_ID => tool_use.id, GEN_AI_TOOL_TYPE => GEN_AI_TOOL_TYPE_VALUE_FUNCTION } # Read as it is, so that a value of another type sets no attribute and raises nothing. toolset_name = tool_use[:toolset_name] attributes[ANTHROPIC_TOOLSET_NAME] = toolset_name if toolset_name.is_a?(String) description = tool_description(tool) attributes[GEN_AI_TOOL_DESCRIPTION] = description unless description.nil? # Options that cannot be read set no ID, and do not fail the run. id = begin conversation_id() rescue StandardError nil end attributes[GEN_AI_CONVERSATION_ID] = id unless id.nil? recording("the tool run") do tracer.start_span( "#{operation} #{tool_use.name}", kind: :internal, attributes: attributes ) end end |
.symbolize_keys(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A deep copy of value whose Hashes have Symbol keys, and whose models are such Hashes.
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/anthropic/helpers/tracing/content.rb', line 86 def symbolize_keys(value) case value in Hash value.to_h { |key, item| [key.is_a?(String) ? key.to_sym : key, symbolize_keys(item)] } in Array value.map { symbolize_keys(_1) } in Anthropic::Internal::Type::BaseModel symbolize_keys(value.to_h) else value end end |
.text_array(array) ⇒ Array<String>?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
nil when any item is not text (utf8_text).
54 55 56 57 |
# File 'lib/anthropic/helpers/tracing/content.rb', line 54 def text_array(array) texts = array.map { utf8_text(_1.to_s) } texts unless texts.any?(&:nil?) end |
.tool_description(tool) ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
nil for no description, an empty one, or one that is not text (utf8_text): it can come from
an MCP server.
99 100 101 102 103 104 |
# File 'lib/anthropic/helpers/tracing/tool_spans.rb', line 99 def tool_description(tool) description = tool.class.doc_string return nil unless description.is_a?(String) && !description.empty? utf8_text(description) end |
.tool_result_content(content) ⇒ Array<Object>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The content blocks of a tool_result block. A text or search_result block keeps its
fields (result_value), and any other block only its type.
79 |
# File 'lib/anthropic/helpers/tracing/content.rb', line 79 def tool_result_content(content) = tool_result_blocks(symbolize_keys(content)) |
.truncate_parts(lists, excess) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Removes parts from lists, in place, from the first on, until excess fewer bytes of JSON
remain. The last part left, when it is a text part, loses the end of its content instead.
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/anthropic/helpers/tracing/content.rb', line 263 def truncate_parts(lists, excess) left = lists.sum(&:length) reduced = false lists.each do |parts| while excess.positive? && parts.any? part = parts.first if left == 1 && part[:type] == "text" text = text_within(part[:content], excess) parts[0] = {**part, content: text} return reduced || text != part[:content] end parts.shift left -= 1 reduced = true # A part that is not the last of its list goes with the comma after it. excess -= JSON.generate(part).bytesize + (parts.empty? ? 0 : 1) end end reduced end |
.utf8_text(string) ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
nil when string is not text in its encoding: an exporter fails on a string that is not
valid UTF-8, which would lose every span it exports with it.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/anthropic/helpers/tracing/content.rb', line 37 def utf8_text(string) text = if string.encoding == Encoding::BINARY string.dup.force_encoding(Encoding::UTF_8) else string.encode(Encoding::UTF_8) end text if text.valid_encoding? rescue EncodingError nil end |