Level 4: Persistence — Briefing
Save, load, and resume chats — without persistence your AI system loses its memory on every reload. In this level you’ll learn how to capture results with
onFinish, manage chat sessions via IDs, persist messages in a database, and validate incoming messages.
Skill Tree
Section titled “Skill Tree”What You’ll Learn
Section titled “What You’ll Learn”- On Finish Callback — Automatically capture and save results of an LLM call before the response is returned
- Chat ID — Identify sessions so your backend knows which chat a message belongs to
- Persistence — Save messages to a database and reload them on page refresh
- Message Validation — Validate incoming messages against a schema before they are saved or sent to the LLM
Why This Matters
Section titled “Why This Matters”Every chatbot project starts in memory — and that feels fine. Until someone reloads the page and the entire conversation history is gone. Or until you have no overview of which chats are running and what they cost. Or until malformed messages corrupt your database.
The concrete problem: Without persistence your chat is a goldfish — no memory beyond the current request. Users lose their history, you lose insights into token usage, and your system has no foundation for features like chat history, analytics, or debugging.
Prerequisites
Section titled “Prerequisites”- Level 1: AI SDK Basics —
generateText,streamText, the Result object, and callbacks must be solid - TypeScript fundamentals — async/await, interfaces, Map/Object
- Basic understanding of APIs — Request/Response, JSON body
- Project directory: Continue working in the same project directory from Level 1 (where
ai,@ai-sdk/anthropic, andzodare already installed)
Skip hint: Already building chat systems with database connections, chat IDs, and message validation? Jump straight to the Boss Fight and test whether you’ve mastered the complete persistence cycle.
Challenges
Section titled “Challenges”Boss Fight
Section titled “Boss Fight”Build a persistent chat with reload: A system that generates a chat ID, saves messages, loads the history after a simulated reload, and seamlessly resumes the conversation. You’ll combine all four building blocks from this level in one project.