getHistory() and reopen it later with setHistory() — the model regains its memory, tool calls included.
Quick Start
1
Save a chat
2
Reopen the chat
How It Works
getHistory() returns a copy of the conversation; setHistory() validates it and replaces the agent’s history, so the next chat() replays the full conversation to the model.
AgentMessage
Each saved message is anAgentMessage. Persist the array getHistory() returns and pass it back to setHistory().
Validation Rules
setHistory() validates at load time and throws a descriptive Error before the next model call. The messages are copied on write, so mutating the array you pass in never changes the agent’s state.
setHistory() also clears the internal response cache, so a repeat prompt after a restore is re-evaluated against the restored history rather than served from the pre-restore prompt-keyed cache.
Common Patterns
Save to disk (Node) and reopen
Mobile / Tauri key-value store
Mirror the file example using the platform’s key-value store — this is the scenario the SDK was designed for (reopening a chat in a mobile app).Tool-calling conversation round-trip
Restored tool results carry over, so the model does not re-run the tool — it already “remembers” the result.Best Practices
Copy on read, copy on write
Copy on read, copy on write
Mutating the array returned by
getHistory() or passed to setHistory() does not mutate the agent’s state, so store references freely.Validate at load time, not at model time
Validate at load time, not at model time
Catch the
Error from setHistory() and surface it to the user. A malformed history accepted silently would otherwise 400 on the next model call, far from the loading code.Do not include a leading system message when serialising
Do not include a leading system message when serialising
A leading
system message is stripped anyway — the agent prepends its own instructions on every run. Keep the stored payload compact.Cross-provider tool restoration
Cross-provider tool restoration
Always keep the
name field on tool messages when serialising. Without it, restoring a tool history and running against a non-OpenAI provider is rejected by the adapter.Related
Agent
The class
getHistory / setHistory live on.Sessions
Multi-turn conversations via
Session / SessionManager.
