Back to course overview
Module 5Putting AI inside your app 40 min

Lab: Add the AI feature end-to-end

Deploy your proxy, wire the Reflect button into the detail screen, and generate real insights for a real entry.

Outcome of this lab

A working, shippable AI feature: tap Reflect on an entry and get a model-generated title, mood, and reflection — with a spinner, error handling, and a securely stored key.

Step 1 — Deploy the proxy

  1. 1Create the serverless project and add the reflect function from the lesson.
  2. 2Set ANTHROPIC_API_KEY as a secret environment variable in the dashboard.
  3. 3Deploy and confirm it works with the curl command. You should get back JSON with title, mood, and reflection.

Step 2 — Wire the app

  1. 1Add AppConfig.swift with your proxy base URL, Insights.swift, InsightsService.swift, and ReflectButton.swift.
  2. 2In EntryDetailView, show ReflectButton(entry: entry) when the entry has no aiTitle yet, and show the generated title/mood/reflection when it does.
  3. 3Build (⌘B) and fix any compile errors — paste them to your assistant if needed.

Step 3 — Try it for real

  1. 1Run the app, open an entry with a few sentences of real text, and tap Reflect.
  2. 2Watch the spinner, then the insights appear and persist. Relaunch — they're still there (they're saved on the Entry).
  3. 3Turn off Wi-Fi and tap Reflect on a fresh entry to see the failure + retry path. Turn it back on and retry.
Add your guardrails now

Before moving on: cap the entry length you send (e.g. 4,000 characters), and disable Reflect while a call is in flight. These two lines prevent both runaway costs and duplicate requests — exactly the kind of thing that bites you after launch.

bashbash
git add .
git commit -m "AI Reflect feature via secure proxy, with loading + error states"

This is the milestone lab. You now have an app that stores data locally and uses a language model through a secure backend — the full stack of a modern AI app, built by directing AI and understanding each piece.