[Bug]: Standalone gateway records a client-cancelled non-streaming call as `ERROR` instead of `UNSET`
Affected area
Rust core runtime
Current behavior
When a client abandons a non-streaming /v1/messages call through the standalone gateway (nemo-relay --bind) while the provider is still working, the LLM end event is recorded as otel.status_code: ERROR with otel.status_description: "LLM execution cancelled". A cancelled streaming call is now recorded as UNSET (#1815). Same mock provider for all four:
| Call | Client | Relay records |
|---|---|---|
| Streaming | cancels after 3 s | UNSET |
| Streaming | reads to the end | OK |
| Non-streaming | cancels after 3 s | ERROR, LLM execution cancelled |
| Non-streaming | waits for the response | OK |
The non-streaming status comes from ManagedLlmCompletion::drop in crates/runtime/src/api/llm.rs, which records ERROR for any drop before completion. #1815 added finish_cancelled for managed streams, and #1819 attaches GatewayCallGuard to buffered calls to release session accounting, but ManagedLlmCompletion has no cancelled path, so a cancelled buffered call still ends through drop.
Relay's documentation treats a client cancellation as neutral: docs/integrate-into-frameworks/wrap-llm-calls.mdx: "For a neutral outcome, such as a client cancelling a response, set otel.status_code to "UNSET"." This is the non-streaming counterpart of #1813.
Expected behavior
A non-streaming call that ends because the client cancelled it is recorded as UNSET, like a cancelled stream after #1815, while provider failures remain ERROR.
Steps to reproduce
-
Mock provider that answers non-streaming requests after 8 s, and streams one event every 2 s:
repro_slow_both.pyimport json import time from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer MESSAGE = {"id": "msg_1", "type": "message", "role": "assistant", "model": "mock", "content": [{"type": "text", "text": "Hello"}], "stop_reason": "end_turn", "stop_sequence": None, "usage": {"input_tokens": 10, "output_tokens": 5}} EVENTS = [ ("message_start", {"type": "message_start", "message": dict(MESSAGE, content=[], stop_reason=None, usage={"input_tokens": 10, "output_tokens": 1})}), ("content_block_start", {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}), ("content_block_delta", {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Hello"}}), ("content_block_stop", {"type": "content_block_stop", "index": 0}), ("message_delta", {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence": None}, "usage": {"output_tokens": 5}}), ("message_stop", {"type": "message_stop"}), ] class H(BaseHTTPRequestHandler): protocol_version = "HTTP/1.1" def log_message(self, *args): pass def do_POST(self): body = json.loads(self.rfile.read(int(self.headers.get("Content-Length", "0"))) or b"{}") try: if not body.get("stream"): time.sleep(8) payload = json.dumps(MESSAGE).encode() self.send_response(200) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(payload))) self.end_headers() self.wfile.write(payload) return self.send_response(200) self.send_header("Content-Type", "text/event-stream") self.send_header("Transfer-Encoding", "chunked") self.end_headers() for name, data in EVENTS: time.sleep(2) frame = f"event: {name}\ndata: {json.dumps(data)}\n\n".encode() self.wfile.write(f"{len(frame):x}\r\n".encode() + frame + b"\r\n") self.wfile.flush() self.wfile.write(b"0\r\n\r\n") except (BrokenPipeError, ConnectionResetError): pass self.close_connection = True ThreadingHTTPServer(("127.0.0.1", 8090), H).serve_forever() -
Configure the gateway as in #1781 (
anthropic_base_url = "http://127.0.0.1:8090", and an ATOF file sink writing tologs/events.jsonl). -
In a directory containing an empty
logs/folder, runpython3 repro_slow_both.py, startnemo-relay --bind 127.0.0.1:4040, and cancel a non-streaming request after 3 s:curl -s --max-time 3 http://127.0.0.1:4040/v1/messages \ -H 'content-type: application/json' -H 'anthropic-version: 2023-06-01' -H 'x-api-key: test' \ -d '{"model":"mock","max_tokens":64,"messages":[{"role":"user","content":"hi"}]}' -
After about 8 s, the
anthropic.messagesend event hasotel.status_code: ERRORandotel.status_description: "LLM execution cancelled". The same request without--max-timeis recorded asOK, and a cancelled streaming request is recorded asUNSET.
Environment
- NeMo Relay version or commit:
mainat0cec6521d, debug build (includes #1815 and #1819) - OS and architecture: macOS 27.2.1, Apple Silicon (arm64)
- Rust version: 1.96.1 (pinned by
rust-toolchain.toml) - Python version: 3.15.0 (for the mock provider)
- Node.js version: N/A
- Go version: N/A
- Install/build command:
cargo build -p nemo-relay-cli
Impact
Every non-streaming call that a client abandons through the standalone gateway (for example an SDK timeout or a user cancelling) is counted as a failed LLM call, while the same cancellation on a streaming call is neutral, so error rates depend on whether a client streams. Not checked: the daemon worker path for non-streaming cancellations.
Approach for discussion: give ManagedLlmCompletion a cancelled path that records UNSET, and have GatewayCallGuard mark a buffered call as cancelled when the client disconnects, as #1815 does for streams.
I'd like to work on this and will open a PR once you're OK with the approach.
@Sakshitapkir03 assigned to you.
Sounds good to me. Please open a PR targeting upstream main
@willkill07 Thank you for assigning this. Before starting, I'd like to confirm the approach.
The gateway deadline also reaches ManagedLlmCompletion::drop: before_deadline uses timeout_at, which drops the call on timeout. Since #1815 keeps timeouts as ERROR, treating every drop as a cancellation would mislabel them. Marking the call from GatewayCallGuard is also unreliable, because the call's future may be dropped before the guard.
I propose a cancellation marker instead: the gateway wraps the buffered call so that the marker is set before the call is dropped on a client disconnect, while a timeout leaves it unset and the call remains ERROR.
Passing the marker to llm_call_execute requires an API change. Adding a field to LlmCallExecuteParams or ExecutionOptions would be breaking, as both have public fields, so I would add llm_call_execute_with_cancellation(params, cancellation) and have llm_call_execute delegate to it. Would that work, or would you prefer the new field?
No new public API should be required/exposed.
This is an implementation detail of the Gateway only.