> Documentation index for m4trix: https://m4trix.dev/llms.txt. Every page is available as Markdown by appending `.md` to its URL.

# RAG + Tools

Source: https://m4trix.dev/docs/examples/rag-tools

Build agents that use retrieval-augmented generation (RAG) or tool-calling patterns.

## Approach

1. **Define events** — e.g. `rag-request` (query + context) and `rag-response` (answer chunks)
2. **Agent logic** — Fetch from your vector store, build context, call the LLM with the augmented prompt
3. **Streaming** — Emit response chunks as they arrive from the LLM

## Tool-Calling Pattern

For agents that call tools (e.g. search, calculator):

1. Emit a `tool-request` event with the tool name and params
2. Another agent (or external system) handles the tool and emits `tool-response`
3. The original agent receives `tool-response` and continues

This can be implemented with multiple agents on different channels, or with a single agent that manages tool state internally.

For a single agent calling an LLM directly, define tools with `Tool.of()` and pass them to the provider with `tools.toOpenAI()`, `tools.toAnthropic()` or `tools.toAiSdk({ jsonSchema })`. Feed the results back with `executeForModel()`. See [How to add tools to an agent](https://m4trix.dev/docs/getting-started/how-to.md#how-to-add-tools-to-an-agent).

## Example Repos

- **core-example** — Basic streaming agent; extend with RAG by adding retrieval before the LLM call
- **open-ai-speech-to-speech-example** — Uses OpenAI Realtime API; shows integration patterns

## Common Recipes

See [Common Recipes](https://m4trix.dev/docs/examples/common-recipes.md) for copyable snippets (retrieval, tool loops, etc.).
