Skip to main content

Maintain RAG embeddings with OpenAI, Postgres and auto drift rollback

Workflow preview

Workflow preview
100%
Maintain RAG embeddings with OpenAI, Postgres and auto drift rollback preview
Open on n8n.io

1. Workflow Overview

Overview This workflow implements a self healing Retrieval Augmented Generation (RAG) maintenance system that automatically updates document embeddings, evaluates retrieval quality, detects embeddi...

Best for

  • Engineering automation workflows
  • AI RAG automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.scheduletrigger, n8n-nodes-base.webhook, n8n-nodes-base.set, n8n-nodes-base.httprequest, n8n-nodes-base.code, n8n-nodes-base.postgres, n8n-nodes-base.comparedatasets, @n8n/n8n-nodes-langchain.embeddingsopenai

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by ResilNext.

Original n8n.io source

1.1 Workflow description

Title
Maintain RAG embeddings with OpenAI, Postgres and auto drift rollback
Workflow name
Maintain RAG embeddings with OpenAI, Postgres and auto drift rollback

Overview

This workflow implements a self-healing Retrieval-Augmented Generation (RAG) maintenance system that automatically updates document embeddings, evaluates retrieval quality, detects embedding drift, and safely promotes or rolls back embedding updates.

Maintaining high-quality embeddings in production RAG systems is difficult. When source documents change or embedding models evolve, updates can accidentally degrade retrieval quality or introduce semantic drift.

This workflow solves that problem by introducing an automated evaluation and rollback pipeline for embeddings.

It periodically checks for document changes, regenerates embeddings for updated content, evaluates the new embeddings against a set of predefined golden test questions, and compares the results with the currently active embeddings.

Quality metrics such as Recall@K, keyword similarity, and answer variance are calculated, while embedding vectors are also analyzed for semantic drift using cosine distance.

If the new embeddings outperform the current ones and remain within acceptable drift limits, they are automatically promoted to production. Otherwise, the system safely rolls back or flags the update for manual review.

This creates a robust, production-safe RAG lifecycle automation system.


How It Works

1. Workflow Trigger

The workflow can start in two ways:

  • Scheduled trigger running daily
  • Webhook trigger when source documents change

Both paths lead to a centralized configuration node that defines parameters such as chunk size, thresholds, and notification settings.

2. Document Retrieval & Change Detection

Documents are fetched from the configured source (GitHub, Drive, Confluence, or other APIs).

The workflow then:

  • Splits documents into deterministic chunks
  • Computes SHA-256 hashes for each chunk
  • Compares them with previously stored hashes in Postgres

Only new or modified chunks proceed for embedding generation, which significantly reduces processing cost.

3. Embedding Generation

Changed chunks are processed through:

  • Recursive text splitting
  • Document loading
  • OpenAI embedding generation

These embeddings are stored as a candidate vector store rather than immediately replacing the production embeddings.

Metadata about the embedding version is stored in Postgres.

4. Golden Question Evaluation

A set of golden test questions stored in the database is used to evaluate retrieval quality.

Two AI agents are used:

  • One queries the candidate embeddings
  • One queries the current production embeddings

Both generate answers using retrieved context.

5. Quality Metrics Calculation

The workflow calculates several evaluation metrics:

  • Recall@K to measure retrieval effectiveness
  • Keyword similarity between generated answers and expected answers
  • Answer length variance to detect inconsistencies

These are combined into a weighted quality score.

6. Embedding Drift Detection

The workflow compares embedding vectors between versions using cosine distance.

This identifies semantic drift, which may occur due to:

  • embedding model updates
  • chunking changes
  • document structure changes

7. Promotion or Rollback

The workflow checks two conditions:

  1. Quality score exceeds the configured threshold
  2. Embedding drift remains below the drift threshold

If both conditions pass:

  • The candidate embeddings are promoted to active

If not:

  • The system rolls back to the previous embeddings
  • Or flags the update for human review

8. Notifications

A webhook notification is sent with:

  • update status
  • quality score
  • drift score
  • timestamp

This allows teams to monitor embedding health automatically.


Setup Instructions

  1. Configure Document Source

Edit the Workflow Configuration node and set:

  • documentSourceUrl API endpoint or file source containing your documents.

Examples include:

  • GitHub repository API
  • Google Drive export API
  • Confluence REST API

  1. Configure Postgres Database

Create the following tables in your Postgres database:

  • document_chunks
  • embeddings
  • embedding_versions
  • golden_questions

These tables store chunk hashes, embedding vectors, version metadata, and evaluation questions.

Connect the Postgres nodes using your database credentials.


  1. Add OpenAI Credentials

Configure credentials for:

  • OpenAI Embeddings
  • OpenAI Chat Model

These are used for generating embeddings and answering evaluation questions.


  1. Populate Golden Questions

