Dify (tracing)

Dify client calls are captured with respan-instrumentation-dify so model calls, workflow calls, and feedback operations can be inspected in Respan traces.

Create an account at platform.respan.ai and grab an API key.

Run npx @respan/cli setup to set up with your coding agent.

See Dify gateway setup to route this integration through the Respan gateway.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-dify dify-client
2

Set environment variables

$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
$export DIFY_CHAT_API_KEY="YOUR_DIFY_CHAT_API_KEY"
3

Initialize and send a request

1import os
2
3from dify_client import ChatClient
4from respan import Respan
5from respan_instrumentation_dify import DifyInstrumentor
6
7respan = Respan(
8 api_key=os.environ["RESPAN_API_KEY"],
9 app_name="dify-tracing",
10 instrumentations=[DifyInstrumentor()],
11)
12
13client = ChatClient(os.environ["DIFY_CHAT_API_KEY"])
14response = client.create_chat_message(
15 inputs={},
16 query="Summarize tracing in one sentence.",
17 user="respan-user",
18 response_mode="blocking",
19)
20response.raise_for_status()
21print(response.json()["answer"])
22
23respan.flush()
24respan.shutdown()
4

View your trace

Open the Traces page to inspect Dify request and workflow spans.