Conversation history
Pass aConversation to run. The run loads the conversation from the object’s state when it starts, and saves its new messages while it runs and when it ends:
- Each object key has its own conversation, so
ChatSession/bobandChatSession/aliceare separate. - Restate runs one exclusive handler per key at a time, so the runs of a session never interleave.
- A shared handler such as
historycan read the turn that is running. See Saving progress. - Read the history from a shared handler with
page(from, limit), which returns up tolimitmessages (default 100, at most 1000) andnext, the offset of the following page. For afromthat isn’t a nonnegative integer, or alimitthat isn’t an integer from 1 to 1000,pagethrows aTypeError, which Restate retries, so reject invalid values from clients with aTerminalErrorbefore you callpage. Callhistorywith{}for the first page, then passnextasfromin subsequent requests until the page is empty. state.preloadkeeps Restate from sending the whole conversation to each invocation. See Lazy state.
Saving progress
A run saves to its conversation at these points:- After each model call that requests tool calls. The run saves the calls before they run, together with the results of the previous calls. While the tools run, the saved history ends with calls that have no results yet.
- After each accepted steering message. A message that arrives while tool calls run is saved with the next save, after the results of those calls.
- When the run ends, with the final answer. A stopped or interrupted run saves its messages too.
page returns the history as of the last save. To show tool results as they happen, use progress events.
Error notes and answers that afterRun rejects are never saved.
Failed runs
When a terminal error fails the run, including a cancellation of the invocation, the run saves what it recorded before it rethrows the error:-
Each unfinished tool call gets a
cancelledresult with the reason. -
A note follows the turn and gives the next run’s model the reason:
For a cancelled invocation, the type is
run-cancelled.
- The error is retryable. Restate retries the invocation, and the run continues from its journal.
- The run ends before
beforeRunaccepts its input, for example becausebeforeRunthrowsAgentTerminalError, or because the invocation is cancelled whilebeforeRunruns. The run saves nothing, not even its input. - The run fails with
ConversationConflictError. Another run changed the conversation, so another save would fail too.
cancelled result, which says that the call was interrupted and may have taken effect. Cancel instead of killing when you can: a cancelled run closes its calls itself, with the reason.
Storage layout
Conversation stores the messages in the object’s state in chunks of 8, each under its own key, next to a metadata entry and, after compaction, the summary. With the default keys, a conversation of 20 messages is stored like this:
- A run writes only the chunk that it extends, the new chunks, and the metadata, so the size of each write depends on the new messages, not on the length of the conversation.
countis the number of stored messages and serves as the conversation’s version. Before each save, a run checks thatcounthasn’t changed since it loaded the conversation or saved last. If another run saved in the meantime, the run throwsConversationConflictError, aTerminalError.- Within one invocation,
Conversationreads each state key at most once and keeps the values that it read and wrote, so the saves of a run don’t read the chunks again. A shared handler that callspagetwice in one invocation receives the values from its first read, so call it once per invocation. - Compaction writes the summary’s
texttoconversation/summary, together withthrough, the number of messages that the summary replaces. A run then receives the summary instead of the messages beforethrough, and reads only the chunks that hold messages fromthroughon. A save writes the summary only when the run has a new summary to store. The messages stay in state, sopagestill returns every message. Conversationreads with shared state and writes with exclusive state. A shared handler can read the conversation while a run is in progress, but only an exclusive handler can save to it.
Several conversations
To keep several conversations in one object, give eachConversation its own stateKey, for example new Conversation({stateKey: "conversation/flights"}). The chunk keys then start with conversation/flights/, and the metadata and summary keys are conversation/flights/meta and conversation/flights/summary. Preload the metadata and summary keys of each conversation. Set metaKey only to match a layout that already exists in state. It doesn’t change the summary key.
Runs that save to the same conversation must run one after the other. To run agents in parallel or race them in one handler, see Agents in workflows.
Lazy state
By default, Restate sends all of an object’s state, up to the eager state size limit, to each invocation when it starts, including invocations of shared handlers such ashistory or steer. Compaction doesn’t remove messages from the state, so every invocation of the session would receive the whole conversation, although a run reads only the chunks after the last summary.
In each object that uses Conversation, preload only the conversation’s metadata and summary keys in the object’s options. Each invocation then fetches a chunk when it reads it:
- Each load, save, and
pagecall readsconversation/metafirst, and each load also readsconversation/summary, so preload both. - Add other small keys that the handlers read on most invocations, such as
active, which thesteerandinterrupthandlers read on the steering page. - With a list of keys to preload, reads of several keys in one handler can return values from different moments. A shared handler that reads related keys must read them in an order that keeps them consistent, or store them in one key. The AG-UI example does both.
- Each chunk read is a round trip to Restate.
- On AWS Lambda and other platforms without bidirectional streaming, each read of a key that isn’t preloaded suspends the invocation, which replays when Restate sends the requested state.
state per handler in options.handlers. state: {preload: false} preloads no keys. See State in the service configuration.
Manual storage
To control how and where the history is stored, passmessages to run and store result.messages yourself:
messagesOf(error) returns its history up to the failure, with the note on why it failed. Save it only for a TerminalError, because Restate retries other errors.
This approach doesn’t chunk the history, save progress during the run, or store summaries: each run loads and writes the whole history as one state value. Tool result previews, compaction, and contextWindow still apply, because they’re agent options, but each run whose request exceeds maxTokens summarizes the history again.