Smart Memory

Configuration-driven memory management for chatbots

Overview

The Smart Memory node provides configuration-driven memory management tailored for chatbots. It enables storing, retrieving, and clearing conversational memory associated with different sessions (e.g., chat IDs). This is useful in chatbot workflows where maintaining context across multiple messages or interactions is essential.

Common scenarios include:

  • Keeping track of recent user messages to provide context-aware responses.
  • Retrieving stored conversation history to inform decision-making or generate summaries.
  • Clearing session memory when a conversation ends or resets.

For example, a chatbot can use this node to insert each incoming message into memory, retrieve the last 15 messages when generating a response, or clear memory when a user ends a 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 associate memory with a specific conversation.
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 (title or username)
- Message Content (text or caption)
- Is Bot Response (boolean)
- Reply To Message (optional reference)
User Settings Information about the user sending the message:
- User ID
- Display Name
- Username
Memory Settings Configuration of 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 role:
- Assistant Role (name/identifier for the assistant)
- Include Reply Context (whether to include replied-to message info)

Output

The node outputs JSON objects containing information about the memory state after the operation:

  • For 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).
  • For Get Memory:
    Same structure as above if memory exists; otherwise:

    {
      "sessionId": "string",
      "error": "No memory found for this session"
    }
    
  • For Clear Memory:

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

The node does not output binary data.

Dependencies

  • The node uses an internal memory manager class that supports multiple storage backends:

    • In-memory storage (default)
    • Google Sheets
    • Redis
  • No external API keys or credentials are explicitly required by the node itself, but configuring Google Sheets or Redis backends will require appropriate credentials and setup within n8n.

  • The node expects input data to contain message and user information, typically from messaging platforms.

Troubleshooting

  • Common Issues:

    • Missing or incorrect Session ID may cause memory operations to fail or behave unexpectedly.
    • Using unsupported or misconfigured storage backends (e.g., Google Sheets or Redis without proper credentials) will cause errors.
    • Exceeding the memory window size may result in older messages being discarded silently.
  • Error Messages:

    • "No memory found for this session": Indicates a get operation was performed on a session with no stored memory. Ensure messages have been inserted first.
    • Other errors will be returned with their message and the input data that caused them, aiding debugging.
  • Resolution Tips:

    • Verify that the session identifier matches across operations.
    • Confirm backend storage credentials and connectivity.
    • Adjust memory window size according to expected conversation length.

Links and References

Discussion