ModelMessage objects. Passing these back into the next run continues the conversation, giving the model full context. History processors let you transform, trim, summarize, or filter messages before each turn - keeping context windows manageable without losing important history.
Multi-turn conversations
Useresult.messages as the messageHistory for the next run:
result.messages contains the full conversation history (including prior turns). result.newMessages contains only the messages added during this specific run - useful for storing incremental updates.
Message serialization
To persist conversation history across sessions, serialize messages to JSON and restore them later.serializeMessages encodes ModelMessage[] to a JSON string. deserializeMessages is the inverse: it parses and validates the JSON, returning a typed ModelMessage[].
History processors
historyProcessors run before each turn and receive the accumulated message history. They return a (possibly modified) message list that is sent to the model. Use them to keep context windows affordable without losing important history.
trimHistoryProcessor
Keep the last N messages. Simple and fast - good for short-context workflows.tokenTrimHistoryProcessor
Trim messages to fit within a token budget. Removes older messages first until the history fits.summarizeHistoryProcessor
Summarize messages that exceed a threshold, replacing them with a condensed summary message. Preserves semantic content while reducing token count.{ maxMessages?: number; summarizePrompt?: string }. When the non-system message count exceeds maxMessages, the older portion is summarized and replaced with a single summary message. The most recent floor(maxMessages / 2) messages are always kept verbatim. System messages are always preserved.
privacyFilterProcessor
Redact sensitive content before messages are sent to the model. Supports regex-based redaction and field-path-based removal.Custom history processors
A history processor is any function that takes(messages: ModelMessage[], ctx: RunContext<TDeps>) and returns a ModelMessage[].
Streaming
Real-time token and event streaming
Agents
Agent class, run methods, and options