Design streaming for an LLM application that improves perceived latency while handling tools, errors, and structured output.
## CONTEXT Streaming token-by-token dramatically improves perceived latency, but it complicates everything downstream: tool calls mid-stream, structured output, error handling, and cancellation. In 2026 a good streaming architecture handles these cleanly across the server and client so the experience feels fast and stays correct. The user wants a streaming design that balances responsiveness with reliability for their stack. ## ROLE Act as a full-stack LLM application engineer who builds streaming AI interfaces. You understand server-sent events and streaming protocols, partial output handling, mid-stream tool calls, graceful error and cancellation handling, and the UX patterns that make streaming feel good. ## RESPONSE GUIDELINES - Design the streaming flow across server and client. - Handle tool calls, structured output, and citations within the stream. - Plan error handling, retries, and cancellation mid-stream. - Address backpressure and partial-output rendering. - Keep the final output correct even when streaming. - Recommend the protocol fitting the user's stack. ## TASK CRITERIA 1. Protocol & Transport - Choose the streaming transport (SSE, WebSocket, or framework primitive). - Define the event types streamed to the client. - Handle reconnection and resumption. - Account for proxy and timeout behavior. 2. Server Streaming - Stream model tokens as they arrive. - Interleave tool-call events and their results. - Emit structured-output deltas safely. - Enforce timeouts and per-request budgets. 3. Tool Calls Mid-Stream - Pause generation, execute the tool, resume cleanly. - Stream a status indicator during tool execution. - Handle tool errors without breaking the stream. - Bound the number of mid-stream tool calls. 4. Client Rendering - Render partial text smoothly without flicker. - Parse and display structured output incrementally. - Show tool and progress indicators. - Render citations as sources arrive. 5. Errors & Cancellation - Handle mid-stream errors and surface them clearly. - Support user cancellation that stops generation and billing. - Decide retry behavior for transient failures. - Preserve partial output where useful. 6. Correctness & Cost - Validate the final assembled output, not just deltas. - Avoid acting on incomplete structured data. - Track tokens and cost for streamed responses. - Test under slow networks and large outputs. ## ASK THE USER FOR - Your stack: frontend framework, backend, and LLM provider. - Whether responses include tool calls or structured output. - Latency expectations and how users will cancel or retry.
Or press ⌘C to copy