liveBook · Manning

Ai state

As explained in Build AI-Enhanced Web Apps

Application state representing information available to or maintained for an AI system, including conversational context and model-related data.

AI state is the serializable data an AI application keeps for model interactions and server-side work. In a chat application, it can hold conversation history, timestamps, conversation IDs, and JSON representations of generated components. Keep it separate from UI state, which holds rendered React elements and client-side interaction state such as loading indicators. This avoids sending model context to the client merely to render the interface. section 4.2.1 section 4.2

In a React or Next.js chat flow:

  • Create an AI context provider with createAI, defining initial AI and UI states and the available server actions. section 4.2.2
  • In a server action, use getMutableAIState to read conversation history and supply it with the new user message to the model. Finalize the updated history when generation completes. section 4.2.3
  • Return the generated display to the client and update UI state for rendering. Use useActions, useUIState, and useAIState where client components need access to those actions or states. section 4.2.3
  • For persistence, save completed AI state with onSetAIState; a saved state can later be supplied as initialAIState. section 4.2.2

Provider setup for a chat

A provider can start with an empty AI conversation and a separate UI message list: const AI = createAI({ actions: { continueConversation }, initialAIState: [], initialUIState: { messages: [] }, onSetAIState: async ({ state, done }) => { if (done) await saveToDatabase(state); }, }); Here, continueConversation is the application’s server action and saveToDatabase is its persistence function. Wrap the application layout in <AI> so components can access the provider. section 4.2.2 section 4.2.3

Figure 4.8 AI and UI state representation. Once the user submits a prompt, it goes through a pipeline of events and actions for the final output to be consistent.

The flow diagram traces input from a client component through AI state and a server action, then back to updated AI and UI states; it also shows where serialization and storage fit. section 4.2.1

How Build AI-Enhanced Web Apps covers it

How deep it goes
Defines the state split and walks through provider setup, conversation updates, hooks, and persistence; it does not examine SDK internals.
Examples
Uses a chat provider with empty conversation history, client message state, a server action, and a completed-state database callback.
What it assumes
Assumes familiarity with React or Next.js application components and hooks while defining AI state and UI state explicitly.
What you can do after
Apply it via the Vercel AI SDK’s createAI and getMutableAIState to manage conversation context separately from rendered chat messages.
How it connects
Connects AI state to UI state, conversation history, server actions, React Server Components, and database persistence.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.