ai-persistence

Implements durable chat and generation state persistence for TanStack AI applications.

3.1k|316|Updated Oct 8, 2025
One-click install
npx skills add https://github.com/TanStack/ai --skill ai-persistence
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ai-persistence
Source: https://github.com/TanStack/ai/tree/main/packages/ai-persistence/skills/ai-persistence
Command: npx skills add https://github.com/TanStack/ai --skill ai-persistence

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @tanstack/ai-persistence, @tanstack/ai.

What problem does it solve?

TanStack AI conversations and generated media disappear on reloads, server restarts, and multi-device use unless you persist them. This Skill guides you through wiring server-side chat persistence (withPersistence), client-side persistence, and the store contracts so conversations, runs, interrupts, and generated artifacts survive across sessions and devices.

Core Features & Use Cases

  • Server chat persistence: Wire withPersistence middleware with MessageStore, RunStore, InterruptStore, and MetadataStore contracts against your own database (Postgres, SQLite, D1, Mongo, Prisma, Drizzle).
  • Generation artifact storage: Persist generated image, audio, and video bytes with ArtifactStore and BlobStore pairs, including R2/D1 recipes for Cloudflare Workers and range-request serving.
  • Adapter recipes and conformance: Follow per-stack recipes (Drizzle, Prisma, Cloudflare, custom) and validate any adapter with the runPersistenceConformance testkit.
  • Use Case: A production chat app needs conversations to survive server restarts and work across devices. Use this Skill to implement the four chat stores against the app's existing Postgres client, wire withPersistence into the chat route, and verify idempotency invariants with the conformance suite.

Quick Start

Ask the AI to add TanStack AI server-side chat persistence to your app using the database it already runs, wiring withPersistence into the chat route.

Frequently Asked Questions about ai-persistence

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add server-side chat persistence to a TanStack AI app?

Implement the MessageStore, RunStore, InterruptStore, and MetadataStore contracts against your existing database, assemble them with defineAIPersistence, and pass the result to the withPersistence middleware in your chat route. The package ships contracts and an in-memory reference backend, not a database adapter.

What is the difference between delivery durability and state persistence in TanStack AI?

Delivery durability reconnects a client to a stream still running, using memoryStream or @tanstack/ai-durable-stream. State persistence saves the conversation itself via withPersistence and the store contracts. They share no code, and production apps often use both.

Does @tanstack/ai-persistence include a database backend?

No, it ships only the store interfaces, the middleware, an in-memory reference backend, and a conformance testkit. You implement the stores against whatever database you already run, such as Postgres, SQLite, D1, or MongoDB, and own the schema and migrations.

Why does withPersistence reject my persistence object?

The factory must be annotated with a named shape like ChatPersistence, not the bare all-optional AIPersistence type, or withPersistence rejects it because stores.messages may be undefined. Also note stores accepts only messages, runs, interrupts, and metadata keys.

How do I persist generated images or audio from TanStack AI?

Provide both an ArtifactStore for metadata and a BlobStore for bytes to withGenerationPersistence; the middleware stores media at blob key artifacts/<runId>/<artifactId>. Recipes cover R2 plus D1 on Cloudflare Workers, with sketches for S3, GCS, Vercel Blob, and Supabase.

How do I verify a custom persistence adapter is correct?

Run the runPersistenceConformance testkit from @tanstack/ai-persistence/testkit against a throwaway database. It checks full-overwrite saveThread, insert-if-absent createOrResume and interrupt create, list ordering, and composite-key handling; declare intentional omissions with skip or skipMethods.