---
title: "Embed the question on the device"
lesson_id: "02"
---

# Embed the question on the device

- **Lesson ID:** 02
- **Goal:** Embed the user question on device with one documented Core AI or Foundation Models path.
- **Human lesson:** [02-embed-the-question.html](02-embed-the-question.html)

## Prerequisites

- Lesson 01.
- Xcode 27, iOS 27 or macOS 27.

## Inputs, outputs, and artifacts

- **Inputs:** A trimmed question string.
- **Outputs:** A float32 vector with a recorded dimension and model name.

## Agent build steps

1. Add Foundation Models and Core AI to an Xcode 27 app.
2. Look up the installed embedding API. Prefer Foundation Models if it exists, else a Core AI catalog embedder.
3. Prepare the embedder once at launch.
4. Refuse a hosted embed API.
5. Record model name and vector length next to the cache.

## Constraints

Illustrative Swift only. No public cloud embed. No em dashes. iOS 27 and macOS 27 only.

## Key concepts

- Turn the question into an on-device vector with Core AI or a documented Foundation Models helper.
- Keep the same embedder, vector length, and float32 type for the life of the cache.
- Prepare the embedder once at launch. First load is slower.
- Confirm embedder type names in the installed Xcode 27 SDK.

## Takeaways

- Never call a hosted embed API for the cache.
- Write the model name and dimension next to the cache file.
- Mixing embedders breaks similarity scores.

## Acceptance checks

- Embedder is prepared once.
- Hosted embed is rejected.
- Dimension and model name are stored.
- Agent brief link is present.
