Skip to main content
DSPy is a declarative framework for programming language models developed at Stanford NLP. Braintrust traces DSPy applications by combining LiteLLM instrumentation with DSPy-specific callbacks to capture module executions and LLM interactions.

Setup

Install DSPy alongside the Braintrust SDK:
// uv
uv add braintrust dspy
// pip
pip install braintrust dspy

Trace with DSPy

DSPy uses LiteLLM internally, so Braintrust tracing requires patching LiteLLM and configuring a DSPy callback. Patch LiteLLM before importing DSPy, and then configure the Braintrust callback:
trace-dspy.py
# Patch LiteLLM before importing DSPy
from braintrust.wrappers.litellm import patch_litellm

patch_litellm()

# Now import DSPy
import dspy
from braintrust import init_logger
from braintrust.wrappers.dspy import BraintrustDSpyCallback

# Initialize Braintrust
logger = init_logger(project="dspy-example")

# Configure DSPy with Braintrust callback
lm = dspy.LM("openai/gpt-4o-mini")
dspy.configure(lm=lm, callbacks=[BraintrustDSpyCallback()])

# Use DSPy as normal - all execution is automatically traced
cot = dspy.ChainOfThought("question -> answer")
result = cot(question="What is the capital of France?")
This will automatically log:
  • DSPy module executions (Predict, ChainOfThought, ReAct, etc.)
  • LLM calls with detailed token counts and costs (via LiteLLM)
  • Tool invocations
  • Hierarchical span relationships
  • Complete pipeline observability

Resources