SvelteKit

06 Persistence

Persist SvelteKit chat history through your app storage.

Anvia returns the new messages from each run. Your SvelteKit app decides where sessions and history live.

1. Load Existing Messages

const session = await db.chatSession.findUnique({
  where: { id: sessionId, userId: locals.userId },
  include: { messages: { orderBy: { createdAt: "asc" } } },
});

if (!session) {
  return json({ error: { code: "not_found" } }, { status: 404 });
}

2. Send With History

const response = await supportAgent
  .prompt(message)
  .messages(session.messages.map((item) => item.message))
  .send();

3. Store New Messages

await db.chatMessage.createMany({
  data: response.messages.map((message) => ({
    sessionId,
    message,
  })),
});

Only store messages after the run succeeds. If the run fails, record an application event instead of adding partial history.

4. Use Memory When The Model Should Remember

Use app storage for conversation history. Use Anvia memory when the model should recall durable facts in future sessions.

Next

Prepare runtime constraints in Deploy. Related guides: Memory, Memory and Sessions, and Agent History.