A session is an agent's transcript: the messages the user sent, the agent's replies, and the tool calls the agent made along the way, with their results. Pi calls a session a conversation. The agent stores its sessions in the Durable Object's SQLite database, so a conversation is still there after the object hibernates or restarts.
A session holds what was said. The other things an agent keeps live elsewhere:
| Information | Where it lives |
|---|---|
| What the user and the agent said, and the tool calls | The session |
| Your application's data, such as settings or a cart | State |
| Instructions, memory, and skills the model is given | Context |
| Files the agent reads and writes | A Workspace |
| A reply the model is still streaming | The stream, until the reply finishes (refer to Messages that are not stored yet) |
Each model SDK has its own message shape, and a session stores messages in one of them. When you choose a harness, you also choose the message format:
| Harness | Where the transcript lives | Message format |
|---|---|---|
| Think | Sessions from agents/sessions |
AI SDK UIMessage |
AIChatAgent |
Sessions from agents/sessions |
AI SDK UIMessage |
| Pi | The conversation store built into Pi Durable | Pi AI messages, as Pi transcript entries |
| OpenCode | OpenCode's own session storage | OpenCode messages |
No harness: your own model loop, calling the AI SDK and Sessions directly |
Sessions from agents/sessions |
AI SDK UIMessage |
A Think session and a Pi session store tool calls, reasoning, and model usage differently, and neither harness can read the other's transcript. If you later move a conversation to another harness, you convert each message, the way Pi's seeded sessions write an imported history as Pi entries.
Each harness has its own API for sessions. The rest of this page covers what they have in common.
A message has an ID, a role (user, assistant, or a tool result), and its content: text, reasoning, tool calls, and files. Each message points to the message before it. When a user regenerates a reply or edits an earlier message, the new message points to the same parent as the old one, and the conversation branches:
user: Plan a trip to Lisbon
└─ assistant: Here is a 3-day plan...
└─ user: Make it 5 days
├─ assistant: Here is a 5-day plan... (first attempt)
└─ assistant: A 5-day plan, slower pace... (regenerated, latest)Regenerating keeps the earlier reply, so a client can offer both. The path from the first message to the latest reply is the conversation the model sees. In Sessions you can read the path to any other reply too. Pi forks a session into a new one that sees the history up to that point.
The latest reply can still change after it is stored. When a turn continues after an interruption, the harness extends the same reply. Before the next turn, Think and AIChatAgent record an error result for any tool call that never got one, so the model never sees a call without an answer.
A tool call is a part of the assistant message that made it. The part records the input, then the output or the error. A tool that needs a person's approval stops with its part waiting for approval, and the reply has no result for that call yet. Because the waiting call is part of a stored message, a pending approval is still there after the Durable Object restarts, and the turn continues once someone answers.
The harness writes a reply to the session when the reply finishes. While the model is still streaming, clients receive the reply from the stream, and a browser tab that reconnects resumes the stream where it left off. If the object restarts in the middle of a reply, Think and AIChatAgent recover the turn instead of reading a half-written message from the session. Refer to Durable recovery.
Sessions stores the message you append with two changes: it removes provider metadata that must not be replayed, such as OpenAI's encrypted reasoning, and it drops reasoning parts that are empty. A message larger than one SQLite row is split across several rows and read back whole. Images, audio, and PDFs that a message carries inline are stored apart from the message row and put back, byte for byte, when the message is read.
A browser tab that connects to the agent receives the session's messages, then an update each time a message is added or changes. Several tabs on the same agent see the same session.
A sub-agent has its own session. The parent's transcript holds the tool call that started the sub-agent and the result it returned, not the sub-agent's conversation.
A long conversation can hold more than an isolate should load at once. A harness reads a session as a stream, a few messages at a time, or asks for a window: the most recent messages that fit in a byte budget. The newest message is always in the window, and the result says whether older messages were left out. Think loads a window when the agent starts, sized by its hydration budget.
When a conversation grows too long for the model, compaction summarizes a range of older messages. Sessions stores the summary as an overlay on that range. When it reads the path, it puts the summary in place of the messages the overlay covers:
Stored: [1] [2] [3] [4] [5] [6] [7] [8] [9]
Read: [1] [2] [summary of 3-6] [7] [8] [9]Sessions never stores the summary as a message, so a client that posts its transcript back after a compaction cannot write the summary into the session. The original messages stay stored, and full-text search over the session still finds them. Compaction usually keeps the first messages and a recent tail word for word, and never separates a tool call from its result. You decide when it runs: when an estimated token count crosses a threshold, or when your code asks for it.
Compaction does not delete anything and does not make storage smaller. The original messages stay in the Durable Object's SQLite database, where search can find them, and count against its storage. To remove what a user said, delete the messages or clear the session:
SessionshasdeleteMessages()andclearMessages().- Think has
clearMessages(). In Think andAIChatAgent, a client clears the conversation withclearHistory()fromuseAgentChat. - Pi's
reset()starts a new context, but the entries from before the reset stay stored and readable.
The harness builds each model request from the session, and the request is usually smaller than the session. To build it, the harness reads the path or a window of it with overlays applied, shortens what the model does not need in full, such as large tool outputs in older messages, converts the messages to the provider's format, and puts the system prompt in front. Building a request never changes what is stored: when the harness shortens a tool output for the model, the full output stays in the session.
Some of these steps change the start of the next request, which loses the provider's prompt cache from that point. Context explains how the cache works and what breaks it.
For a chat application, give each conversation its own Durable Object, so one busy conversation does not slow down the others. Think and AIChatAgent hold one session per agent. Sessions, Pi, and OpenCode let you keep several sessions in one object, for drafts or side threads.
- Context: what the model is told besides the conversation, and prompt caching.
Sessions: store AI SDK messages in a Durable Object.- Pi sessions and OpenCode sessions: each harness's session API.
- Think sessions: compaction, storage, and prompt caching in Think.