---
name: Stream tokens and measure speed
description: >-
  Teach an Edge FDE to stream Foundation Models tokens on iOS 27 and macOS 27,
  then measure time to first token and inter-token latency for on-device and
  Private Cloud Compute sessions.
---
# Stream tokens and measure speed

Use this course when a reader needs the app to feel fast and to prove that feeling with numbers. Users judge speed from the first token, not from total generation time. Target iOS 27 and macOS 27 only.

## End-to-end agent recipe

1. Start with [lesson 01](01-why-first-token-matters.llms.md). Separate time to first token from total time.
2. Use [lesson 02](02-stream-the-response.llms.md) for `LanguageModelSession.streamResponse` and a live SwiftUI update.
3. Use [lesson 03](03-measure-ttft-and-gaps.llms.md) to clock TTFT and inter-token latency around the stream.
4. Use [lesson 04](04-on-device-vs-pcc.llms.md) to compare `SystemLanguageModel` and `PrivateCloudComputeLanguageModel`.
5. Use [lesson 05](05-instruments-and-budget.llms.md) for the Foundation Models instrument in Xcode 27 and a simple speed budget.
6. Keep these links exact: [Trace every step of an AI call](../on-device-llm-tracer/), [Test AI behavior before you ship](../on-device-eval-harness/), [Core AI vs Core ML vs MLX](../core-ai-vs-core-ml-vs-mlx/), and [Version prompts like product copy](../on-device-prompt-registry/).
7. Cite [LanguageModelSession.streamResponse](https://developer.apple.com/documentation/foundationmodels/languagemodelsession/streamresponse(options:prompt:)), [WWDC26 session 243](https://developer.apple.com/videos/play/wwdc2026/243/), and [WWDC26 session 319](https://developer.apple.com/videos/play/wwdc2026/319/) when a source is needed.
8. Use the human HTML pages for the reader. Use the `.llms.md` briefs for agent steps and acceptance checks.

## Constraints

- Plain spoken English for a non-native reader.
- Literal sentences. No flourish, no quizzes.
- Do not use em dashes.
- Use no Mermaid. Build progressive `.flow` diagrams in HTML.
- Do not invent OS versions. This course is iOS 27, macOS 27, and Xcode 27.
- Mark Swift sketches as sketches. Confirm names in the installed SDK. Do not use Python for stream clocks.
- A Dynamic Profile switch can invalidate the KV cache and raise TTFT. Say that. Do not invent a cache API.

## Key concepts

- Users judge speed from the first token, not total generation time.
- Stream with `LanguageModelSession.streamResponse` and paint partial text in SwiftUI.
- Clock TTFT and inter-token gaps with `ContinuousClock`.
- Compare `SystemLanguageModel` on-device with `PrivateCloudComputeLanguageModel`.

## Takeaways

- You can stream tokens into the UI and prove speed with TTFT, gaps, and total time.
- You can log the same four numbers for on-device and PCC with a model field.
- You can confirm in-app clocks in the Xcode 27 Foundation Models instrument.

## Acceptance checks

- Every human lesson HTML has **Key concepts** and **Takeaways** sections before lesson-nav. The course home has the same two headings for the whole course.
- Five human lessons exist with the requested basenames.
- Five matching `.llms.md` briefs exist and link to their human HTML pages.
- Every human lesson has its Agent brief link and sibling-course links.
- The course index lists all five lessons, the course SKILL.md, and all five briefs.
- Diagrams are progressive HTML `.flow` elements. There is no Mermaid, no em dash, and no quiz.

## Environment setup

No runtime environment is required. The short code samples are sketches of public APIs, not a project to run.
