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 on a per-session basis. This is particularly useful in chatbot workflows where maintaining conversation history improves response relevance and user experience.

For the Insert Message operation, the node inserts a new message into the session's memory. It supports appending messages or replacing the entire memory with the new message. This allows chatbots to keep track of ongoing conversations, user inputs, and bot responses efficiently.

Practical examples:

  • A customer support chatbot that remembers previous user queries within a session to provide contextual answers.
  • A personal assistant bot that tracks conversation history to maintain continuity across multiple interactions.
  • Any chatbot that needs to store chat logs temporarily or persistently using different storage backends.

Properties

Name Meaning
Session ID Identifier for the conversation session (e.g., chat ID).
Insert Mode How to insert the message: either "Append" to add to existing memory or "Replace Memory" to overwrite it.
Message Settings Collection of message-related details:
• Chat ID: Identifier of the chat.
• Chat Type: Type of chat (private, group, etc.).
• Chat Name: Name or title of the chat.
• Message Content: Text or caption of the message.
• Is Bot Response: Boolean indicating if this message is from the bot.
• Reply To Message: Optional reference to a message being replied to.
User Settings Collection of user-related details:
• User ID: Identifier of the user.
• Display Name: User's display name.
• Username: User's username.
Memory Settings Collection of memory configuration:
• Memory Window Size: Number of messages to keep in memory (5–50).
• Storage Backend: Where to store memory; options are "In-Memory", "Google Sheets", or "Redis".
Assistant Settings Collection of assistant-specific settings:
• Assistant Role: How the assistant is identified in memory.
• Include Reply Context: Whether to include information about replied-to messages.

Output

The output JSON contains the following fields after inserting a message:

  • sessionId: The session identifier used.
  • chatInfo: Metadata about the chat session (such as chat ID, type, and name).
  • messages: Array of stored messages in the current memory window.
  • messageCount: Number of messages currently stored.
  • tokenEstimate: Estimated token count of the stored memory content (useful for managing limits in language models).

If an error occurs, the output includes an error field describing the issue and may include the original input data for debugging.

Dependencies

  • The node uses an internal memory manager class (PureMemoryManager) to handle memory operations.
  • Supports multiple storage backends: in-memory, Google Sheets, and Redis. Using Google Sheets or Redis requires appropriate credentials and configurations outside this node.
  • Requires proper API authentication credentials configured in n8n for Google Sheets or Redis if those backends are selected.

Troubleshooting

  • Common issues:

    • Missing or incorrect session ID can cause memory not to be found or cleared properly.
    • Misconfiguration of storage backend credentials will prevent saving or retrieving memory when using Google Sheets or Redis.
    • Exceeding memory window size or token limits might cause unexpected truncation of stored messages.
  • Error messages:

    • "No memory found for this session": Indicates that no memory exists yet for the given session ID. Ensure the session ID is correct and that messages have been inserted before attempting to get memory.
    • Errors related to storage backend connectivity usually indicate misconfigured credentials or network issues.
    • General errors include the error message and input data to help identify problematic inputs.

Links and References

Discussion