# Build a rag for customer support knowledge base that answers tickets automatically

> Source: <https://dev.to/samchenreviews/build-a-rag-for-customer-support-knowledge-base-that-answers-tickets-automatically-311o>
> Published: 2026-09-02 16:31:27+00:00

You'll create a Retrieval-Augmented Generation (RAG) pipeline that pulls the most relevant support articles from your knowledge base, feeds them to an OpenAI LLM, and returns a ready-to-send answer to Zendesk tickets. The result is a hands-free response engine that reduces agent load while keeping answers accurate and up-to-date.

**What is RAG?** RAG (Retrieval-Augmented Generation) is an architecture that first retrieves relevant documents from an external source and then conditions a large language model on those passages before generating a response.

| Tool | Plan / Price | Role |
|---|---|---|
OpenAI API |
Pay-as-you-go (see
|

**Time to build:** ~8 hours for a functional prototype (2 h env setup, 3 h data ingestion, 2 h integration, 1 h testing).

Create a fresh directory and initialise a Python virtual environment:

```
mkdir rag-support && cd rag-support
python3 -m venv .venv
source .venv/bin/activate
```

This isolates dependencies and lets you run the same code locally and in Docker later.

Install the required libraries:

```
pip install openai langchain chromadb tqdm
```

**Why:** `openai`

provides the embedding and completion endpoints, `langchain`

offers high-level abstractions for retrieval, and `chromadb`

(the Python client for Chroma) stores vectors efficiently on disk.

Export the knowledge base as Markdown or plain-text files. A quick way is to use Zendesk's API:

```
curl -s -H "Authorization: Bearer $ZENDESK_TOKEN" \
 "https://yoursubdomain.zendesk.com/api/v2/help_center/articles.json" \
 | jq -r '.articles[] | "\(.title)\n\(.body)"' > articles.txt
```

Replace

`$ZENDESK_TOKEN`

with a token that hasreadpermission on the Help Center. The output concatenates every article into`articles.txt`

, one article after another.

Long documents need to be split into manageable pieces (≈ 300 tokens) so that embeddings stay within OpenAI's token limits.

``` python
from langchain.text_splitter import RecursiveCharacterTextSplitter

with open("articles.txt", "r", encoding="utf-8") as f:
 raw = f.read()

splitter = RecursiveCharacterTextSplitter(
 chunk_size=300,
 chunk_overlap=30,
 separators=["\n\n", "\n", " "]
)
chunks = splitter.split_text(raw)
print(f"Created {len(chunks)} chunks.")
```

**Why:** Overlapping chunks preserve context across paragraph boundaries, improving retrieval relevance.

Convert each chunk into a 1536-dimensional vector using the `text-embedding-ada-002`

model:

``` python
import os, openai
from tqdm import tqdm

openai.api_key = os.getenv("OPENAI_API_KEY")

def embed(texts):
 # Batch up to 2048 tokens per request (OpenAI limit)
 return openai.Embedding.create(
 model="text-embedding-ada-002",
 input=texts
 )["data"]

embeddings = []
batch_size = 100
for i in tqdm(range(0, len(chunks), batch_size)):
 batch = chunks[i:i+batch_size]
 resp = embed(batch)
 embeddings.extend([r["embedding"] for r in resp])
```

Each embedding costs $0.0001 per 1 000 tokens, so a 10 000-article knowledge base typically stays under $5 per month on the OpenAI pay-as-you-go tier.

``` python
import chromadb
from chromadb.utils import embedding_functions

client = chromadb.Client()
collection = client.create_collection(
 name="support-knowledge",
 embedding_function=embedding_functions.OpenAIEmbeddingFunction(
 api_key=os.getenv("OPENAI_API_KEY")
 )
)

ids = [f"doc-{i}" for i in range(len(chunks))]
collection.add(
 ids=ids,
 documents=chunks,
 embeddings=embeddings
)
print("Vectors persisted to ./chromadb")
python
import pinecone

pinecone.init(api_key=os.getenv("PINECONE_API_KEY"), environment="us-west1-gcp")
index = pinecone.Index("support-knowledge")
vectors = [(ids[i], embeddings[i]) for i in range(len(embeddings))]
index.upsert(vectors=vectors, namespace="support")
```

Why choose Pinecone?It offers sub-millisecond latency, automatic scaling, and built-in metadata filtering - critical for high-traffic support desks.

``` python
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
from langchain.vectorstores import Chroma, Pinecone
from langchain.embeddings import OpenAIEmbeddings

# Choose the backend that matches step 5
if use_chroma:
 vectorstore = Chroma(
 collection_name="support-knowledge",
 embedding_function=OpenAIEmbeddings()
 )
else:
 vectorstore = Pinecone.from_existing_index(
 index_name="support-knowledge",
 embedding=OpenAIEmbeddings(),
 namespace="support"
 )

retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
qa = RetrievalQA.from_chain_type(
 llm=OpenAI(model_name="gpt-3.5-turbo"),
 chain_type="stuff",
 retriever=retriever,
 return_source_documents=True
)
```

This chain fetches the four most relevant chunks, concatenates them, and prompts the LLM to answer the user's question while citing sources.

```
 docker run -d --name n8n \
 -p 5678:5678 \
 -v ~/.n8n:/home/node/.n8n \
 n8nio/n8n
```

**Create a workflow**:

`ticket_id`

, `question`

). `qa.run(question)`

