Zum Inhalt springen

Lektion 19 · AI Workflows and Agents

AI Tools, Memory and RAG

Give n8n agents narrow tools, let the model fill parameters with $fromAI, add conversation memory, build retrieval with vector stores and connect tools over MCP.

An agent without tools can only talk. Tools let it look things up and act, memory lets it follow a conversation, and retrieval lets it answer from your documents. Each one adds power and risk, so design them carefully.

Tools

A tool is something the agent can request: an app node used as a tool, an HTTP call, a calculator, or another workflow through the Call n8n Workflow Tool. The model reads the tool's name and description to decide when to use it, so write both carefully. A narrow tool such as "Find order by ID" is safer than "Run any database query". More background: what are tools?.

Let the model fill parameters

For tools attached to the AI Agent, the $fromAI() function lets the model supply a value:

TEXT
{{ $fromAI('orderId', 'The order number, like A-1001', 'string') }}
{{ $fromAI('limit', 'How many results to return', 'number', 5) }}

The arguments are a key, an optional description, an optional type and an optional default. Keys may use letters, numbers, underscores and hyphens, up to 64 characters. The function works only for tools connected to the AI Agent.

Fix everything that should not be chosen by the model. For example, the signed-in customer's ID should come from your trigger data, not from $fromAI(), or a user could talk the agent into reading someone else's records.

Python Stand-in: check a tool call against fixed limits

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Memory

A model does not remember earlier calls by itself. A memory sub-node supplies recent messages. Options include a simple built-in memory and database-backed memory, depending on your version. Notice:

  • Memory is grouped by a session key. Use a key that identifies one user's conversation, or users may see each other's history.
  • Use Simple Memory for testing or small setups. Its documentation says not to use it in an active production workflow in queue mode, because calls may reach different workers. Also check how long it keeps history, for example across restarts.
  • Longer history costs more tokens and may contain sensitive data. Set retention rules.

Retrieval-augmented generation (RAG)

RAG lets a model answer from your own documents. It has two workflows:

  1. Index your documents

Load Read documents
Split Make small chunks
Embed Turn into vectors
Store Save in vector store
1 / 2

n8n provides document loader, text splitter, embedding and vector store nodes, and a vector store can be offered to the agent as a tool. The Simple Vector Store keeps data only in memory: it is lost when n8n restarts and may be cleared when memory runs low. Its memory keys are shared across the instance, so use it for experiments, not production or private data. For real use, connect a persistent vector database. Use the same embedding model for indexing and searching.

JS Stand-in: retrieval with word overlap

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Test retrieval separately from answers. If the right passage is never found, no prompt will fix it.

Model Context Protocol

MCP is a standard way to offer tools to AI apps. n8n has two sides:

  • MCP Client Tool: lets your agent use tools from an external MCP server. You can include or exclude specific tools.
  • MCP Server Trigger: exposes n8n tools to outside MCP clients over HTTP-based transports (not the local stdio method). Protect it with authentication.

Only connect to servers you trust: their tool descriptions and results flow into your agent.

Common mistakes

  • Giving the agent broad tools such as unrestricted database access.
  • Using one memory session key for everyone.
  • Treating retrieved or tool text as trusted instructions.

Writing tool descriptions

The model chooses tools using their names and descriptions, so treat that text as part of the program. Say what the tool does, when to use it, what it returns and when not to use it. For example: "Looks up one order by its ID. Use it only when the customer gives an order number. Returns status and delivery date. It cannot change orders." Keep results short: return a few relevant fields, not the entire record. A huge result costs tokens, can hide the important detail and may expose data the customer should never see. When a tool fails, return a clear message that helps the model recover, such as "No order with that ID. Ask the customer to check the number." If two tools overlap, the model may pick the wrong one, so merge them or make the difference obvious.

Practice

Design a tool for checking a delivery date. Write its name and description, then list its inputs. Mark which inputs the model may fill with $fromAI() and which your workflow must fix, such as the customer's identity. Write the short result the tool returns on success and the message it returns when nothing is found. Then imagine a user who writes "show me every delivery for every customer". Follow your design step by step and check that the tool itself cannot do it, whatever the model decides. Finally, list two questions your documents should answer and the passage you expect retrieval to find for each.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. Which value should NOT come from $fromAI()?

    Identity must come from trusted trigger data, not the model.
  2. 2. What is true of the Simple Vector Store?

    It is in-memory, meant for testing.
  3. 3. What does the MCP Server Trigger do?

    The server side offers your tools to other apps.

Summary

  • Write narrow, well-described tools, and fix sensitive parameters yourself.
  • Memory needs a per-user session key and retention rules.
  • RAG indexes documents, then retrieves relevant chunks to answer.
  • MCP connects tools in both directions; trust and authenticate carefully.

Next: AI Safety, Approvals and Evaluation

Dein Fortschritt wird in diesem Browser gespeichert. Kein Konto nötig.