AI State Management Advanced
As your AI app grows, you need to manage multiple conversations, persist message history, handle optimistic updates, and coordinate state across components. This lesson covers the patterns and tools for managing AI state at scale.
Multi-Conversation Support
Use the id parameter to manage multiple independent conversations:
import { useChat } from '@ai-sdk/react'; function ChatWindow({ conversationId }: { conversationId: string }) { const { messages, input, handleInputChange, handleSubmit } = useChat({ id: conversationId, // Unique ID per conversation api: '/api/chat', body: { conversationId }, // Send to server for context initialMessages: [], // Load from storage if needed }); // Each conversationId gets its own message state return ( /* ... render chat ... */ ); }
Persisting Conversations
Save conversation history to localStorage or a database:
import { useChat } from '@ai-sdk/react'; import { useEffect } from 'react'; export function usePersistedChat(id: string) { const stored = localStorage.getItem(`chat-${id}`); const initialMessages = stored ? JSON.parse(stored) : []; const chat = useChat({ id, api: '/api/chat', initialMessages, onFinish: () => { // Save after each completed response localStorage.setItem(`chat-${id}`, JSON.stringify(chat.messages)); }, }); return chat; }
Conversation List with Context
import { createContext, useContext, useState, ReactNode } from 'react'; interface Conversation { id: string; title: string; } interface ChatContextType { conversations: Conversation[]; activeId: string | null; createConversation: () => void; selectConversation: (id: string) => void; } const ChatContext = createContext<ChatContextType>(null!); export function ChatProvider({ children }: { children: ReactNode }) { const [conversations, setConversations] = useState<Conversation[]>([]); const [activeId, setActiveId] = useState<string | null>(null); function createConversation() { const id = crypto.randomUUID(); setConversations(prev => [...prev, { id, title: 'New Chat' }]); setActiveId(id); } return ( <ChatContext.Provider value={{ conversations, activeId, createConversation, selectConversation: setActiveId }}> {children} </ChatContext.Provider> ); } export const useChatContext = () => useContext(ChatContext);
Trimming Message History
State Management Complete!
You can now manage complex AI state across your React app. In the final lesson, learn production best practices.
Next: Best Practices →Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX