# Streaming Responses

> Send tokens to the user as they are generated so a 20-second answer feels instant, and handle the events and errors that come with it. Free notes, key...

Canonical: https://www.internhack.in/roadmaps/tracks/ai-engineer/streaming-responses
Last updated: 2026-11-28

Send tokens to the user as they are generated so a 20-second answer feels instant, and handle the events and errors that come with it.

Beginner · about 5 hours · difficulty 3/5

Learn first: [Messages APIs and Provider SDKs](https://www.internhack.in/roadmaps/tracks/ai-engineer/first-api-calls), [Async I/O and Concurrency](https://www.internhack.in/roadmaps/tracks/ai-engineer/async-programming)

Without streaming, the user stares at a spinner until the whole answer is done. With streaming, the first words appear within a second or two. **Time to first token** is the latency number users actually feel.

### How it works

Providers stream over **Server-Sent Events**: a long-lived HTTP response delivering small events such as message start, content delta (a few characters or tokens), tool call deltas, and message stop with final usage. SDKs wrap this in iterators or event handlers.

### Building a streaming path end to end

1. Your server calls the model with streaming enabled.
2. It forwards deltas to the browser, typically via SSE, a streaming fetch response or WebSockets.
3. The client appends text as it arrives and renders markdown progressively.

### Things that go wrong

- **Proxies and buffering**: some reverse proxies and serverless platforms buffer responses, killing the streaming effect. Disable buffering.
- **Mid-stream errors**: the connection can fail after partial output. Decide whether to retry, resume or show the partial answer with an error.
- **Tool calls and structured output** arrive in fragments; accumulate them before parsing.
- **Cancellation**: when the user clicks stop, abort the upstream request so you stop paying for tokens.
- Usage totals usually arrive in the final event; log them there.

### Good enough to move on

You can stream a response from a model through your own backend to a browser page, support a stop button, and log final token usage.

## Key concepts

- **Stream events and deltas**
- **Time to first token**
- **Forwarding streams via SSE**
- **WebSockets**
- **Cancellation and abort signals**
- **Mid-stream error handling**
- **Proxy buffering issues**

## Mini project

Create a FastAPI or Express endpoint that streams a model response over SSE to a simple HTML page, with a working stop button that cancels the upstream request.

## Check yourself

Why does cancelling the model request on the server matter even after the user has closed the browser tab?

## Free resources

- [Anthropic docs: streaming messages](https://docs.anthropic.com/en/api/messages-streaming) (Anthropic)
- [OpenAI docs: streaming](https://platform.openai.com/docs/guides/streaming-responses) (OpenAI)
- [MDN: Server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) (MDN)

Previous: [Messages APIs and Provider SDKs](https://www.internhack.in/roadmaps/tracks/ai-engineer/first-api-calls) · Next: [Structured Outputs and JSON Schema](https://www.internhack.in/roadmaps/tracks/ai-engineer/structured-outputs)

## More in Working with Model APIs

- [Messages APIs and Provider SDKs](https://www.internhack.in/roadmaps/tracks/ai-engineer/first-api-calls)
- [Streaming Responses](https://www.internhack.in/roadmaps/tracks/ai-engineer/streaming-responses)
- [Structured Outputs and JSON Schema](https://www.internhack.in/roadmaps/tracks/ai-engineer/structured-outputs)
- [Tool and Function Calling](https://www.internhack.in/roadmaps/tracks/ai-engineer/tool-calling)
- [Prompt Caching and Batch Processing](https://www.internhack.in/roadmaps/tracks/ai-engineer/prompt-caching-batching)
- [Rate Limits, Errors and Cost Awareness](https://www.internhack.in/roadmaps/tracks/ai-engineer/rate-limits-and-cost)
- [Running Open-Weight Models Locally](https://www.internhack.in/roadmaps/tracks/ai-engineer/open-weight-models)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [AI Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/ai-engineer)
