Skip to main content
DSPy is a framework for programming language model pipelines. Braintrust traces the full DSPy execution tree, so a single trace shows how each module decomposed into its underlying steps.

Setup

Install the Braintrust and DSPy packages, then set your API keys. Requires dspy v2.6.0 or later.
1

Install packages

2

Set environment variables

.env

Auto-instrumentation

To trace DSPy runs without modifying your application code, call braintrust.auto_instrument() before you configure DSPy. It patches dspy.configure() so Braintrust’s DSPy callback is attached automatically.
To patch only DSPy instead of enabling all supported Python integrations, use patch_dspy().

Manual instrumentation

To trace DSPy runs manually, attach BraintrustDSpyCallback() 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:
Braintrust captures:
  • 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.format and dspy.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.

Resources