Durable chat runs can span hours and many turns. You usually want:Documentation Index
Fetch the complete documentation index at: https://trigger-docs-tri-7532-ai-sdk-chat-transport-and-chat-task-s.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
- Conversation state — full
UIMessage[](or equivalent) keyed bychatId, so reloads and history views work. - Live session state — a scoped access token for the session and optionally
lastEventIdfor stream resume.
Conceptual data model
You can use one table or two; the important split is semantic:| Concept | Purpose | Typical fields |
|---|---|---|
| Conversation | Durable transcript + display metadata | Stable id (same as chatId), serialized uiMessages, title, model choice, owner/user id, timestamps |
| Active session | Hydrate the transport on page reload | Same chatId as key (or FK), publicAccessToken, optional lastEventId |
runId is optional — useful for telemetry / dashboard linking (“View this run”) but not required for resume. The Session row owns its current run server-side; the transport reads from session.out keyed on chatId, so a run swap (continuation, upgrade) is invisible to your DB schema.
Store
UIMessage[] in a JSON-compatible column, or normalize to a messages table — the pattern is when you read/write, not how you encode rows.Where each hook writes
onPreload (optional)
When the user triggers preload, the run starts before the first user message.
- Ensure the conversation row exists (create or no-op).
- Upsert session:
chatAccessTokenfrom the event (a session-scoped PAT covering bothread:sessions:{chatId}andwrite:sessions:{chatId}). - Load any user / tenant context you need for prompts (
clientData).
onChatStart when preloaded is false.
onChatStart (turn 0, non-preloaded path)
- If
preloadedis true, return early —onPreloadalready ran. - Otherwise mirror preload: user/context, conversation create, session upsert.
- If
continuationis true, the conversation row usually already exists (previous run ended or timed out); only update session fields so the new PAT andlastEventIdare stored.
onTurnStart
awaitpersistuiMessages(full accumulated history including the new user turn) before the hook returns —chat.agentdoes not begin streaming untilonTurnStartresolves, so this is what bounds “user message is durable before the stream”.
onTurnComplete
- Persist
uiMessagesagain with the assistant reply finalized. - Upsert session with the fresh
chatAccessTokenandlastEventIdfrom the event.
lastEventId lets the frontend resume without replaying SSE events it already applied. Treat it as part of session state, not optional polish, if you care about duplicate chunks after refresh.
Token renewal (app server)
The persisted PAT has a TTL (seechatAccessTokenTTL on chat.agent, default 1h). When the transport gets a 401 on a session-PAT-authed request, it calls your accessToken callback to mint a fresh PAT — no DB lookup required, since the session is keyed on chatId (which the transport already has).
Your accessToken callback typically just wraps auth.createPublicToken:
onSessionChange callback fires every time the cached PAT changes — persist the new value there.
No Trigger task code needs to run for renewal.
Minimal pseudocode
Alternative: hydrateMessages
For apps that need the backend to be the single source of truth for message history — abuse prevention, branching conversations, or rollback support — use hydrateMessages instead of relying on the frontend’s accumulated state.
With hydration, the hook loads messages from your database on every turn. The frontend’s messages are ignored (except for the new user message, which arrives in incomingMessages):
onTurnStart persistence pattern — the hook handles both loading and persisting the new message in one place.
Design notes
chatIdis stable for the life of a thread and is the only identifier the transport persists. Runs come and go (idle continuation, upgrade, cancel/restart) but the chat keeps its identity.continuation: truemeans “same logical chat, new run” — refresh the persisted PAT, don’t assume an empty conversation.- The current
runIdis available on every hook event for telemetry / dashboard linking (“View this run”), but you don’t need to persist it for resume to work — the transport addresses bychatId. - Keep task modules that perform writes out of browser bundles; the pattern assumes persistence runs in the worker (or your BFF that the task calls).
See also
- Backend — Lifecycle hooks
- Session management —
resume,lastEventId, transport chat.defer()— non-blocking writes during a turn- Code execution sandbox — combines
onWait/onCompletewith this persistence model