Insert evaluation questions into the golden_questions table.

Each record should include:

  • question_text
  • expected passages
  • expected answer keywords

These questions represent critical queries your RAG system must answer correctly.


  1. Configure Notification Webhook

Add a Slack or Teams webhook URL in the configuration node.

Notifications will be sent whenever:

  • embeddings are promoted
  • embeddings are rolled back
  • manual review is required

  1. Adjust Quality Thresholds

In the configuration node you can modify:

  • qualityThreshold
  • driftThreshold
  • chunkSize
  • chunkOverlap

These parameters control the sensitivity of the evaluation system.


Use Cases

Production RAG Monitoring

Automatically evaluate and update embeddings in production knowledge systems without risking degraded results.

Continuous Knowledge Base Updates

Keep embeddings synchronized with frequently changing documentation, repositories, or internal knowledge bases.

Safe Embedding Model Upgrades

Test new embedding models against production data before promoting them.

AI System Reliability

Detect retrieval regressions before they affect end users.

Enterprise AI Governance

Provide automated evaluation and rollback capabilities for mission-critical RAG deployments.


Requirements

This workflow requires the following services:

  • n8n
  • Postgres Database
  • OpenAI API

Recommended integrations:

  • Slack or Microsoft Teams (for notifications)

Required nodes include:

  • Schedule Trigger
  • Webhook
  • HTTP Request
  • Postgres
  • Compare Datasets
  • Code nodes
  • OpenAI Embeddings
  • OpenAI Chat Model
  • Vector Store nodes
  • AI Agent nodes

Summary

This workflow provides a fully automated self-healing RAG infrastructure for maintaining embedding quality in production systems.

By combining change detection, golden-question evaluation, embedding drift analysis, and automatic rollback, it ensures that retrieval performance improves safely over time.

It is ideal for teams running production AI assistants, knowledge bases, or internal search systems that depend on high-quality vector embeddings.

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - Daily RAG Maintenance Schedule

Type / Role
n8n-nodes-base.scheduleTrigger - scheduleTrigger
Config choices
Version 1.3

Block 2 - Source Change Webhook

Type / Role
n8n-nodes-base.webhook - webhook
Config choices
Version 2.1

Block 3 - Workflow Configuration

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 4 - Fetch Documents from Source

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.3

Block 5 - Chunk Documents & Compute Hash

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 6 - Fetch Previous Chunk Hashes

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 7 - Detect Changed Chunks

Type / Role
n8n-nodes-base.compareDatasets - compareDatasets
Config choices
Version 2.3

Block 8 - OpenAI Embeddings

Type / Role
@n8n/n8n-nodes-langchain.embeddingsOpenAi - embeddingsOpenAi
Config choices
Version 1.2

Block 9 - Recursive Text Splitter

Type / Role
@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter - textSplitterRecursiveCharacterTextSplitter
Config choices
Version 1

Block 10 - Document Loader

Type / Role
@n8n/n8n-nodes-langchain.documentDefaultDataLoader - documentDefaultDataLoader
Config choices
Version 1.1

Block 11 - New Vector Store (Candidate)

Type / Role
@n8n/n8n-nodes-langchain.vectorStoreInMemory - vectorStoreInMemory
Config choices
Version 1.3

Block 12 - Store Embedding Metadata

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 13 - Save Embedding Version Metadata

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 14 - Fetch Golden Questions

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 15 - OpenAI Chat Model

Type / Role
@n8n/n8n-nodes-langchain.lmChatOpenAi - lmChatOpenAi
Config choices
Version 1.3

Block 16 - Fetch Previous Embeddings

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 17 - Calculate Quality Metrics

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 18 - Calculate Embedding Drift

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 19 - Quality Improved?

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.3

Block 20 - Promote New Embeddings

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 21 - Rollback to Previous Embeddings

Type / Role
n8n-nodes-base.postgres - postgres
Config choices
Version 2.6

Block 22 - Flag for Human Review

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 23 - Send Notification

Type / Role
n8n-nodes-base.httpRequest - httpRequest
Config choices
Version 4.3

Block 24 - Generate Answers (New)

Type / Role
@n8n/n8n-nodes-langchain.agent - agent
Config choices
Version 3

Showing the first 24 of 41 workflow blocks. Download the JSON for the full node graph.

3. Summary Table

Workflow Maintain RAG embeddings with OpenAI, Postgres and auto drift rollback
Complexity advanced
Nodes 41
Categories Engineering, AI RAG
Author ResilNext
Published 14 Mar 2026

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/14036/14036.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Maintain RAG embeddings with OpenAI, Postgres and auto drift rollback do?

Overview This workflow implements a self healing Retrieval Augmented Generation (RAG) maintenance system that automatically updates document embeddings, evaluates retrieval quality, detects embeddi...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Engineering, AI RAG use case.