> ## Documentation Index
> Fetch the complete documentation index at: https://traceroot.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# DSPy

> Auto-instrument DSPy modules, signatures, and chain-of-thought reasoning

Automatically capture module executions, signature predictions, and underlying LLM calls in [DSPy](https://github.com/stanfordnlp/dspy) programs.

## Install

> [tab] Python
>
> ```bash theme={null}
> pip install "traceroot[dspy]"
> ```

The `dspy` extra brings in DSPy itself along with the OpenInference instrumentor that emits its spans.

## Set up

> [tab] Python
>
> ```python theme={null}
> import traceroot
> from traceroot import Integration
>
> traceroot.initialize(integrations=[Integration.DSPY])
> ```

## Usage

Once initialized, every DSPy module call and underlying LLM request is traced automatically:

```python theme={null}
import dspy
import traceroot
from traceroot import Integration

traceroot.initialize(integrations=[Integration.DSPY])

# DSPy resolves the API key from OPENAI_API_KEY in the environment.
dspy.configure(lm=dspy.LM("openai/gpt-4o-mini", max_tokens=1024))


class CoTQA(dspy.Module):
    """Chain-of-thought question-answering module."""

    def __init__(self):
        super().__init__()
        self.cot = dspy.ChainOfThought("question -> answer")

    def forward(self, question: str):
        return self.cot(question=question)


qa = CoTQA()

# The forward call, the chain-of-thought step, and the LLM call are all captured
result = qa(question="Why does ice float on water?")
print(result.reasoning)
print(result.answer)

traceroot.flush()
```

## Verify

Run your code and confirm the trace arrives:

1. **In the traceroot UI**: Navigate to **Tracing > Traces** in [traceroot Cloud](https://app.traceroot.ai). You should see a trace with root span `CoTQA.forward` — the outer module — with `ChainOfThought.forward` and `Predict(StringSignature).forward` nested beneath it.
2. **Via the CLI**: Run `traceroot traces list` to inspect your recent traces from the terminal.

## What gets captured

| Attribute | Description |
| - | - |
| Module calls | Each `Module.__call__` / `Module.forward` invocation |
| Predictors | `Predict`, `ChainOfThought`, `ReAct`, etc. as nested spans |
| Signatures | Input/output fields declared on each signature |
| LLM calls | Raw completion requests to the configured `dspy.LM` |
| Tokens & Cost | Aggregated token usage; cost when the model ID matches traceroot's pricing table |
| Latency | Duration per module call and per LLM call |

## Limitations

* Requires installing the `dspy` extra via `pip install "traceroot[dspy]"`.

## Run the example

Clone the repo and run a complete agent end-to-end.

> [card] [Python](https://github.com/traceroot-ai/traceroot/tree/main/examples/python/dspy-agent)
>
> Run the Python example

## Next steps

> [card] [Python SDK Reference](https://traceroot.ai/docs/tracing/python-sdk.md)
>
> Explore the full Python SDK reference and instrumentation options.

> [card] [Cost Tracking](https://traceroot.ai/docs/tracing/cost-tracking.md)
>
> Learn how traceroot calculates token costs and tracks model spend.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.