Back to Nodes

MemoryGateway

Last updated Nov 16, 2025

Memory Gateway node for filtering and transforming chat memory content in n8n AI workflows

2 Weekly Downloads
162 Monthly Downloads

Included Nodes

MemoryGateway

Description

n8n-nodes-gateway-memory

This is an n8n community node that provides Memory Gateway – a powerful middleware for filtering and transforming chat memory content in AI workflows.

n8n is a fair-code licensed workflow automation platform.

Features

  • ๐Ÿ”„ Memory Middleware – Acts as a gateway between AI Agent and any Memory storage
  • ๐Ÿงน Content Filtering – Filter data before saving and after retrieving from memory
  • ๐Ÿ’ป Custom JavaScript Code – Write custom filter logic with full JavaScript support
  • ๐ŸŽฏ Universal Compatibility – Works with Postgres, Redis, MongoDB, and any other Memory node
  • โšก Performance – Minimal overhead, processes data on-the-fly
  • ๐Ÿ”’ Data Control – Remove sensitive information, limit message length, clean technical logs

Installation

Follow the installation guide in the n8n community nodes documentation.

Manual Installation (Self-Hosted)

  1. Navigate to your n8n installation directory
  2. Install the package:
npm install n8n-nodes-gateway-memory
  1. Restart n8n

Local Development

  1. Clone this repository
  2. Mount to n8n custom nodes directory
  3. Restart n8n
docker-compose down
docker-compose up --build

Operations

Memory Gateway

The Memory Gateway node provides filtering capabilities for chat memory.

Connection Flow

AI Agent
    โ†“ (Memory slot)
Memory Gateway
    โ†“ (Internal Memory slot)
Postgres Chat Memory (or any other Memory node)

Parameters

Parameter Type Description
Filter Before Save Code Editor JavaScript function to filter data BEFORE saving to memory
Filter After Retrieve Code Editor JavaScript function to filter data AFTER retrieving from memory

Usage

Basic Setup

  1. Add Memory Gateway to your workflow
  2. Connect a Memory node (e.g., Postgres Chat Memory) to Memory Gateway's input
  3. Connect Memory Gateway to AI Agent's Memory slot
  4. Configure filters in Memory Gateway settings

Filter Before Save

This filter runs when AI Agent saves a message to memory.

Available variables:

  • input (string) – User's input message
  • output (string) – AI's response message

Must return:

  • Object with { input: string, output: string }

Example – Remove tool execution logs:

function filterBeforeSave(input, output) {
  // Remove [Used tools: ...] from output
  if (output) {
    output = output.replace(/\[Used tools:[\s\S]*?\]\s*/g, '');
  }
  
  // Limit message length
  const MAX_LENGTH = 3000;
  if (output && output.length > MAX_LENGTH) {
    output = output.substring(0, MAX_LENGTH) + '... [truncated]';
  }
  
  return { input, output };
}

Filter After Retrieve

This filter runs when AI Agent loads messages from memory.

Available variables:

  • messages (Array) – Array of message objects from memory

Must return:

  • Modified messages array

Example – Limit number of messages:

function filterAfterRetrieve(messages) {
  // Keep only last 10 messages
  if (messages && Array.isArray(messages) && messages.length > 10) {
    messages = messages.slice(-10);
  }
  
  // Filter out system messages
  messages = messages.filter(msg => msg.type !== 'system');
  
  return messages;
}

Examples

Example 1: Clean AI Tool Logs

Remove technical information from AI responses before storing:

function filterBeforeSave(input, output) {
  // Remove [Used tools: ...]
  output = output.replace(/\[Used tools:[\s\S]*?\]\s*/g, '');
  
  // Remove JSON metadata
  output = output.replace(/\{[\s\S]*?"action"[\s\S]*?\}/g, '');
  
  return { input, output };
}

Example 2: Privacy Filter

Remove sensitive information:

function filterBeforeSave(input, output) {
  // Remove email addresses
  input = input.replace(/[\w.-]+@[\w.-]+\.\w+/g, '[EMAIL]');
  output = output.replace(/[\w.-]+@[\w.-]+\.\w+/g, '[EMAIL]');
  
  // Remove phone numbers
  input = input.replace(/\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}/g, '[PHONE]');
  output = output.replace(/\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}/g, '[PHONE]');
  
  return { input, output };
}

Example 3: Context Window Management

Keep only relevant messages:

function filterAfterRetrieve(messages) {
  // Keep only last 5 messages
  if (messages.length > 5) {
    messages = messages.slice(-5);
  }
  
  // Remove messages older than 24 hours
  const oneDayAgo = Date.now() - 24 * 60 * 60 * 1000;
  messages = messages.filter(msg => {
    return !msg.timestamp || msg.timestamp > oneDayAgo;
  });
  
  return messages;
}

Example 4: Content Summarization

Summarize long messages:

function filterBeforeSave(input, output) {
  const MAX_LENGTH = 500;
  
  if (output.length > MAX_LENGTH) {
    // Keep first and last parts
    const start = output.substring(0, 200);
    const end = output.substring(output.length - 200);
    output = start + '\n\n[... content summarized ...]\n\n' + end;
  }
  
  return { input, output };
}

Compatibility

Tested with:

  • n8n version: 1.0.0+
  • Node.js: 18.x, 20.x

Compatible Memory Nodes:

  • โœ… Postgres Chat Memory
  • โœ… Redis Chat Memory
  • โœ… MongoDB Chat Memory
  • โœ… Window Buffer Memory
  • โœ… Buffer Memory
  • โœ… All LangChain-compatible memory nodes

Resources

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

MIT

Author

vyunak

Support

If you find this node helpful, please give it a โญ๏ธ on GitHub!


Made with โค๏ธ for the n8n community