The Vercel AI SDK · Chapter 14 of 21
Saving chats
By the end of this chapter a conversation with Mali survives a reload. You will keep each chat in a store under its own id, have the route load the chat, add the new message and save the answer, have the page open a saved chat and send only what is new, and measure how much a long chat can be trimmed before it goes to the model. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The chat UI
- 4 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of The Vercel AI SDK are free. This one comes with the all labs plan.
Why you need this
Deepa forwarded a complaint: a customer asked Mali about her order, checked delivery to her pincode, then her phone locked. When she opened the website again, the chat was empty, and Mali had no idea what they had talked about. She had to start over, and she was not pleased.
Today the whole chat lives in one place: useChat's state, in the browser tab. A reload, a second tab, a phone that dropped the page: the chat is gone. And every message sends the whole chat again from the browser, tool results and all, so the request grows every turn. Farhan says the chat belongs on the server: give it an id, keep its messages in a store, and let the browser send only what is new.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
store.ts: a store for chats
Export createChat() (a new id from generateId(), its file holding []), loadChat(id) (the saved messages, or [] when there is no file) and saveChat(id, messages), keeping each chat as JSON in .chats/<id>.json next to store.ts. Refuse, with an Error, any id that is not only letters, digits, _ and -.
The Check creates a chat, saves two messages and reads them back, loads an id that was never saved, and tries ../escape and ../../shared/mali as ids.
- Task 2
server.ts: the route loads and saves the chat
Export POST that reads { id, message }, answers 400 for a bad id, a missing message or a body that is not JSON, loads the chat with loadChat, adds the message, checks them all with safeValidateUIMessages and maliTools() (400 if not valid), streams Mali's answer as in chapter 9, and in toUIMessageStream passes originalMessages, generateMessageId: createIdGenerator({ prefix: 'msg', size: 16 }), and an onEnd that saves the messages with saveChat.
The Check sends three messages of one chat, one at a time: the saved file grows to 2, 4 and 6 messages, the answer's id in the stream is the id in the file, the follow-up And on Sundays? is understood from the saved chat, and the saved chat validates with its tool part.
- Task 3
Chat.tsx: open a saved chat, send only the new message
Give chapter 9's chat the props { id, initialMessages }, pass them to useChat as id and messages, and give DefaultChatTransport a prepareSendMessagesRequest that sends { body: { id, message: messages.at(-1) } }.
The Check opens the page with saved-chat.json and the id chat-kavya, wants the saved chat on the page at once, then sends a message and wants one request whose body has the id and only the new message, and the answer on the page.
- Task 4 · interview question
Interview: trim.ts, what does trimming save?
Turn saved-chat.json into model messages, add Can I pay by card?, and ask it twice with generateText (Mali, maliTools(), isStepCount(5)): the whole chat, then pruneMessages({ messages, toolCalls: 'before-last-message' }). Print:
whole: N messages, IN tokens in
pruned: N messages, IN tokens in
saved: D tokens (P%)
The Check reads the two requests and wants your counts to equal them.
Vikram will ask: what did pruning take out, and when would leaving it out make Mali answer worse?
What you will learn
- Chat ids, and a store that keeps each chat's messages.
- Saving on the server when the answer ends: originalMessages and onEnd.
- Message ids that stay the same from the stream to the store: generateMessageId.
- Loading a saved chat on the page: useChat's id and messages.
- Sending only the last message: prepareSendMessagesRequest.
- Checking what you load with safeValidateUIMessages, and trimming with pruneMessages.
More in The chat UI
Build "Saving chats" yourself
HariBhari, a Bengaluru plant shop, wants Mali, a plant helper on its website. You build it end to end, from the first call to a chat that streams cards, asks before it adds to the cart and stays within budget. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of The Vercel AI SDK are free. This one comes with the all labs plan.