Skip to Content

Traces

Traces record individual LLM calls captured by the SDK’s auto-instrumentation. Each trace stores the prompt, response, model, token count, and latency. Traces can be scored after the fact — scored traces are automatically converted to feedback entries for the optimization pipeline.

Endpoints

MethodPathDescription
POST/api/v1/tracesCreate a trace
POST/api/v1/traces/\{trace_id\}/scoreScore a trace
GET/api/v1/tracesList traces

Traces are typically created automatically by the SDK’s @ct.trace decorator or with ct.trace_context() context manager. You rarely need to call this endpoint directly.


POST /api/v1/traces

Ingest a trace from an LLM call. Usually called by the SDK automatically.

Authentication: Required (X-API-Key)

Request Body

FieldTypeRequiredDescription
task_idUUIDThe task this trace belongs to
prompt_textstringThe prompt sent to the LLM
response_textstringThe LLM’s response
modelstringModel identifier (e.g., "gpt-4o")
tokensintegerTotal token count for the call
latency_msfloatRound-trip latency in milliseconds
source_filestringSource file where the call was made
source_variablestringVariable name of the traced DSPy module
metadataobjectArbitrary key/value metadata

Example Request

curl -X POST http://localhost:8000/api/v1/traces \ -H "X-API-Key: kaizen_abc123" \ -H "Content-Type: application/json" \ -d '{ "task_id": "550e8400-e29b-41d4-a716-446655440000", "prompt_text": "You are an expert support engineer. Ticket: Login fails on Safari", "response_text": "Authentication issue on Safari browser. Check OAuth token handling.", "model": "gpt-4o", "tokens": 187, "latency_ms": 423.5, "source_file": "app/support/classifier.py", "source_variable": "summarizer" }'

Response — 201 Created

{ "id": "e5f6a7b8-c9d0-1234-efab-234567890123", "task_id": "550e8400-e29b-41d4-a716-446655440000", "prompt_text": "You are an expert support engineer. Ticket: Login fails on Safari", "response_text": "Authentication issue on Safari browser. Check OAuth token handling.", "model": "gpt-4o", "tokens": 187, "latency_ms": 423.5, "source_file": "app/support/classifier.py", "source_variable": "summarizer", "score": null, "scored_by": null, "metadata": null, "created_at": "2024-01-15T11:00:00Z" }

Error Cases

StatusCondition
401Missing or invalid API key
404task_id refers to a non-existent task

POST /api/v1/traces/{trace_id}/score

Attach a quality score to an existing trace. Once scored, the trace is available as a training signal for optimization.

Authentication: Required (X-API-Key)

Path Parameters

ParameterTypeDescription
trace_idUUIDThe trace’s unique identifier

Request Body

FieldTypeRequiredDescription
scorefloatQuality score between 0.0 and 1.0
scored_bystringWho/what scored this trace (default: "sdk")

Example Request

curl -X POST \ "http://localhost:8000/api/v1/traces/e5f6a7b8-c9d0-1234-efab-234567890123/score" \ -H "X-API-Key: kaizen_abc123" \ -H "Content-Type: application/json" \ -d '{"score": 0.8, "scored_by": "user"}'

Response — 200 OK

Returns the updated TraceResponse object with score and scored_by populated.

Error Cases

StatusCondition
401Missing or invalid API key
404Trace not found
422score is outside [0.0, 1.0]

GET /api/v1/traces

List traces, optionally filtered by task. Returns newest traces first.

Authentication: Required (X-API-Key)

Query Parameters

ParameterTypeDescription
task_idUUID (string)Filter by task ID
limitintegerNumber of results (default: 100, max: 1000)
offsetintegerNumber of entries to skip (default: 0)

Example Request

curl -H "X-API-Key: kaizen_abc123" \ "http://localhost:8000/api/v1/traces?task_id=550e8400-e29b-41d4-a716-446655440000&limit=50"

Response — 200 OK

[ { "id": "e5f6a7b8-c9d0-1234-efab-234567890123", "task_id": "550e8400-e29b-41d4-a716-446655440000", "prompt_text": "You are an expert support engineer...", "response_text": "Authentication issue on Safari browser.", "model": "gpt-4o", "tokens": 187, "latency_ms": 423.5, "source_file": "app/support/classifier.py", "source_variable": "summarizer", "score": 0.8, "scored_by": "user", "metadata": null, "created_at": "2024-01-15T11:00:00Z" } ]

Error Cases

StatusCondition
401Missing or invalid API key
Last updated on