Save and restore session
Contents
[
Hide
]
You can save a chat session to a file and load it later to continue the same conversation (e.g. after closing the app or moving to another machine).
Save a session
After exchanging messages with a session, save it with a path of your choice:
string sessionId = await api.StartNewChatAsync();
await api.SendMessageToSessionAsync(sessionId, "Remember: my favorite color is blue.");
api.SaveChatSession(sessionId, "my-chat.json");
If you omit the file path, the library uses the session ID as the file name (location may depend on the implementation).
Restore and continue
Later, load the session and keep chatting:
string sessionId = await api.LoadChatSession("my-chat.json");
string reply = await api.SendMessageToSessionAsync(sessionId, "What is my favorite color?");
// reply should refer to "blue"
Use cases
- Resumable conversations — User closes the app and reopens; load the session and continue.
- Backup — Periodically save important conversations.
- Migration — Save on one machine, copy the file, load on another (same API version and model/preset compatibility).
For implementation details, see Session persistence in the Developer’s reference.