Braintrust.init performs auto-instrumentation of LLM libraries #81
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.
Purpose
This PR introduces automatic LLM library instrumentation as part of
Braintrust.init, making the SDK zero-configuration for most use cases. Previously, users had to manually callBraintrust.instrument!(:openai)(etc.) for each LLM library. Now, a singleBraintrust.initcall automatically detects and instruments all supported libraries.Before:
After:
Architectural Overview
New Components
Braintrust.auto_instrument!lib/braintrust/contrib.rbBraintrust::Contrib.auto_instrument!lib/braintrust/contrib.rbBraintrust::Internal::Envlib/braintrust/internal/env.rbControl Flow
Environment Variables
BRAINTRUST_AUTO_INSTRUMENTtrue"false"to disableBRAINTRUST_INSTRUMENT_ONLY"openai,anthropic")BRAINTRUST_INSTRUMENT_EXCEPT"ruby_llm")Usage Examples
Default: Auto-instrument everything
Disable auto-instrumentation
Selective instrumentation
Standalone auto-instrument (without init)
Supported Integrations
Auto-instrumentation works with all registered integrations:
:openai- OpenAI's official Ruby gem:ruby_openai- alexrudall's ruby-openai gem:anthropic- Anthropic's official Ruby gem:ruby_llm- RubyLLM unified interfaceKey Design Decisions
Enabled by default: Auto-instrumentation is on unless explicitly disabled, providing the best out-of-box experience.
Config precedence: Explicit
auto_instrument:parameter overrides environment variables, allowing code to take precedence when needed.Graceful degradation: Only instruments libraries that are actually loaded; missing libraries are silently skipped.
Backward compatible: Existing
Braintrust.instrument!(:name)calls continue to work. Users who prefer manual control can disable auto-instrumentation.Files Changed
lib/braintrust.rb- Addedauto_instrumentparameter toinitlib/braintrust/contrib.rb- Addedauto_instrument!methods at both levelslib/braintrust/internal/env.rb- New environment variable utilitiesREADME.md- Updated quick start and examplestest/braintrust/contrib_test.rb- Comprehensive test coverageexamples/setup/init.rb- Full demo of auto-instrumentationexamples/setup/init_minimal.rb- Minimal example