Skip to main content

Agent traces

Agent tracing lets you follow an agent run as a tree of spans, one for each model call, tool call and intermediate step, with its timing and, when the gateway keeps content, its inputs and outputs. It answers "what did the agent actually do" when a run was slow, expensive or wrong. Spans are stored by Lens, the gateway's observability service, and the gateway exposes a read API over them that applies the same team scoping as the rest of the management API. The Lens page in the Admin UI renders the same data.

Scenario​

A research agent sometimes takes four minutes to answer instead of twenty seconds. The owner lists yesterday's traces for that agent, opens the slowest one, and sees that a single tool call to an internal search service waited three minutes before timing out and retrying. The fix is a timeout on the tool, found without adding a single log line to the agent.

Steps​

  1. Create a tracing key for the agent in the Admin UI under Lens Home, Set up manually, Create tracing key, and point the agent's OTLP exporter at the Lens endpoint shown there. To send through the gateway with a virtual key instead, follow app traces and provider attempts.

  2. Run the agent once so it sends a trace.

  3. List the traces of the last day through the gateway.

    curl "http://localhost:4000/v1/traces?start_ms=1760572800000&end_ms=1760659200000" -H "Authorization: Bearer $SPANWAY_API_KEY"
  4. Open one trace with GET /v1/traces/{trace_id} and the slow span with GET /v1/traces/{trace_id}/spans/{span_id}, or open Lens in the Admin UI and click the trace in the list.

  5. For a question the list view cannot answer, run a read only SQL query over your traces with POST /v1/traces/query and a body of {"sql": "..."}, after reading GET /v1/traces/query/help for the tables and columns.

Expected result​

The list answers 200 with a page of traces in the window, each with its agent, model, duration, token counts and status, and a cursor for the next page. The trace view returns the span tree, and the span view returns the full span, with inputs and outputs only when the gateway's content policy allows them.

Sending traces​

Agents send OTLP traces and logs either directly to the Lens endpoint shown in the Lens setup panel of the Admin UI, with a Lens tracing key, or to the gateway's own POST /v1/traces and POST /v1/logs routes with a virtual key. The gateway relays an upload to Lens under the key's team and user, and joins the app's spans to the gateway's model calls by the traceparent header, as described in app traces and provider attempts. The Claude Code gateway has its own telemetry routes, which accept exports and discard them so the client does not fail.

Tracing is on when both SPANWAY_LENS_URL and SPANWAY_LENS_SERVICE_TOKEN are set on the gateway, the token being the same secret of at least 32 characters that Lens has. general_settings.tracing.store.type: lens turns it on from config.yaml, and still needs both variables. Without them the gateway logs Agent tracing unavailable: configure SPANWAY_LENS_URL and SPANWAY_LENS_SERVICE_TOKEN at startup and the read endpoints answer 501 with Agent tracing is not enabled. Configure the Lens service and SPANWAY_LENS_URL. The settings reference lists every tracing setting with its default.

What spans keep​

Lens keeps the inputs, outputs and free-form attributes of a span only when the gateway allows it. The gateway sends Lens a content policy every 30 seconds that says whether prompts are stored (store_prompts_in_spend_logs on and turn_off_message_logging off) and which teams require zero data retention. Spans from a trace credential whose team requires it, spans received while prompts are not stored, and spans received while Lens holds no policy from the last 90 seconds keep metadata only, meaning ids, the span tree, names, timing, status, agent, model, token counts, tool call ids and the exception type. store_prompts_in_spend_logs is off by default, so a default install keeps no span content. A Lens that runs without the gateway connection keeps span content. What Spanway stores covers the other stores.

Reading traces​

curl "http://localhost:4000/v1/traces?start_ms=1760572800000&end_ms=1760659200000" -H "Authorization: Bearer $SPANWAY_API_KEY"

GET /v1/traces pages through traces in a time window that defaults to the last 24 hours, GET /v1/traces/agents lists the agents seen in the window, GET /v1/traces/{trace_id} returns one trace as a page of spans with a cursor, GET .../spans/{span_id} returns one span in full and GET .../spans/{span_id}/error pages through a span's error details. POST /v1/traces/query runs a read only SQL query over the caller's traces for ad hoc analysis and GET /v1/traces/query/help documents the tables and columns it may use.

Every read is scoped. Proxy admins and admin viewers see every trace. Other keys with a user see the traces of their own requests and of the teams where they are a team admin or hold the spend logs permission, the same rule as the run ledger. A key with no user, such as a service key, answers 403. Failures use one status per cause so a client can react to each: 400 with code invalid_request for a bad cursor or query, 409 with code trace_changed when a trace changed under a traversal and the client should restart it, 413 with code too_large when a view is too big and a filtered query is needed, and 503 with code unavailable and a Retry-After header when Lens is configured but not answering, and 501 when tracing is not set up at all.

