Integrations / DSPy

Trace DSPy programs

To trace DSPy with Tracify, wrap the function that calls your DSPy program with trace_agent, and open a step around each module you want to see. Model calls made inside it stay in the same run.

Your code

DSPy

Tracify

One run.

  • 01One trace per program call
  • 02A step for each module you mark
  • 03Inputs and outputs of each module
  • 04Errors and slow modules in order

Setup

5 steps.

  1. 01

    Install

    DSPy and the Tracify SDK.

    bash
    pip install tracify-sdk dspy
  2. 02

    Set your project key

    Create a project in Tracify, copy its API key and put it in the environment where your code runs. The SDK reads it from there, so nothing is hard-coded.

    bash
    export TRACIFY_API_KEY="tracify_sk_live_..."
  3. 03

    Wrap the program call

    trace_agent turns each call into one run. A step around the module shows its input and output inside that run.

    python
    import dspy
    import tracify
    
    program = dspy.ChainOfThought("question -> answer")
    
    @tracify.trace_agent(name="dspy-qa")
    def answer(question: str) -> str:
        with tracify.span("chain-of-thought", type="agent", input=question) as step:
            result = program(question=question)
            step.set_output(result.answer)
            return result.answer
  4. 04

    Mark the modules you care about

    For a program with several modules, open a step in each one's forward method. The run then shows which module was slow or wrong.

    python
    class RAG(dspy.Module):
        def __init__(self):
            self.retrieve = dspy.Retrieve(k=3)
            self.generate = dspy.ChainOfThought("context, question -> answer")
    
        def forward(self, question):
            with tracify.span("retrieve", type="retrieval", input=question) as step:
                context = self.retrieve(question).passages
                step.set_output(context)
            with tracify.span("generate", type="llm", input=question):
                return self.generate(context=context, question=question)
  5. 05

    Run it once and look

    Run your code one time, then open the Tracing page of your project. Your DSPy program shows up as a run with its steps in order, plus the tokens, time and any error for each call.

Good to know

Steps, not model internals

DSPy calls its language model through its own layer, so the SDK does not see each raw model request automatically. You see the modules you mark, with their inputs and outputs and timing.

Good to know

Tracing during optimization

Optimizers can run your program hundreds of times. Use the sample rate option on the client so you only keep a share of those runs.

Full details in the documentation. All integrations are listed on the integrations page.

Will I see the prompts DSPy builds?
You see the input and output of each step you mark. The final prompt text built inside DSPy is not captured unless you record it yourself.
Can I trace an optimizer or evaluation run?
Yes. Wrap the function that calls compile or evaluate with trace_agent. Lower the sample rate if it makes many calls.
Does it work with async DSPy programs?
Yes. trace_agent and span work in async functions and keep concurrent runs in separate traces.

Send your first trace.

Free to start.