Smart Memory

Configuration-driven memory management for chatbots

Overview

The Smart Memory node provides configuration-driven memory management for chatbots, enabling them to store, retrieve, and clear conversational context associated with different sessions (e.g., chat IDs). This is useful for maintaining stateful conversations where the bot needs to remember previous messages or user interactions.

Common scenarios include:

  • Chatbots that keep track of recent messages to provide context-aware responses.
  • Clearing conversation history when a session ends or resets.
  • Retrieving stored conversation history to analyze or display past interactions.

For example, a chatbot integrated with messaging platforms can use this node to insert new messages into memory, fetch the current memory state to understand the conversation flow, or clear memory when starting a fresh session.

Properties

Name Meaning
Operation The action to perform on memory: Insert Message, Get Memory, or Clear Memory.
Session ID Identifier for the session (e.g., chat ID) to scope memory operations.
Insert Mode How to insert a message: Append (add to existing memory) or Replace Memory (overwrite memory).
Message Settings Details about the message to insert, including:
- Chat ID
- Chat Type (private, group, etc.)
- Chat Name
- Message Content (text or caption)
- Is Bot Response (boolean)
- Reply To Message (optional)
User Settings Information about the user sending the message:
- User ID
- Display Name
- Username
Memory Settings Configuration for memory storage:
- Memory Window Size (number of messages to keep, 5–50)
- Storage Backend (In-Memory, Google Sheets, Redis)
Assistant Settings Settings related to the assistant identity:
- Assistant Role (name used in memory)
- Include Reply Context (boolean to include replied-to message info)

Output

The node outputs JSON objects with the following structure depending on the operation:

  • Insert Message:

    {
      "sessionId": "string",
      "chatInfo": { /* metadata about the chat */ },
      "messages": [ /* array of stored messages */ ],
      "messageCount": 0,
      "tokenEstimate": 0
    }
    
    • chatInfo: Metadata about the chat session.
    • messages: Array of messages currently stored in memory.
    • messageCount: Number of messages stored.
    • tokenEstimate: Estimated token count of the stored memory (approximate size).
  • Get Memory:

    {
      "sessionId": "string",
      "chatInfo": { /* metadata about the chat */ },
      "messages": [ /* array of stored messages */ ],
      "messageCount": 0,
      "tokenEstimate": 0
    }
    

    Similar to insert output but reflects current memory state.

  • Clear Memory:

    {
      "sessionId": "string",
      "message": "Memory cleared"
    }
    

If no memory is found during a get operation, the output includes an error field:

{
  "sessionId": "string",
  "error": "No memory found for this session"
}

The node does not output binary data.

Dependencies

  • The node supports multiple storage backends for memory:

    • In-memory storage (default).
    • Google Sheets (requires appropriate API credentials and setup).
    • Redis (requires connection details and credentials).
  • Proper configuration of these external services and credentials is necessary if using Google Sheets or Redis as storage backends.

  • No internal credential names are exposed; users must provide valid API keys or connection info via n8n credentials.

Troubleshooting

  • Common Issues:

    • Memory not persisting across executions when using in-memory backend (expected behavior since it’s volatile).
    • Errors connecting to Google Sheets or Redis due to missing or incorrect credentials.
    • Invalid session IDs causing unexpected behavior or inability to retrieve memory.
  • Error Messages:

    • "No memory found for this session": Indicates that no stored memory exists for the given session ID. Verify the session ID and ensure messages have been inserted before attempting to get memory.
    • Other errors will be reported with their message and the input data that caused them, aiding debugging.
  • Resolutions:

    • Ensure correct session ID is provided.
    • Confirm that the chosen storage backend is properly configured and accessible.
    • For Google Sheets or Redis, verify credentials and network connectivity.
    • Use the "Insert Message" operation before "Get Memory" to initialize memory.

Links and References

Discussion