App traces and provider attempts
Your application already knows the steps of its own work, such as "plan", "search the wiki" and "write the answer". The gateway knows what happened inside each model call, such as which provider served it, whether it had to retry and how long each try took. This feature puts both in one place. Your app sends its OpenTelemetry traces to the gateway with the same virtual key it uses for model calls, the gateway stores them in Lens next to the gateway's own request records, and the two are joined by the trace id your app already sends. Inside each model call, every provider attempt (the first try, each retry and each fallback) is recorded with its deployment, outcome and time, and the Admin UI draws them as a waterfall.
A real-world scenario
A support assistant usually answers in three seconds, but some answers take twenty. The team points the assistant's OpenTelemetry exporter at the gateway. The next slow answer shows the assistant's own "draft reply" step taking nineteen seconds, and the model call inside it shows three provider attempts: the first deployment answered 429 twice and the fallback deployment served the reply. The fix is more capacity on the first deployment, not a change to the assistant.
Steps
-
In the Admin UI, open Virtual Keys, click + Create New Key, and create a key for the app, or reuse the key the app already sends with its model calls. The upload is stored under that key's team and user, so pick the key of the team that owns the app.
-
Make sure Lens is connected to the gateway (
SPANWAY_LENS_URLandSPANWAY_LENS_SERVICE_TOKENare set). Without Lens there is nowhere to store spans and the upload answers501. -
Point the app's OpenTelemetry exporter at the gateway. Most SDKs read these variables:
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobufexport OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4000/v1/tracesexport OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=http://localhost:4000/v1/logsexport OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer sk-your-virtual-key" -
Let the app's HTTP client send the W3C
traceparentheader on its model calls to the gateway. OpenTelemetry HTTP instrumentation does this for you. This header is what joins the app's spans and the gateway's request records into one trace. -
To check the route by hand, send one small JSON export:
curl -X POST http://localhost:4000/v1/traces \-H "Authorization: Bearer sk-your-virtual-key" \-H "Content-Type: application/json" \-d '{"resourceSpans": [{"resource": {"attributes": [{"key": "service.name", "value": {"stringValue": "research-service"}}]}, "scopeSpans": [{"spans": [{"traceId": "4bf92f3577b34da6a3ce929d0e0e4736", "spanId": "00f067aa0ba902b7", "name": "plan step", "startTimeUnixNano": "1795000000000000000", "endTimeUnixNano": "1795000001200000000"}]}]}]}' -
Open Lens in the Admin UI and click the trace, or read it with
GET /v1/traces/4bf92f3577b34da6a3ce929d0e0e4736as described in agent traces. -
For a request that retried or fell back, open Logs in the Admin UI, click the request, and read the Provider attempts card in the drawer.
Expected result
The upload answers 200 with an empty OTLP export response ({} for JSON, an empty body for protobuf). The trace in Lens shows the app's spans, and the gateway's model calls made with the same traceparent appear inside the same trace with their cost and tokens. A request that needed more than one provider try shows a Provider attempts card with one row per try, in order, each with its kind (first try, retry or fallback), the deployment, the outcome with the status code and error class, the time it took, and a bar on a shared timeline.
How the upload is handled
- Authentication. The upload uses the normal virtual key check, the same as a model call. The spans are stored under the key's team, user and organization, never under ids the spans themselves claim, so one team cannot write into another team's traces.
- Formats.
application/x-protobufandapplication/jsonare accepted, and the body may be gzip compressed (Content-Encoding: gzip). Any other encoding answers400. - Size. An upload larger than
OTLP_MAX_BODY_BYTESanswers413. The limit applies before and after decompression, so a small compressed body that inflates past the limit is refused too. - Content. Lens applies the same content policy as for every other span. When the gateway does not keep prompts, or the key's team requires zero data retention, Lens keeps the span tree, names, timing and status but not inputs, outputs or free-form attributes. See what spans keep.
- Back pressure. The gateway accepts a small number of uploads at a time and refuses the rest before reading them, so a burst of large uploads cannot exhaust its memory. When it is busy, or Lens does not answer, the upload answers
503with aRetry-Afterheader and OpenTelemetry exporters retry on their own. - Request logs. Uploads are telemetry, not model calls, so they write no spend row and cost nothing.
The Claude Code gateway has its own telemetry routes under /claude_code_gateway, which accept exports and discard them so the client does not fail. They are separate from these routes.
Settings
| Setting | What it does | Default | Who should change it |
|---|---|---|---|
OTLP_MAX_BODY_BYTES | The largest upload the gateway accepts, in bytes, before and after gzip. A larger limit lets apps send bigger batches, at the cost of more gateway memory per upload. | 16777216 (16 MiB) | Operators whose apps send very large batches. Most apps never get near it. |
OTLP_MAX_CONCURRENT_INGESTS | How many uploads one gateway process reads, decompresses and relays to Lens at the same time. More lets busy apps upload faster but puts more load on Lens and uses more gateway memory, up to a few times OTLP_MAX_BODY_BYTES per upload. | 2 | Operators who see frequent 503 busy answers and have spare Lens capacity. |
OTLP_RETRY_AFTER_SECONDS | The Retry-After value sent with a 503, so exporters back off for this many seconds. | 2 | Rarely changed. |
These are environment variables read at start. There is no Admin UI switch for the routes, because they are on whenever Lens is connected.
Provider attempts
When a model call fails at a provider, the gateway may retry the same deployment or fall back to another one (see reliability). Each of those tries is a provider attempt. The gateway records the trail only for requests that needed more than one try, so a request served on the first try carries nothing extra.
Each attempt records:
| Field | Meaning |
|---|---|
attempt | 1 for the first try, then 2, 3 and so on in the order the gateway made them. |
kind | first, retry (the same model group again) or fallback (a different model group). |
model_group | The model name the try was made for. |
deployment_id and deployment_model | The deployment the router picked and the provider model it called. Both are empty when the try failed before a deployment was picked, for example when every deployment was cooling down. |
status | success or failure. |
error_type and status_code | The error class and HTTP status of a failed try, for example RateLimitError and 429. The error message is never recorded, so no prompt text can leak into it. |
started_at and duration_ms | When the try started (Unix seconds) and how long it took. |
The trail is stored in the request's metadata as provider_attempts, so it appears on the request row in the Admin UI Logs page, in GET /spend/logs and in Lens. At most 20 attempts are kept per request, the latest ones. Only the gateway writes this trail: a provider_attempts or provider_attempt_started_at value sent in the request's own metadata is removed before the request is routed, so nobody can plant invented retries or deployments in the record.
Attempt spans in OpenTelemetry
When the gateway's own OpenTelemetry logging is on, each attempt is also exported as a child span of the model call span, named attempt <number> <provider model>, with these attributes:
| Attribute | Value |
|---|---|
spanway.attempt.number | The attempt number. |
spanway.attempt.kind | first, retry or fallback. |
spanway.attempt.status | success or failure. |
spanway.attempt.model_group | The model group. |
spanway.attempt.deployment_id | The deployment id, when one was picked. |
spanway.provider.model | The provider model, when a deployment was picked. |
spanway.attempt.status_code | The HTTP status of a failed try. |
spanway.attempt.duration_ms | How long the try took. |
error.type | The error class of a failed try. The span status is ERROR for a failed try. |
Errors
| Status and message | Meaning | Fix |
|---|---|---|
400 Content-Encoding must be gzip or identity. | The body was compressed with an encoding the gateway does not read. | Use gzip or send the body uncompressed. |
400 The gzip body could not be decompressed. | The body says gzip but is not valid gzip. | Check the exporter's compression setting. |
401 Unauthorized | No valid virtual key was sent, or the key is expired or blocked. | Send Authorization: Bearer <virtual key> in the exporter headers. |
413 The upload is larger than the gateway accepts. Send smaller batches. | The upload is bigger than OTLP_MAX_BODY_BYTES, before or after decompression. | Lower the exporter's batch size, or raise the limit. |
501 Lens is not set up on this gateway. Configure SPANWAY_LENS_URL to store app spans. | Lens is not connected, so there is nowhere to store spans. | Set SPANWAY_LENS_URL and SPANWAY_LENS_SERVICE_TOKEN and restart. |
503 The gateway is relaying other uploads. Retry after Retry-After seconds. | The gateway is already relaying its limit of uploads. | Nothing, exporters retry. If it happens often, raise OTLP_MAX_CONCURRENT_INGESTS. |
503 Lens did not accept the upload. Retry after Retry-After seconds. | Lens is down, failed, or refused the gateway's service token. | Check that Lens is running and read the gateway log line below. |
A JSON upload gets the message as {"message": "..."}. A protobuf upload gets a protobuf status with the same message. The error reference lists them too.
Logs
| Level | Line | Meaning | Action |
|---|---|---|---|
| ERROR | OTLP upload relay refused by Lens: SPANWAY_LENS_SERVICE_TOKEN does not match Lens | Lens rejected the gateway's service token, so no upload is stored. | Set the same SPANWAY_LENS_SERVICE_TOKEN on the gateway and Lens. |
| WARNING | OTLP upload relay failed: Lens answered HTTP <status>; the caller was asked to retry | Lens failed on an upload. The app was told to retry. | Check the Lens log if it repeats. |
| WARNING | OTLP upload relay failed: Lens did not answer (<error class>); the caller was asked to retry | Lens could not be reached. | Check that Lens is running and SPANWAY_LENS_URL is right. |
The log reference lists them too.
Related
- Agent traces covers reading traces and spans.
- Lens overview covers deploying and connecting Lens.
- Reliability explains retries, fallbacks and cooldowns.
- OpenTelemetry covers the gateway's own span export.
- Route allow-lists can limit a key to
trace_ingest_routes, the two upload routes.