and returns `answer`

and `sources`

. `PUT /api/v2/tickets/{ticket_id}`

.**Python script for the Execute Command node** (`answer.py`

):

``` python
 import sys, json
 from answer_chain import qa # assumes qa defined in previous step

 payload = json.loads(sys.stdin.read())
 question = payload["question"]
 resp = qa({"query": question})
 result = {
 "answer": resp["result"],
 "sources": [doc.metadata["source"] for doc in resp["source_documents"]]
 }
 print(json.dumps(result))
```

n8n pipes the incoming JSON to

`stdin`

; the script writes a JSON response to`stdout`

which n8n captures for downstream nodes.

`https://n8n.mycompany.com/webhook/rag-support`

) becomes the endpoint you register in Zendesk's In Zendesk, create a **Trigger** that fires on *Ticket Created* with the condition `Ticket is a support request`

. Add an **Action** → *Notify target* → *HTTP target* pointing at the n8n webhook URL, passing `{ "ticket_id": "{{ticket.id}}", "question": "{{ticket.description}}" }`

.

When a ticket arrives, Zendesk calls the webhook, the RAG chain returns an answer, and the workflow updates the ticket with the response.

Create a dummy ticket in Zendesk:

```
Subject: How do I reset my password?
Description: I cannot find the reset link on the login page.
```

After a few seconds, the ticket body should contain a concise answer such as:

To reset your password, click "Forgot password?" on the login page, enter your email, and follow the link you receive. See article "Password Reset Procedure" for screenshots.

If the answer is missing, check n8n's execution log (accessible at `https://n8n.mycompany.com/executions`

) for any runtime errors.

> The most common failure is hitting OpenAI's rate limits or token quotas, which silently abort the embedding step.

| Failure mode | Symptom | Fix |
|---|---|---|
OpenAI rate limit (60 requests/min for `text-embedding-ada-002` ) |
Embedding script stalls, `openai.error.RateLimitError` raised |
Add exponential back-off (`time.sleep(2**retry)` ) and request higher limits via the OpenAI dashboard. |
Vector DB cost overrun (Pinecone reads > 2 M per month) |
Unexpected bill spike, API returns `429 Too Many Requests`
|
Enable Pinecone's request throttling and monitor usage via the Pinecone console; switch to Chroma for bulk offline queries. |
n8n webhook authentication |
Zendesk receives `401 Unauthorized` and tickets remain unchanged |
Secure the webhook with a static `X-API-KEY` header; add the same header in the Zendesk HTTP target settings. |
Chunk size too large |
`openai.error.InvalidRequestError: This model's maximum context length is 4096 tokens` |
Reduce `chunk_size` to ≤ 300 tokens or upgrade to `gpt-4` (larger context). |
Source document mismatch |
Answer cites wrong article IDs | Ensure each chunk's metadata includes a `source` field (e.g., article URL) when adding to the vector store. |
Network latency |
End-to-end response > 10 s, causing Zendesk timeout | Deploy n8n behind a low-latency VPC, enable keep-alive connections, and consider caching the most common queries in Redis. |

For a deeper technical reference, see [n8n's documentation](https://docs.n8n.io/).

RAG first retrieves factual snippets from a searchable store, then conditions the LLM on those snippets. This reduces hallucinations because the model's output is anchored to concrete documentation rather than relying solely on its pre-training.

Yes. LangChain supports Cohere, Anthropic, and open-source models like LLaMA via the `llama-cpp-python`

wrapper. Swap the `OpenAI`

object with the provider's equivalent and adjust the embedding model accordingly.

Chroma is excellent for prototyping and low-traffic environments because it runs locally and costs nothing. For high-volume SaaS or multi-region deployments, a managed vector DB such as Pinecone or Weaviate provides automatic scaling and SLA guarantees.

Schedule the ingestion script to run nightly (via `cron`

or an n8n timer) and use `vectorstore.delete(ids=old_ids)`

followed by `vectorstore.add(...)`

to replace stale vectors. Pinecone's *upsert* operation automatically overwrites vectors with matching IDs.

Never store raw personally identifiable information (PII) in the vector store. Strip or redact PII during the chunking stage, and configure the OpenAI API to disable data logging (`openai.api_key = "..."; openai.api_base = "https://api.openai.com/v1"; openai.log = "none"`

).

If you want to sell this automation to other SaaS teams, check out ** AI automations you can sell** for pricing ideas, and grab

Ready to replace manual ticket replies with a reliable rag for customer support knowledge base? Deploy the steps above, monitor the metrics, and iterate on your retrieval prompts - your support agents will thank you.
