> For the complete documentation index, see [llms.txt](https://vibemx.gitbook.io/neuronai-studio/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://vibemx.gitbook.io/neuronai-studio/knowledge-bases/retrieval.md).

# Retrieval & RAG Node

## Search preview

On the knowledge base edit screen, use **Retrieval Preview** to run the same path as runtime (`RagRetrievalService`):

1. Embed the query with the KB embeddings provider
2. `similaritySearch` on the configured vector store
3. Apply `top_k` and optional score `threshold`

Returned chunks show `document_name` (human-readable) and similarity score.

## RAG workflow node

Place a **RAG** node upstream of an Agent or LLM node.

| Config                | Description                                          |
| --------------------- | ---------------------------------------------------- |
| `knowledge_base_id`   | Knowledge base ID (required)                         |
| `query`               | Template with `{{state_key}}`; falls back to `input` |
| `top_k` / `threshold` | Override KB defaults                                 |
| `output_key`          | State key (default `rag_context`)                    |

### Output shape

```php
[
    'query' => '...',
    'results' => [/* chunk arrays */],
    'context' => '...', // concatenated text for prompts
    'knowledge_base_id' => 1,
    'chunk_count' => 3,
    'top_score' => 0.91,
]
```

In the agent message template:

```
Use this context:

{{ rag_context.context }}

User question: {{ input }}
```

Large contexts are truncated by the context-engineering budget (`budget_rag`). See [State & Conditions](/neuronai-studio/workflows/state-and-conditions.md) and [Runtime & Traces](/neuronai-studio/workflows/runtime-and-traces.md).

## Node vs tool

|              | RAG node         | RAG tool                       |
| ------------ | ---------------- | ------------------------------ |
| When it runs | Fixed graph step | When the LLM calls the tool    |
| Output       | Workflow state   | Tool result string             |
| Typical use  | Q\&A pipelines   | Playground / multi-turn agents |

Details: [Agent binding](/neuronai-studio/knowledge-bases/agent-binding.md) · [AI Nodes — RAG](/neuronai-studio/workflows/ai-nodes.md#rag)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://vibemx.gitbook.io/neuronai-studio/knowledge-bases/retrieval.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
