---
title: "Stream tokens in the UI"
lesson_id: "02"
---

# Stream tokens in the UI

- **Lesson ID:** 02
- **Goal:** Stream a Foundation Models response into the UI with LanguageModelSession.streamResponse.
- **Human lesson:** [02-stream-the-response.html](02-stream-the-response.html)

## Prerequisites

- Lesson 01, or a clear split between TTFT and total time.

## Inputs, outputs, and artifacts

- **Inputs:** A prompt and a SwiftUI screen.
- **Outputs:** Growing on-screen text.
- **Artifacts:** Progressive `.flow` diagrams and a stream sketch.

## Agent build steps

1. Create a LanguageModelSession.
2. Call streamResponse and iterate the async sequence.
3. Update visible text from each partial snapshot.
4. Mark the sketch as something to confirm in Xcode 27.
5. Link the prompt registry course.

## Constraints

Keep the language simple, friendly, and literal. iOS 27 and macOS 27 only. No quizzes, Mermaid, or em dashes. Do not invent API names. Mark sketches as sketches and tell the reader to confirm them in the Xcode 27 SDK.

## Key concepts

- Create a `LanguageModelSession`, call `streamResponse`, and bind partial text to SwiftUI.
- The framework yields snapshots. Update the UI on each yield.
- Confirm the partial-text property name in the Xcode 27 SDK.
- Show a typing status until the first token, then growing text.

## Takeaways

- Paint partial text. Do not wait for the last token to show the first word.
- Do not block the main actor on the whole stream.
- Keep instruction text in a registry, not the view.

## Acceptance checks

- streamResponse is named.
- The UI updates on each partial, not only at the end.
- The sketch is marked as a sketch.
- Sibling links, progressive `.flow` diagrams, and an Agent brief link are present.
- [ ] Proceed to [lesson 03 brief](03-measure-ttft-and-gaps.llms.md).
