# Core AI Fast First Token and the Right Compute Unit: Lesson 01. Where the first-token wait goes

Lesson id: `core-ai-fast-first-token/01-where-first-token-time-goes`
Human lesson: https://edgefde.dev/courses/core-ai-fast-first-token/01-where-first-token-time-goes.html

## Goal

Split first-token time into specialization, load, setup, and inference, with Apple's tools in the lab and logs in the field.

## Prerequisites

- Xcode 27 target linking CoreAI.framework
- An exported `.aimodel` in the target

## Inputs

- Model URL
- Function name (`main` by default)

## Outputs and artifacts

- Instruments Core AI trace
- Debug gauge screenshot
- `timedLoad` function and log lines

## Steps

1. Turn off Include Model Debug Information in the scheme Diagnostics.
2. Run on device, open the Core AI debug gauge, and note median Specialization, Load, and Inference.
3. Profile with the Core AI Instruments template. Record a cold launch, then a second launch.
4. Add `timedLoad`: check `AIModelCache.default.model(for:options:)`, time `AIModel(contentsOf:options:)`, time `loadFunction(named:)`.
5. Time the first `run` call separately.
6. Log device model and OS version with each line.

## Constraints

- Swift only.
- Do not time specialization with model debug information on.

## Acceptance checks

- The second launch shows no Specialization event, or you can explain why.
- Field logs show all four timings.

## Key concepts

- Core AI work shows up as Specialization, Load, Setup, and Inference events.
- Specialization is often the slowest and happens at most once per model and options, unless cached.
- The debug gauge and the Core AI Instruments template show these events and the compute units used.
- "Include Model Debug Information" slows specialization. Turn it off before timing it.

## Takeaways

- Measure before you optimize. Log cache hit, model init, function load, and first run.
- Profile on a real device with nothing else heavy running.
- Log device model and OS version with every timing.
