AI SDK chat persistence and compaction without a database: a port of Vercel's example
Hi all,
I've seen a lot of posts here about persisting AI SDK chat messages, tool calls and reasoning, and about how painful it is to write compaction logic and keep track of context windows.
Because of this, I thought it'd be helpful to port Vercel's chat persistence example to our stateful LLM API, Twigg.
The idea is simple. Instead of sending the whole transcript to the model each time, you send only the next event (a prompt or a tool result). The API stores the history, assembles it, compacts it if needed and sends it to whichever LLM you choose, then streams the response back to your app. On the next event, you send just that, and the API adds it to the history and makes the next call.
If you're building a chat app or agent, it removes the need to:
set up a database to store the transcript
write summarisation or compaction code for long chats
track each model's context window to know when to compact or prune
set up file and image storage if your agents use them (these are stored too)
add cache breakpoints for prompt caching
In the example, chat-store.ts is gone (because you don't need it anymore). The chat route also went from 95 lines to about 30. One current limitation is that regenerate and edit only work on the latest message.
We initially built this so people can build agents without setting up persistence infrastructure and context window logic, but keep the flexibility to code their own tools and UI. It seems to be a common issue in Vercel, so I hope it helps. I'd love to hear from anyone who's dealt with this, or answer any questions.
Example: https://github.com/Twigg-AI-Ltd/twigg-ai-sdk/tree/main/examples/no-database
More info: twigg.ai
