Cloudflare D1 Chat Memory
AI-generatedSummary
Stores and retrieves chat memory in Cloudflare D1.
Inputs
- databaseId (required) — Unique identifier of the Cloudflare D1 database where chat messages are stored.
- sessionId (required) — Identifier for the chat session; messages with the same ID are grouped together.
- tableName — Name of the table to store messages; created automatically if missing.
- maxMessages — Maximum number of messages to retain per session, older ones are removed.
- expirationDays — Number of days after which messages are automatically deleted.
- memoryKey — Variable name used to store memory in prompts.
- returnMessages — If true, returns messages as a list of LangChain message objects; otherwise as a formatted string.
- inputKey — Key to retrieve input from prompt values; defaults to "input".
- outputKey — Key to retrieve output from model; defaults to "output".
Output shape
A memory object (list of LangChain messages or formatted string) representing the stored chat history.
The returned type depends on the Return Messages setting; it may be a list of message objects or a string, and pagination is not applicable.
Examples
Example 1: Add a new chat message to session
databaseId=123, sessionId=chat-session-456, tableName=chat_memory, maxMessages=100, expirationDays=30, memoryKey=chat_history, returnMessages=false, inputKey=input, outputKey=output
Example 2: Retrieve memory for a session in list form
databaseId=123, sessionId=chat-session-456, returnMessages=true
Example 3: Reset memory by clearing old entries
databaseId=123, sessionId=chat-session-456, maxMessages=50