Agent turn
On Start here, you sent a message to your agent in the test chat, and the agent answered. On this page, we follow that message through the main.
sequenceDiagram
autonumber
participant E as Editor<br/>(browser)
participant M as Main
participant DB as Database
participant P as AI model<br/>provider
E->>M: send msg
M->>DB: enqueue msg
M->>DB: dequeue msg,<br/>mark turn running
loop until model stops calling tools
M->>P: call AI model provider
P-->>M: reply with text or tool to call
M->>M: call tool
M-->>E: stream text and tool calls
end
M->>DB: save turn result
M-->>E: end stream
Queue
The test chat sends your message to the main, which keeps the HTTP response open to stream the answer. First, the main saves the message to a queue in the database, so a crash or a closed tab does not lose it.
Turn start
Any main can dequeue a message, and the database makes sure that only one main dequeues each message. The messages of one conversation run in order, one at a time, and different conversations run at the same time. The main that dequeues your message records the turn as running and starts the agent runtime.
Loop
The agent runtime on the main sends instructions, conversation, and available tools to the model at the AI model provider. For this call, it uses a credential, as a node does, or Gateway credits. The model only decides what comes next: it replies with text, or with a tool to call. The runtime calls the tool, sends back the result, and asks the model again. The turn ends when the model replies with text only, or when a limit (e.g. a cost budget) stops it.
While the turn runs, the main streams text and tool calls in the open HTTP response. When the turn ends, the main saves the result to the conversation, so a reload shows the saved conversation instead of replaying the stream.
Tools
The agent that you built on Start here had no tools. An agent can use four kinds of tools:
| Tool | What runs it |
|---|---|
| Node | Main, outside any workflow |
| Workflow | Workflow execution |
| MCP server | External server, called via Model Context Protocol |
| Custom tool† | V8 isolate on the main |
† A custom tool is a small TypeScript function that a user adds to the agent in the editor. Its name, description, and input schema tell the model when and how to call it. The code comes from the user, so n8n runs it in an isolate, i.e. a separate JavaScript heap with its own memory limit.
In queue mode, a workflow tool runs on a worker, and the turn waits for it on the main.
Other turns
Your message took the simplest path. Other turns differ from it in these common ways:
- Other sources: A published agent can also take messages from a chat platform (e.g. Slack), or run on a schedule. These messages go into the same queue.
- Second message: A second message to the same conversation waits in the queue until the turn ends. It can also steer the running turn, i.e. join it before the next call to the model. Only messages from the test chat and the n8n chat can steer.
- Approval: A tool can ask a person for approval. The turn then saves a checkpoint and pauses, and the approval resumes it from the checkpoint on any main.
- Multi-main: Mains relay the stream over pubsub, so a second tab or the Stop button works on any main.
The agent runtime comes from @n8n/, a library that knows nothing about n8n. Two products build on it: the agents module, which runs the agents that users build, and the n8n Assistant in @n8n/, which builds and fixes workflows for the user. The Assistant has its own threads and event log, and it reaches n8n through an adapter service.
The AI Agent node is not part of this runtime. It is a node inside a workflow. When its model asks for a tool, the node asks the engine to run the tool nodes that connect to it, and the whole loop stays inside one execution.
Read next
- AI functionality: how AI builds and fixes workflows and agents for the user.
- APIs: what happens to a streamed reply when the connection drops.
Quiz
- A main crashes just after you send a message to an agent. Is the message lost?
A crash or a closed tab does not lose a saved message.
- Two messages for the same conversation arrive on two different mains. How do they run?
The database makes sure that only one main dequeues each message, and the messages of one conversation run in order.
- In queue mode, an agent calls a workflow as a tool. Where does that workflow run?
A workflow tool starts a workflow execution, and queue mode runs executions on workers.
- The model replies with a tool to call. What calls the tool?
The model only decides what comes next. The runtime calls the tool, sends back the result, and asks the model again.
- An AI Agent node in a workflow asks the model for a tool. Where does the tool run?
The AI Agent node is a node, not an agent of the
agentsmodule. The whole loop stays inside one execution.