---
title: RAG Agent
description: Retrieve and generate an action plan and execute it. Can use external tools.
---

Answer questions with a chat model that looks up your knowledge base first, and can remember the conversation.

## What it does

RAG Agent takes the question in **Prompt**, searches the knowledge base connected to **Knowledge** for the most relevant passages, adds them to the prompt and asks the chat model on **Model** to answer from them. It returns the answer and the sources it used.

Retrieved passages are added inside a block marked as reference data, not instructions. A document that says "ignore your instructions" is treated as text to quote, not as a command.

With a chat memory connected to **Memory**, the agent has a conversation:

1. Before calling the model, it loads the earlier messages and adds them as chat history.
2. It saves the incoming prompt as a `user` message.
3. After the model answers, it saves the reply as an `assistant` message (a structured reply from an output parser is saved as JSON text).

This is how a [Chat Trigger](/nodes/builtin/trigger/chat/) gets its replies: connect the **same** memory to the trigger and to this agent.

## When to use it

* A chatbot that answers from your documents, built with a [Chat Trigger](/nodes/builtin/trigger/chat/).
* Answer one question from a knowledge base in the middle of a workflow, with the sources it used.
* Ground a model's answer in pages you indexed with the [Indexer](/nodes/builtin/ai/aiagents/indexernode/).

To answer fully on-device by quoting your documents, use [Q\&A Agent](/nodes/builtin/ai/aiagents/qanode/) with a Local Q\&A Model. When the model must also call tools, use [Tools Agent](/nodes/builtin/ai/aiagents/toolsagentnode/), which accepts the same knowledge base.

## Inputs and settings

**Prompt** is the question. Behind a Chat Trigger, use `{{ $input.chatInput }}`.

### Knowledge base options

The knowledge base options are the same on the RAG Agent, the [Q\&A Agent](/nodes/builtin/ai/aiagents/qanode/) and the [Tools Agent](/nodes/builtin/ai/aiagents/toolsagentnode/):

* **Search type**: **Most similar** picks the passages closest to the question. **Diverse (MMR)** picks passages close to the question but different from each other, so you get fewer repeats.
* **Match by**: with **Keywords and meaning**, exact words, names and codes are found too. **Meaning only** uses similarity alone.
* **Minimum relevance**: raise it (for example `0.5`) to drop weak passages.
* **Metadata filter**: useful keys are `sourceType` (`file`, `web`, `tab`, `text`, `workflow`, `chat`), `sourceId`, `title`, `url`, `fileName` and `page`. For example `sourceType` = `web` searches only saved web pages.
* **Device-only knowledge with a cloud model**: a knowledge base marked **Device only** (see [Privacy](/app/knowledge-bases/overview/#privacy-cloud-ok-or-device-only)) is never sent to a cloud model. Models that count as on-device are Web LLM, Transformers Chat, Ollama, Chrome AI, Local Q\&A Model and Local Classifier. With any other model, the node stops with an error, or with **Answer without it** runs without the knowledge base and logs a warning.

## Outputs

Each output item has these fields:

```json title="Shape of an output item"
{
  "response": null,
  "sources": []
}
```

`sources` is present when a knowledge base is connected (it may be empty). Each entry has `title`, `url` (when there is one), `knowledgeId`, `sourceId` and `score`. After a run, the node shows a badge such as **3 sources cited**.

## Dependencies and credentials

* **Model**: a chat model such as [Chat OpenAI](/nodes/builtin/ai/aidependencies/llm/openai/) or [Web LLM](/nodes/builtin/ai/aidependencies/llm/wbellm/). Cloud models hold their own API key.
* **Knowledge**: a [Local Knowledge](/nodes/builtin/ai/aidependencies/vectorstore/localknowledge/) node.
* **Memory**: a [Chat Memory](/nodes/builtin/ai/aidependencies/chatmemories/chatmemory/), to remember the conversation.
* **Output Parser**: shapes the answer, for example a [Structured Output Parser](/nodes/builtin/ai/aidependencies/outputparser/structuredoutputparser/).

## Example workflow

1. [Chat Trigger](/nodes/builtin/trigger/chat/) opens a chat window.
2. RAG Agent with **Prompt** `{{ $input.chatInput }}` and **System message** `Answer only from the documents. If the answer is not there, say so.`
3. Connect an [Chat OpenAI](/nodes/builtin/ai/aidependencies/llm/openai/) model to **Model**, a [Local Knowledge](/nodes/builtin/ai/aidependencies/vectorstore/localknowledge/) node that points at your "Help center" knowledge base to **Knowledge**, and one [Chat Memory](/nodes/builtin/ai/aidependencies/chatmemories/chatmemory/) to both **Memory** and the Chat Trigger.

Every message you send runs the workflow; the agent's replies appear in the chat window.

## Common issues

* **"… is on this device only, but … is a cloud model."** The knowledge base is marked **Device only**. Use an on-device model, change the knowledge base's privacy, or set **Device-only knowledge with a cloud model** to *Answer without it*.
* **"Knowledge base "…" doesn't exist on this device."** The workflow comes from another device or the knowledge base was deleted. Pick a knowledge base in the Local Knowledge node, or import its `.awkb` file.
* **The answer ignores your documents.** No passages were found. Lower **Minimum relevance**, check the **Metadata filter**, or try the question in the knowledge base's **Test search** tab.
* **The chat window stays empty.** The Chat Trigger and the agent use different memories. Connect the same Chat Memory to both.
* **"Model not found".** The connected model node is not set up: open it and choose a model and its credential.

## Related nodes

* [Chat Trigger](/nodes/builtin/trigger/chat/): turn the workflow into a chatbot.
* [Q\&A Agent](/nodes/builtin/ai/aiagents/qanode/): one-shot answers, also fully on-device.
* [Tools Agent](/nodes/builtin/ai/aiagents/toolsagentnode/): knowledge plus tools.
* [Indexer](/nodes/builtin/ai/aiagents/indexernode/): fill the knowledge base from a workflow.