Setup
Install the Braintrust and DSPy packages, then set your API keys. Requiresdspy v2.6.0 or later.1
Install packages
2
Set environment variables
.env
Auto-instrumentation
To trace DSPy runs without modifying your application code, callbraintrust.auto_instrument() before you configure DSPy. It patches dspy.configure() so Braintrust’s DSPy callback is attached automatically.patch_dspy().Manual instrumentation
To trace DSPy runs manually, attachBraintrustDSpyCallback() yourself when you configure DSPy. For detailed LiteLLM token and cost spans, patch LiteLLM before importing DSPy.What Braintrust traces
A DSPy execution appears as a parent module span with child spans for adapter work and model calls:- Module spans (
dspy.module.Predict,dspy.module.ChainOfThought, and other module classes), with module inputs, the module class, and outputs. - Adapter formatting and parsing spans (
dspy.adapter.formatanddspy.adapter.parse), with inputs, the adapter class, and outputs. - LLM call spans (
dspy.lm), with inputs, the model and provider, request parameters (temperature,max_tokens,top_p,top_k,stop), outputs, and latency. - Tool spans (named after the invoked tool), with tool inputs and outputs.
- Evaluation spans (
dspy.evaluate), with evaluation inputs, the metric name, thread count, outputs, and score metrics (accuracy,score,total,correct). - Additional LiteLLM completion spans (
Completion), with token usage and latency metrics, when you patch LiteLLM.