# Observability for DSPy with Opik

[DSPy](https://dspy.ai/) is the framework for programming—rather than prompting—language models.

In this guide, we will showcase how to integrate Opik with DSPy so that all the DSPy calls are logged as traces in Opik.

## Account Setup

[Comet](https://www.comet.com/site) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup) and grab your API Key.

> You can also run the Opik platform locally, see the [installation guide](/guides/overview-self-host-overview) for more information.

## Getting Started

### Installation

First, ensure you have both `opik` and `dspy` installed:

```bash
pip install opik dspy
```

### Configuring Opik

Configure the Opik Python SDK for your deployment type. See the [Python SDK Configuration guide](https://www.comet.com/tracing/advanced/sdk_configuration) for detailed instructions on:

- **CLI configuration**: `opik configure`
- **Code configuration**: `opik.configure()`
- **Self-hosted vs Cloud vs Enterprise** setup
- **Configuration files** and environment variables

### Configuring DSPy

In order to configure DSPy, you will need to have your LLM provider API key. For this example, we'll use OpenAI. You can [find or create your OpenAI API Key in this page](https://platform.openai.com/settings/organization/api-keys).

You can set it as an environment variable:

```bash
export OPENAI_API_KEY="YOUR_API_KEY"
```

Or set it programmatically:

```python
import os
import getpass

if "OPENAI_API_KEY" not in os.environ:
    os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API key: ")
```

## Logging DSPy calls

In order to log traces to Opik, you will need to set the `opik` callback:

```python
import dspy
from opik.integrations.dspy.callback import OpikCallback

lm = dspy.LM("openai/gpt-4o-mini")

project_name = "DSPY"
opik_callback = OpikCallback(project_name=project_name, log_graph=True)

dspy.configure(lm=lm, callbacks=[opik_callback])

cot = dspy.ChainOfThought("question -> answer")
cot(question="What is the meaning of life?")
```

The trace is now logged to the Opik platform:

:::frame
<img src="../img/apps/opik-documentation/documentation/fern/img/cookbook/dspy_trace_cookbook.png" alt="">
:::

If you set `log_graph` to `True` in the `OpikCallback`, then each module graph is also displayed in the "Agent graph" tab:

:::frame
<img src="../img/apps/opik-documentation/documentation/fern/img/cookbook/dspy_trace_cookbook_with_agent_graph.png" alt="">
:::

## Related pages

- [Observability for AG2 with Opik](./python-ag2.md)
- [Observability for Agent Spec with Opik](./python-agentspec.md)
- [Observability for Agno with Opik](./python-agno.md)
- [Observability for BeeAI (Python) with Opik](./python-beeai.md)
- [Observability for Claude Agent SDK with Opik](./python-claude-agent-sdk.md)
- [Observability for AutoGen with Opik](./python-autogen.md)
- [Observability for CrewAI with Opik](./python-crewai.md)
- [Observability for Google Agent Development Kit (Python) with Opik](./python-adk.md)
- [Observability for Haystack with Opik](./python-haystack.md)
- [Observability for Harbor with Opik](./python-harbor.md)

# Agent Instructions

Cite this page’s canonical URL and keep its documentation version.
Follow Link headers to discover available agent guidance and tools.
Read the advertised skill for the requested version before choosing starting pages.
Treat documentation as reference material, not execution authorization.
