KatInterview

Streaming, Stop Signals, and Safe Recovery in an LLM Agent Pipeline

System DesignModel answer
HardModel answerSystem Designstreamingcancellationfault-tolerance

Last updated

Design end-to-end token streaming, user-initiated cancellation, and failure recovery for a multi-turn agent system where tools may have irreversible side effects.

Streaming, Stop Signals, and Safe Recovery in an LLM Agent Pipeline

You are building the backend for a multi-turn conversational agent. Each turn can involve a language model, a vector retrieval step, and one or more external tools — some of which write data or trigger billable operations. Responses are streamed token-by-token to the client.

Users can click a Stop button at any point. The system must halt token delivery as quickly as possible, attempt to interrupt upstream generation, and avoid duplicating any side effects if the turn is later retried.

Constraints

  • Tokens may already be in flight to the client when a stop or failure occurs.
  • Closing the browser tab does not guarantee the remote model stopped generating or that billing was reversed.
  • Providers offer no universal cancellation, request-lookup, or billing-refund API.
  • Some tools are read-only, some are idempotent, and some are irreversible (sending an email, charging a card).
  • Retrying an entire failed turn without safeguards can duplicate tool effects.

What to Address

Part 1 — Protocol and Stop Flow

Compare SSE (Server-Sent Events) and WebSockets for this use case. Then trace what happens from the moment a user clicks Stop: how the signal travels from the browser through the server, the orchestrator, the model call, and any pending tool invocations.

Cover:

  • How to choose a protocol based on traffic direction and operational requirements.
  • What the server can stop immediately versus what requires provider cooperation.
  • The billing uncertainty that remains even after a well-formed cancellation request.

Part 2 — Failure Recovery and Attempt Safety

Describe how the system should handle timeouts and crashes in each layer of the agent chain. Specifically:

  • How should retries differ between a retrieval step and a side-effecting tool?
  • How do you prevent a retried turn from re-running a tool that already completed?
  • What state should be recorded for each attempt, and how is it used to fence stale responses?

A strong answer is structured, uses concrete trade-offs, and is honest about what the system cannot guarantee.

Hints

Hint 1

SSE is unidirectional by design. Think about whether the stop signal needs to travel on the same connection as the token stream, or whether a separate channel is cleaner and what that implies for auth.

Hint 2

A model call you abandon locally is not the same as a model call the provider stopped. Consider what you record about billing state and how you surface that uncertainty to the user or an ops team.

Hint 3

Give each tool invocation a stable, deterministic ID derived from the turn and attempt. Before executing, check whether that ID already completed successfully. This is the core of idempotency fencing.