Typorithm · medium

Streaming Responses

streamingllmttftevents
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Streaming Responses

0%

Context & Tokens

Streaming returns tokens as they are generated, giving responsive UIs and a lower time-to-first-token. You iterate the event stream — content_block_delta events carry the text. The final message is also available when the stream completes.

A non-streaming request waits for the entire response before returning anything. Streaming instead emits tokens as the model produces them, so the user sees output almost immediately — a much lower time-to-first-token.

You open the stream and iterate it with for await. The stream emits typed events; content_block_delta events carry the incremental text, which you append to the growing response as each chunk arrives.

When the stream finishes, the SDK still gives you the complete final message — so you can render live deltas for responsiveness and read the assembled result and usage once it is done.

01

Streaming emits tokens as they are generated.

02

Lower time-to-first-token means a responsive UI.

03

Iterate events; content_block_delta carries the text.

const stream = client.messages.stream({
  model: "claude-opus-4-8",
  max_tokens: 1024,
  messages,
});
for await (const event of stream) { /* token deltas */ }

Read the implementation

Connect each part of the code to the concept before you type.

1. Open a stream

stream returns tokens as they are generated, instead of waiting for the whole response.

const stream = client.messages.stream({

2. Output cap

max_tokens still caps the response length; streaming just delivers those tokens incrementally.

  max_tokens: 1024,

3. The messages

You pass the same messages as a normal request — only the delivery is incremental.

  messages,

4. Iterate events

You iterate the event stream; content_block_delta events carry the text as it arrives.

for await (const event of stream) { /* token deltas */ }

5. Token deltas

Each delta is a small chunk of text; concatenating them rebuilds the full response.

/* token deltas */

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Streaming Responses: Lesson & Practice · Typorithm | ApplyDjinn