---
name: llm-lora-writing-style lesson 06
description: >-
  Agent recipe for lesson 06: load the baked instruct model and tokenizer in
  Xcode 27 and run on-device chat with current Core AI language APIs.
---
# Build on-device chat

**Lesson id:** `06`  
**One-line goal:** Prepare the merged Core AI language model once and return a local reply on the target device.

## On-device job

Writing style, privacy, and latency are proven only when the device answers from the baked artifact with no adapter download.

## Prerequisites

Lesson 05 bundle. Xcode 27. Metal Toolchain. Core AI framework.

## Inputs

`.aimodel`, tokenizer sidecar, review prompts, platform preset.

## Outputs and artifacts

A SwiftUI chat screen, a local reply, and a note with latency, device, checksum, and `runtime_lora_swap: false`.

## Executable steps

1. Confirm Xcode with `xcode-select --install` and `xcodebuild -version`.
2. Add Core AI, the Metal Toolchain, the `.aimodel`, and the tokenizer to the target.
3. Prepare the documented language model and session once at launch.
4. Generate off the main UI path. Keep prompts inside the context budget.
5. Compare against a base-model build. Verify API names in the installed SDK.

## Constraints

Illustrative Swift only. No runtime LoRA load. No CUDA, quizzes, Mermaid, or em dashes.

## Failure modes

Missing tokenizer, prepare-on-send, copied placeholder types, Mac-only judgment, adapter-slot loader.

## Key concepts

- Style counts when the phone answers with no network.
- Load the baked `.aimodel` and tokenizer once. Prepare in `.task` or app setup.
- Generate off the main UI path. Measure latency and memory.
- Swift class names in the lesson are illustrative. Check the installed SDK docs.

## Takeaways

- A missing tokenizer sidecar makes output look like the wrong model.
- Test a short prompt, a long prompt, a two-turn rewrite, and an out-of-training request.
- A second writing style is a second bake.

## Done when

Prepare succeeds once. A review prompt returns a local reply with recorded latency. The note says no runtime LoRA was loaded.

**Human lesson:** [Build on-device chat](06-on-device-chat.html)
