Cursor SDK (tracing)

Respan supports Cursor tracing in Python through Cursor hook payloads and in TypeScript through the Cursor SDK (@cursor/sdk). Use the Python setup for Cursor hook events, or the TypeScript setup for programmatic Cursor SDK agent runs.

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

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

Setup

1

Install packages

1pip install respan-ai respan-instrumentation-cursor-sdk
2

Set environment variables

$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
$export RESPAN_BASE_URL="https://api.respan.ai/api"
3

Initialize and run

1from respan import Respan
2from respan_instrumentation_cursor_sdk import CursorSDKInstrumentor
3
4respan = Respan(instrumentations=[CursorSDKInstrumentor()])
5processor = CursorSDKInstrumentor.create_processor()
6
7processor.process_event(
8 {
9 "hook_event_name": "afterAgentThought",
10 "conversation_id": "conv_123",
11 "generation_id": "gen_456",
12 "text": "Inspecting repository files to find the right implementation.",
13 "duration_ms": 320,
14 }
15)
16
17respan.shutdown()

For TypeScript, pass sdkModule when your app imports @cursor/sdk directly. This ensures the instrumentor patches the same Cursor SDK module instance your application uses.

4

Configure Cursor hooks for Python

Use respan-cursor-hook as your Cursor hook command when using the Python hook integration.

1{
2 "version": 1,
3 "hooks": {
4 "beforeSubmitPrompt": [{ "command": "respan-cursor-hook" }],
5 "afterAgentThought": [{ "command": "respan-cursor-hook" }],
6 "afterShellExecution": [{ "command": "respan-cursor-hook" }],
7 "afterFileEdit": [{ "command": "respan-cursor-hook" }],
8 "afterMCPExecution": [{ "command": "respan-cursor-hook" }],
9 "afterAgentResponse": [{ "command": "respan-cursor-hook" }],
10 "stop": [{ "command": "respan-cursor-hook" }]
11 }
12}
5

Trace streamed runs in TypeScript

1const agent = await cursorSdkModule.Agent.create({
2 name: "cursor_stream_agent",
3 model: { id: "cursor-small" },
4});
5
6const run = await agent.send("Use the docs MCP server and explain Cursor SDK streaming.", {
7 mcpServers: {
8 docs: {
9 type: "stdio",
10 command: "node",
11 args: ["docs-server.js"],
12 },
13 },
14});
15
16for await (const event of run.stream()) {
17 console.log(event.type);
18}
19
20const finalResult = await run.wait();
21console.log(finalResult.result);
6

View your trace

Open the Traces page to inspect Cursor hook spans or Cursor TypeScript SDK agent spans, generation spans, tool spans, streamed events, and local custom tool activity.

Captured data

1- Cursor hook payloads
2- Conversation and generation identifiers
3- Agent thoughts
4- Shell execution events
5- File edit events
6- MCP execution events
7- Agent responses
8- Stop events

Local custom tools

Cursor TypeScript SDK runs can define local custom tools. The instrumentor captures the tool definition and the tool call result.

1const agent = await cursorSdkModule.Agent.create({
2 name: "cursor_custom_tools_agent",
3 model: { id: "cursor-small" },
4});
5
6const run = await agent.send("Look up Tokyo weather with a local custom tool.", {
7 local: {
8 customTools: {
9 lookup_weather: {
10 description: "Return deterministic weather for a city.",
11 inputSchema: {
12 type: "object",
13 properties: { city: { type: "string" } },
14 required: ["city"],
15 },
16 execute: async ({ city }: { city: string }) => ({
17 city,
18 condition: "clear",
19 temperatureC: 21,
20 }),
21 },
22 },
23 },
24});
25
26await run.wait();

Attributes

Use Respan attributes to group Cursor runs by user, thread, or workflow.

1from respan import Respan, propagate_attributes
2from respan_instrumentation_cursor_sdk import CursorSDKInstrumentor
3
4respan = Respan(instrumentations=[CursorSDKInstrumentor()])
5
6with propagate_attributes(
7 customer_identifier="user_123",
8 thread_identifier="cursor-session-abc",
9 metadata={"environment": "production"},
10):
11 processor = CursorSDKInstrumentor.create_processor()
12 processor.process_event({...})
AttributeTypeDescription
customer_identifierstr | stringIdentifies the end user in Respan analytics.
thread_identifierstr | stringGroups related Cursor activity into a session.
trace_group_identifierstringGroups related TypeScript SDK spans into a named trace group.
metadatadict | objectCustom key-value pairs attached to spans.