---
title: "Storing Vercel's AI SDK chat messages in a Turso database"
description: "Vercel AI SDK's useChat hook doesn't persist conversations, so chat history can be stored in a Turso (libSQL) table and reloaded on page render."
url: "https://swe.quest/articles/storing-vercel-s-ai-sdk-chat-messages-in-a-turso-database-14cd5acbb753b2cf"
canonical: "https://turso.tech/blog/storing-vercels-ai-sdk-chat-messages-in-a-turso-database"
source: "Turso"
published: "2024-08-07"
difficulty: "medium"
kind: "How-to"
reading_minutes: 5
tags: ["sqlite","llms","react","typescript"]
---

# Storing Vercel's AI SDK chat messages in a Turso database

A post on [Turso](https://swe.quest/sources/turso.md), published 2024-08-07, 5 min read, How-to. Original: [https://turso.tech/blog/storing-vercels-ai-sdk-chat-messages-in-a-turso-database](https://turso.tech/blog/storing-vercels-ai-sdk-chat-messages-in-a-turso-database)

## Summary

Vercel AI SDK's useChat hook doesn't persist conversations, so chat history can be stored in a Turso (libSQL) table and reloaded on page render. A server component queries the messages table and passes rows as initialMessages to useChat. The API route inserts the user's last message before calling streamText, and an onFinish callback inserts the assistant's reply. Suggested extensions include a chats/sessions table with foreign keys, background persistence via server actions, or a database per user or chat.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [SQLite](https://swe.quest/tags/sqlite.md) (Data & databases)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [React](https://swe.quest/tags/react.md) (Web & mobile)
- [TypeScript](https://swe.quest/tags/typescript.md) (Languages)

## Prerequisites

- Next.js App Router ([easy posts on React](https://swe.quest/tags/react.md?difficulty=easy))
- React hooks ([easy posts on React](https://swe.quest/tags/react.md?difficulty=easy))
- basic SQL ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))

## Keywords

Turso, Vercel AI SDK, useChat, libSQL, Next.js, streamText, OpenAI

## Related posts

- [Building an AI-powered ChatBot using Vercel, OpenAI, and Postgres](https://swe.quest/articles/building-an-ai-powered-chatbot-using-vercel-openai-and-postgres-4fd423473384581f.md): Neon · 2023-03-09 · Medium · 13 min read · RAG & retrieval, PostgreSQL, Vector search, LLMs, Serverless
  A ChatGPT-style documentation assistant (Ask Neon) is built by storing OpenAI text-embedding-ada-002 vectors in Postgres with pgvector, retrieving context by cosine distance, and feeding it to text-davinci-003 from a Vercel Edge Function.
- [Making a Real-Time Chat App with Supabase on Replit](https://swe.quest/articles/making-a-real-time-chat-app-with-supabase-on-replit-0a77c4de25e9cc9d.md): Replit · 2022-03-23 · Medium · 8 min read · Real-time systems, Frontend, JavaScript, PostgreSQL
  A real-time public chat app can be built with a Vue 3 and Tailwind CSS frontend on Replit backed by Supabase's hosted Postgres.
- [Save Resend email events to your Turso Database](https://swe.quest/articles/save-resend-email-events-to-your-turso-database-e416b48a04ff4cae.md): Turso · 2024-07-23 · Medium · 7 min read · SQLite, Communications, JavaScript, TypeScript
  Resend email webhook events (sent, delivered, bounced, opened, clicked, etc.) can be persisted to a Turso database through a small Hono/Node.js handler using @libsql/client.
- [Toasty now fully supports Turso](https://swe.quest/articles/toasty-now-fully-supports-turso-b9805f71f7e75171.md): Turso · 2026-10-07 · Medium · 5 min read · Rust, SQLite, Local-first & sync, Serverless, Databases
  Toasty, the async Rust ORM from Tokio's creator, now supports Turso in three modes behind one API: local files with optional concurrent writes (transactions automatically wrapped in BEGIN CONCURRENT), a serverless remote mode over HTTP with URL and auth token, and local-first sync with explicit pull/push calls.
- [Bringing SQLite to Vercel Functions with Turso](https://swe.quest/articles/bringing-sqlite-to-vercel-functions-with-turso-afed2a49c7b44020.md): Turso · 2026-02-19 · Medium · 8 min read · SQLite, Serverless, Replication, Multi-tenancy, JavaScript
  Turso's partial sync replicates a subset of SQLite database pages into Vercel Functions so reads run in-process with no network round-trip, while writes go to the remote Turso database for durability and consistency.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
