Skip to main content
DSPy is a framework for programming language model pipelines. Braintrust instruments DSPy runs so you can inspect module spans, adapter formatting and parsing steps, underlying LLM calls, and tool usage in a single trace.

Setup

Install Braintrust and DSPy:
Set your API keys before you run your app:
.env

Instrument DSPy

Braintrust’s DSPy integration requires dspy>=2.6.0.

Automatic Instrumentation

braintrust.auto_instrument() patches dspy.configure() so Braintrust’s DSPy callback is added automatically when you configure DSPy.
trace-dspy-auto.py
If you only want to patch DSPy instead of enabling all supported Python integrations, use patch_dspy().
trace-dspy-patch-only.py

Manual callback setup

Use BraintrustDSpyCallback() when you want to attach the callback explicitly. If you also want detailed LiteLLM token and cost spans, patch LiteLLM before importing DSPy.
trace-dspy-manual.py
Python resources:

Examples

In Braintrust, a DSPy execution typically appears as a parent DSPy module span with child spans for adapter work and model calls. Example trace shape:
Braintrust captures:
  • DSPy module spans such as Predict, ChainOfThought, and other module executions
  • Adapter formatting and parsing spans
  • LLM call inputs, outputs, and latency on dspy.lm spans
  • Tool execution when your DSPy program invokes tools
  • Extra LiteLLM token and cost spans when you patch LiteLLM

Resources