---
title: "Train a LoRA and export Core AI"
lesson_id: "04"
---

# Train a LoRA and export Core AI

- **Lesson ID:** 04
- **Goal:** Train, merge, and export a Core AI .aimodel in Python. Load it on device in Swift. Deny runtime LoRA attach. Separate system-model evals from custom-model evals.
- **Human lesson:** [04-lora-and-export.html](04-lora-and-export.html)

## Prerequisites

- Lesson 03, or a reviewed training file.

## Inputs, outputs, and artifacts

- **Inputs:** Reviewed JSONL and a small instruct base.
- **Outputs:** A baked .aimodel plus tokenizer, and a Swift load sketch.
- **Artifacts:** Progressive `.flow` diagrams, Python train/merge, bash export, Swift load.

## Agent build steps

1. Point at the writing-style LoRA course for the long form.
2. Show PEFT train and merge in Python.
3. Name merge then coreai.llm.export.
4. Show Swift on-device load with placeholder names to confirm in the SDK.
5. Deny hot-swap LoRA.
6. Separate Evaluations on Foundation Models from Core AI evals.
7. Link pytorch-core-ai-xcode.

## 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. Mac train/export is Python. Device load is Swift.

## Key concepts

- Train a LoRA on a Mac with PEFT, merge it, then export with `coreai.llm.export`.
- Swift loads the baked `.aimodel`. There is no runtime LoRA attach to the system model.
- Foundation Models Evaluations grade the system model. Your Core AI model needs its own evals.
- Train a LoRA when the same error class survives prompt version changes.

## Takeaways

- No documented hot-swap LoRA. Bake, then ship.
- Prompt-only fixes are cheaper than a bake.
- Confirm export flags with `--help`. Do not invent a `--model` flag.

## Acceptance checks

- Python train and merge are shown.
- Export path is named.
- Swift load is shown.
- Hot-swap is denied.
- Sibling links, progressive `.flow` diagrams, and an Agent brief link are present.
- [ ] Proceed to [lesson 05 brief](05-privacy-limits.llms.md).
