Migrated Anthropic instrumentation to new integration API #80
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Anthropic Integration Migration to New API
Summary
This PR migrates the Anthropic gem integration to the new contrib integration API, aligning it with the patterns established by the OpenAI and RubyLLM integrations. The migration introduces a unified
Braintrust.instrument!()API while maintaining backward compatibility with the existingwrap()method.Key Changes
New Unified API
Class-level instrumentation (all clients):
Instance-level instrumentation (specific client only):
Deprecated API
The old
wrap()API still works but emits a deprecation warning:Architectural Overview
File Structure
Components
create()andstream()methods onAnthropic::Resources::Messageseach(),text(), andclose()on stream objects for deferred tracingStreaming Architecture
Streaming uses deferred span creation - the span is created when the stream is consumed, not when
stream()is called:This ensures accurate
time_to_first_tokenmetrics and complete output capture.Usage Examples
Basic Usage (Class-Level)
Instance-Level Instrumentation
Streaming
What's Traced
Each
messages.createor consumedmessages.streamcreates a span with:braintrust.input_jsonbraintrust.output_jsonbraintrust.metadatabraintrust.metricsprompt_tokens,completion_tokens,tokens,time_to_first_token)Migration Guide
require "braintrust/trace/contrib/anthropic"require "braintrust"Braintrust::Trace::Anthropic.wrap(client)Braintrust.instrument!(:anthropic, target: client)Braintrust.instrument!(:anthropic)(class-level)The old API continues to work but logs a deprecation warning.