MethodPathSummary
GET/v1/traces/searchSearch the text of agent traces
GET/v1/traces/sessionsList conversation sessions
GET/v1/traces/sessions/{session_id}Read one session as a thread
GET/v1/traces/{trace_id}/graphRead the agent graph of one trace
GET/v1/traces/{trace_id}/commentsList the comments on a trace
POST/v1/traces/{trace_id}/commentsComment on a trace
PATCH/v1/traces/{trace_id}/comments/{comment_id}Edit a comment on a trace
DELETE/v1/traces/{trace_id}/comments/{comment_id}Delete a comment on a trace
GET/v1/tracesList agent traces
POST/v1/tracesSend OTLP traces from your app
GET/v1/traces/agentsList the agents seen in traces
POST/v1/traces/queryRun a read only SQL query over traces
GET/v1/traces/query/helpDescribe the tables a trace query may use
GET/v1/traces/{trace_id}Read one trace as a page of spans
GET/v1/traces/{trace_id}/spans/{span_id}Read one span in full
GET/v1/traces/{trace_id}/spans/{span_id}/errorPage through a span's error details

Retention​

Lens keeps agent traces for 14 days by default.

Data subject requests reach Lens too. Export reads the subject's Lens request logs and spans, and erase deletes them along with every span of the traces they took part in and the trace rollups that name them. The gateway asks Lens to delete them through SPANWAY_LENS_URL and the service token, and Lens deletes with its own ClickHouse user, so the gateway needs no ClickHouse credentials. When Lens cannot be reached or does not confirm the delete, the request is recorded as partial and says why, and running it again finishes Lens.

Keys for tracing​

Agents send traces with a dedicated Lens tracing key, created from Lens Home, Set up manually, Create tracing key. It can only send traces to Lens, so never use a model key or an admin key for ingestion.

On the gateway, a key that should only read traces can be limited with route allow-lists to trace_read_routes, which covers listing traces and agents, reading traces and spans, and the read only SQL query. trace_ingest_routes holds only the POST /v1/traces and POST /v1/logs upload routes, for a key that should only send telemetry. The older trace_telemetry_routes group is still accepted and grants both, including trace reads and the SQL query, so do not hand it to a key that should only send telemetry.

Errors​

StatusWhat it meansFix
400 invalid_requestThe cursor or the SQL query is malformed.Start the traversal again without the cursor, or fix the query against GET /v1/traces/query/help.
403 "Not allowed to view agent traces"The key has no user, so it cannot read traces. The trace SQL query answers the same.Call with a key that belongs to a user, or as a proxy admin or admin viewer.
404 "Trace ... not found" or "Span ... not found"The id is wrong, the trace is outside the caller's teams or it passed the retention window.Check the id in the Lens page and the 14 day default retention.
409 trace_changedThe trace changed while you paged through it.Restart the traversal from the first page.
4xx or 5xx on POST /v1/traces or POST /v1/logsThe upload was refused.See the upload errors.
413 too_largeThe view is too big to return.Narrow the time window or run a filtered SQL query.
501 "Agent tracing is not enabled. Configure the Lens service and SPANWAY_LENS_URL."The gateway has no Lens connection, because SPANWAY_LENS_URL or SPANWAY_LENS_SERVICE_TOKEN is missing or the token is shorter than 32 characters.Set both variables to the values Lens uses and restart the gateway.
503 unavailable with Retry-AfterLens is configured but did not answer, or a trace SQL query failed or ran past the reader limits (Trace SQL query failed or exceeded reader limits), or the query help did not load (Trace query help is temporarily unavailable).Retry after the given seconds. If it keeps failing, check that Lens is running and read the gateway log line named below.
503 "Trace SQL queries require a configured proxy master key"The trace SQL query and its help are signed with the master key, and the gateway has none.Set a master key on the gateway.

Logs​

LevelLineMeaningAction
WARNINGAgent tracing unavailable: configure SPANWAY_LENS_URL and SPANWAY_LENS_SERVICE_TOKENTracing is turned on but the gateway cannot connect to Lens, so tracing stays off.Set both variables and restart.
INFOAgent tracing enabled (store=lens)The gateway connected to Lens at startup.None, this is normal.
WARNINGTrace read unavailable: ...A trace or span read failed because Lens did not answer. The caller got a 503.Check that Lens is running. A single line during a Lens restart is normal.
WARNINGTrace SQL query unavailable: ...A trace SQL query failed or ran past the reader limits.Narrow the query, or check Lens if every query fails.
WARNINGTrace query help unavailable: ...The query help could not be read from Lens.Same as above.

The log reference lists these lines with the rest of the gateway's operator log lines.

Audit​

Reading trace content is recorded, because spans can hold prompts and answers. Reading a span that returns its input or output writes a viewed row on Spanway_AgentTraces with object_id <trace_id>/<span_id>, and every trace SQL query writes a viewed row with object_id trace_sql_query that keeps a SHA-256 and the length of the SQL, never the SQL itself or the rows. Listing traces and reading a span without content write nothing. Find the rows on the audit log:

curl "http://localhost:4000/audit?table_name=Spanway_AgentTraces&action=viewed" -H "Authorization: Bearer $SPANWAY_ADMIN_KEY"