KatInterview

Build a Scalable Streaming LLM API

System DesignModel answer
MediumModel answerSystem Designstreamingllmavailability

Last updated

Design an API that streams LLM responses, starting simple and adding reliability, overload protection, and conversation state.

Build a Scalable Streaming LLM API

Design an API service that accepts text prompts and streams back LLM-generated responses token by token. Start with a basic implementation that handles individual requests independently, then progressively add features for production scale: reliability under load, graceful degradation when dependencies fail, and conversation memory.

Initial Scope

Your first implementation does not need to remember past interactions—treat every incoming prompt as standalone. However, even this basic version must handle authentication, content filtering, request limits, and client disconnections correctly.

Key Considerations

  • The LLM backend is an external service with unpredictable response times and capacity limits
  • Clients receive tokens as they're generated and may abandon requests mid-stream
  • System resources are bounded; define what happens when capacity is exceeded
  • Security and content safety apply to every request

Questions You Should Explore

  • What format and size restrictions apply to prompts and responses?
  • What performance targets (p50, p99 latency) and uptime guarantees are needed?
  • What compliance requirements govern content filtering and data retention?
  • If you later add conversation memory, must users access history across multiple devices?

Part 1: Core Request Flow

Design the basic architecture for a single stateless request. Explain how a prompt moves through the system from API entry to streamed response, including validation, safety filtering, model invocation, and termination handling.

Your design should address:

  • User authentication, rate limiting, prompt validation, and tracing
  • An orchestration layer that calls the model service and pipes streamed tokens back
  • Handling client disconnects and duplicate requests after partial responses

Part 2: Production Readiness

Extend your design for high throughput and resilience. Cover traffic management when approaching capacity limits, strategies for handling model service outages, multi-region deployment, and architectural changes needed to support persistent conversation history.

Your design should address:

  • Request prioritization, queue management, shedding load before saturation
  • Timeout policies, fallback behaviors, and avoiding dangerous retries
  • Consistency and ordering requirements once conversation state is introduced
  • Isolating blast radius of failures

Part 3: Observability and Testing

Define the metrics and tests that reveal whether problems originate in API handling, orchestration logic, or the underlying model service.

Your design should address:

  • Latency breakdowns (first token, full response), throughput, error types, abandonment rate
  • Resource utilization, safety filter outcomes, dependency status, unit economics
  • Scenarios to validate: traffic spikes, model slowdowns, abrupt disconnections, retry storms, regional outages

Hints

Hint 1

Start with a synchronous path for one request before adding queues or state

Hint 2

Distinguish between problems you can retry safely versus those requiring user intervention

Hint 3

Separate metrics for each system boundary: API gateway, orchestrator, model backend