# Serve a RAG chat backend for Next.js with OpenAI and Redis: n8n workflow

> Started by another system calling it, and works across Pinecone, Postgres and Redis. It puts OpenAI behind a retrieval step, so answers are drawn from...

Source: https://millionrounds.com/workflow/serve-a-rag-chat-backend-for-next-js-with-openai-and-redis (Millionrounds; every figure on the page links to its filing or article)

[Automation library](https://millionrounds.com/workflows) · [Data collection](https://millionrounds.com/workflows?category=Data+collection#library)

# Serve a RAG chat backend for Next.js with OpenAI and Redis

Started by another system calling it, and works across Pinecone, Postgres and Redis. It puts OpenAI behind a retrieval step, so answers are drawn from documents you supply rather than from the model alone. 23 steps, 3 branches: large, and worth reading before you trust it with anything live.

Large Calls a model: OpenAI Retrieval

[Download free](https://millionrounds.com/login?next=%2Fapp%2Fworkflow%2Fserve-a-rag-chat-backend-for-next-js-with-openai-and-redis)

**23 steps** Large: read it before trusting it with anything live

**3** Branches: places the flow splits

**0** Loops or waits

**3** Services it connects to

## The flow

Starts it 4 Model 6 Logic 9 Service 4

The steps in the order they run, from what starts it.

- 1 **Webhook - RAG Agent Request** Webhook *Starts it*
- 2 **OpenAI Chat Model** Lm Chat Open Ai *Model*
- 3 **Window Buffer Memory** Memory Buffer Window *Model*
- 4 **Embeddings - OpenAI** Embeddings Open Ai *Model*
- 5 **Set - Config** Set *Logic*
- 6 **AI Agent - RAG Assistant** Agent *Model*
- 7 **Vector Store - Retrieve Context** Vector Store Pinecone *Model*
- 8 **Code - Validate Request Schema** Code *Logic*
- 9 **Code - Parse & Validate Agent Output** Code *Logic*
- 10 **Tool - Search Knowledge Base** Tool Vector Store *Model*
- 11 **IF - Validation Passed** If *Logic*
- 12 **Redis - Write Query Cache** Redis *Service*
- 13 **Code - Build Cache Key** Code *Logic*
- 14 **Respond - Validation Error (400)** Respond To Webhook *Starts it*
- 15 **Postgres - Log Conversation Turn** Postgres *Service*
- 16 **Redis - Check Query Cache** Redis *Service*
- 17 **Respond - Success (200)** Respond To Webhook *Starts it*
- 18 **IF - Content Changed Requires Revalidation** If *Logic*
- 19 **IF - Cache Hit** If *Logic*
- 20 **HTTP Request - Notify Next.js Revalidation** Http Request *Service*
- 21 **NoOp - Revalidation Skipped** No Op *Logic*
- 22 **Respond - Success (Cached)** Respond To Webhook *Starts it*
- 23 **Code - Handle Revalidation Failure** Code *Logic*

## How it's built

- **Starts on:** Another system calling it
- **Steps:** 23, with 3 branches
- **Size:** Large: read it before trusting it with anything live
- **Connects to:** Pinecone, Postgres, Redis
- **Model:** OpenAI
- **Retrieval:** Answers are drawn from documents you supply, not from the model alone

## Questions

What does it need to run?

An n8n account, plus your own Pinecone, Postgres and Redis accounts, and a key for OpenAI. You connect your own accounts after importing it; none are included.

What starts it?

It starts on another system calling it.

Does it use AI?

Yes. It calls OpenAI. Its answers draw on documents you give it, not on the model alone. Model calls are billed by the provider, not by n8n.

How long does it take to set up?

Take your time. At 23 steps it is large: read it before trusting it with anything live.

How do I import it?

Copy the workflow, open a new workflow in n8n and paste. The steps appear on the canvas, ready for your own accounts.

**Before you run it**

Nothing here has been tested by us. It is somebody's working automation, read and described from its own steps. Treat the connections it wants as a list of accounts it will touch, and read the 23 steps before you point it at anything that matters.

## Run it in n8n

- **Create your n8n account** Free to start. n8n is where the workflow runs. [Create an n8n account ↗](https://millionrounds.com/n8n)
- **Download the workflow** Free with a Millionrounds account. [Download free](https://millionrounds.com/login?next=%2Fapp%2Fworkflow%2Fserve-a-rag-chat-backend-for-next-js-with-openai-and-redis)
- **Paste it into n8n** Open a new workflow in n8n and paste. Then connect your own accounts.

Stored with the author's traces removed: canvas notes, instance identifiers, saved credential names and any live document they had left behind.

## Connects to

Each opens the other workflows that use it.

[Pinecone](https://millionrounds.com/workflows?service=Pinecone#library)[Postgres](https://millionrounds.com/workflows?service=Postgres#library)[Redis](https://millionrounds.com/workflows?service=Redis#library)

## Workflows like it

Also data collection, with the same services where possible, closest in size first.

- [**Automate customer retention with Stripe** 22 steps · calls a model · Gmail, Google Sheets](https://millionrounds.com/workflow/automate-customer-retention-with-stripe)
- [**Automatic service with Gemini and Postgres** 22 steps · calls a model · Postgres, Redis](https://millionrounds.com/workflow/automatic-service-with-gemini-and-postgres)
- [**Smart chat assistant with OpenAI and Google Sheets** 22 steps · calls a model · Gmail, Google Drive](https://millionrounds.com/workflow/smart-chat-assistant-with-openai-and-google-sheets)
- [**Taxi service provider with Grok and Postgres** 24 steps · calls a model · Postgres, Redis](https://millionrounds.com/workflow/taxi-service-provider-with-grok-and-postgres)
- [**Build AI-ready knowledge base from Outlook & Notion** 25 steps · calls a model · Notion, Outlook](https://millionrounds.com/workflow/build-ai-ready-knowledge-base-from-outlook-and-notion)
- [**Conversing with OpenAI and Postgres** 25 steps · calls a model · Postgres](https://millionrounds.com/workflow/conversing-with-openai-and-postgres)
