> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agnost.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# n8n

> Send n8n AI Agent conversations, tool calls, and traces to Agnost AI

## Choose a setup method

| Method | Use it when |
| - | - |
| **Agnost AI skill** | You want your coding agent to inspect the app, make the change, and verify it. |
| **Agnost AI SDK** | You want explicit control over the interaction boundaries and fields. |
| **OpenTelemetry** | The framework already emits useful spans or you operate an OTel pipeline. |

Start with one method for a call path. Combining SDK tracking with framework
OpenTelemetry on the same call can create duplicate interactions.

## Recommended: Agnost AI skill

> [!NOTE]
>
> **Recommended.** Choose this path when you want your coding agent to inspect the
> project, select a supported transport, make the smallest instrumentation change,
> send a test event, and verify that it reached Agnost AI.

Install the skill once in your coding-agent environment:

```bash theme={null}
npx skills add AgnostAI/skills --skill agnost-ai
```

Run the framework-specific prompt below from the application root. Review the
resulting diff before deploying it.

```text theme={null}
Use the agnost-ai skill to add Agnost AI analytics to this n8n workflow.
Org ID: your-org-id
Add the Agnost AI HTTP Request nodes after the AI Agent node in the exported workflow JSON and verify one fresh chat turn.
```

## Manual setup: Agnost AI ingest API

An n8n workflow cannot load the Agnost AI SDK, so this setup uses HTTP Request
nodes that call the same ingest API the SDKs use:
[Capture Session](https://docs.agnost.ai/capture-session.md) and [Capture Event](https://docs.agnost.ai/capture-event.md). It
works on n8n Cloud and on self-hosted n8n, with no code and no environment
variables.

After each chat message, the workflow sends:

* one session for the chat, keyed by the Chat Trigger's `sessionId`, with the user ID if the chat passes one
* one event for the turn, with the user's message as input and the AI Agent's reply as output
* one child event for each tool the AI Agent called, with the tool's input and result

### 1. Get your organization ID

Sign in to [app.agnost.ai](https://app.agnost.ai), open **Settings**, and copy
the **Organization ID** under **General**. It is a UUID.

### 2. Import the workflow

The workflow below is a chat agent (Chat Trigger, AI Agent, Anthropic Chat
Model, Calculator tool) with four Agnost AI nodes after the AI Agent:

| Node | What it does |
| - | - |
| **Agnost AI: session** | `POST /api/v1/capture-session` with the chat's `sessionId` and user ID |
| **Agnost AI: turn** | `POST /api/v1/capture-event` with the user message and the agent's reply |
| **Tool steps** | Splits the AI Agent's `intermediateSteps` into one item per tool call |
| **Agnost AI: tool calls** | `POST /api/v1/capture-event` for each tool call, linked to the turn |

> [accordion] Workflow JSON
>
> ```json theme={null}
> {
>   "name": "Chat agent with Agnost AI",
>   "nodes": [
>     {
>       "parameters": {
>         "public": true,
>         "options": {
>           "responseMode": "streaming"
>         }
>       },
>       "name": "When chat message received",
>       "type": "@n8n/n8n-nodes-langchain.chatTrigger",
>       "typeVersion": 1.5,
>       "position": [
>         240,
>         0
>       ],
>       "webhookId": "6f1c2d3e-4a5b-4c6d-8e9f-0a1b2c3d4e5f"
>     },
>     {
>       "parameters": {
>         "options": {
>           "returnIntermediateSteps": true
>         }
>       },
>       "name": "AI Agent",
>       "type": "@n8n/n8n-nodes-langchain.agent",
>       "typeVersion": 3.1,
>       "position": [
>         480,
>         0
>       ]
>     },
>     {
>       "parameters": {
>         "model": {
>           "__rl": true,
>           "mode": "id",
>           "value": "claude-haiku-4-5"
>         },
>         "options": {}
>       },
>       "name": "Anthropic Chat Model",
>       "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
>       "typeVersion": 1.6,
>       "position": [
>         480,
>         220
>       ]
>     },
>     {
>       "parameters": {},
>       "name": "Calculator",
>       "type": "@n8n/n8n-nodes-langchain.toolCalculator",
>       "typeVersion": 1,
>       "position": [
>         640,
>         220
>       ]
>     },
>     {
>       "parameters": {
>         "method": "POST",
>         "url": "https://api.agnost.ai/api/v1/capture-session",
>         "sendHeaders": true,
>         "headerParameters": {
>           "parameters": [
>             {
>               "name": "x-org-id",
>               "value": "YOUR_AGNOST_ORG_ID"
>             }
>           ]
>         },
>         "sendBody": true,
>         "specifyBody": "json",
>         "jsonBody": "={{ JSON.stringify({ session_id: $('When chat message received').item.json.sessionId, user_data: { user_id: $('When chat message received').item.json.metadata?.userId }, metadata: { source: 'n8n', workflow: $workflow.name } }) }}",
>         "options": {}
>       },
>       "name": "Agnost AI: session",
>       "type": "n8n-nodes-base.httpRequest",
>       "typeVersion": 4.5,
>       "position": [
>         800,
>         0
>       ],
>       "onError": "continueRegularOutput"
>     },
>     {
>       "parameters": {
>         "method": "POST",
>         "url": "https://api.agnost.ai/api/v1/capture-event",
>         "sendHeaders": true,
>         "headerParameters": {
>           "parameters": [
>             {
>               "name": "x-org-id",
>               "value": "YOUR_AGNOST_ORG_ID"
>             }
>           ]
>         },
>         "sendBody": true,
>         "specifyBody": "json",
>         "jsonBody": "={{ JSON.stringify({ session_id: $('When chat message received').item.json.sessionId, event_id: $('When chat message received').item.json.sessionId + ':' + $execution.id, primitive_name: 'n8n-ai-agent', args: $('When chat message received').item.json.chatInput, result: String($('AI Agent').item.json.output), success: true }) }}",
>         "options": {}
>       },
>       "name": "Agnost AI: turn",
>       "type": "n8n-nodes-base.httpRequest",
>       "typeVersion": 4.5,
>       "position": [
>         1020,
>         0
>       ],
>       "onError": "continueRegularOutput"
>     },
>     {
>       "parameters": {
>         "fieldToSplitOut": "intermediateSteps",
>         "options": {}
>       },
>       "name": "Tool steps",
>       "type": "n8n-nodes-base.splitOut",
>       "typeVersion": 1,
>       "position": [
>         800,
>         240
>       ]
>     },
>     {
>       "parameters": {
>         "method": "POST",
>         "url": "https://api.agnost.ai/api/v1/capture-event",
>         "sendHeaders": true,
>         "headerParameters": {
>           "parameters": [
>             {
>               "name": "x-org-id",
>               "value": "YOUR_AGNOST_ORG_ID"
>             }
>           ]
>         },
>         "sendBody": true,
>         "specifyBody": "json",
>         "jsonBody": "={{ JSON.stringify({ session_id: $('When chat message received').item.json.sessionId, event_id: $('When chat message received').item.json.sessionId + ':' + $execution.id + ':tool:' + $itemIndex, parent_id: $('When chat message received').item.json.sessionId + ':' + $execution.id, primitive_name: $json.action.tool, args: JSON.stringify($json.action.toolInput), result: typeof $json.observation === 'string' ? $json.observation : JSON.stringify($json.observation), success: true }) }}",
>         "options": {}
>       },
>       "name": "Agnost AI: tool calls",
>       "type": "n8n-nodes-base.httpRequest",
>       "typeVersion": 4.5,
>       "position": [
>         1020,
>         240
>       ],
>       "onError": "continueRegularOutput"
>     }
>   ],
>   "connections": {
>     "When chat message received": {
>       "main": [
>         [
>           {
>             "node": "AI Agent",
>             "type": "main",
>             "index": 0
>           }
>         ]
>       ]
>     },
>     "Anthropic Chat Model": {
>       "ai_languageModel": [
>         [
>           {
>             "node": "AI Agent",
>             "type": "ai_languageModel",
>             "index": 0
>           }
>         ]
>       ]
>     },
>     "Calculator": {
>       "ai_tool": [
>         [
>           {
>             "node": "AI Agent",
>             "type": "ai_tool",
>             "index": 0
>           }
>         ]
>       ]
>     },
>     "AI Agent": {
>       "main": [
>         [
>           {
>             "node": "Agnost AI: session",
>             "type": "main",
>             "index": 0
>           },
>           {
>             "node": "Tool steps",
>             "type": "main",
>             "index": 0
>           }
>         ]
>       ]
>     },
>     "Agnost AI: session": {
>       "main": [
>         [
>           {
>             "node": "Agnost AI: turn",
>             "type": "main",
>             "index": 0
>           }
>         ]
>       ]
>     },
>     "Tool steps": {
>       "main": [
>         [
>           {
>             "node": "Agnost AI: tool calls",
>             "type": "main",
>             "index": 0
>           }
>         ]
>       ]
>     }
>   },
>   "settings": {
>     "executionOrder": "v1"
>   }
> }
> ```

1. Copy the JSON and paste it on an empty n8n canvas with Ctrl+V (Cmd+V on macOS).
2. In each of the three **Agnost AI** nodes, replace `YOUR_AGNOST_ORG_ID` in the `x-org-id` header with your organization ID.
3. Open **Anthropic Chat Model** and select your Anthropic credential, or swap in the chat model you already use.
4. Publish the workflow.

To add Agnost AI to an agent you already have, copy the four Agnost AI nodes
into it. Connect the AI Agent's output to both **Agnost AI: session** and
**Tool steps**, as in the JSON above. Then:

* In the AI Agent node, turn on **Options** > **Return Intermediate Steps**. Without it, **Tool steps** has nothing to split and no tool events are sent.
* If your trigger node is not named `When chat message received`, update the `$('When chat message received')` references in the three Agnost AI bodies.
* If your agent node is not named `AI Agent`, update `$('AI Agent')` in **Agnost AI: turn**.
* Change `primitive_name: 'n8n-ai-agent'` in **Agnost AI: turn** to the agent name you want to see in Agnost AI.

### 3. How the fields map

| Agnost AI field | n8n expression |
| - | - |
| `session_id` | `$('When chat message received').item.json.sessionId` |
| `user_data.user_id` | `$('When chat message received').item.json.metadata?.userId` |
| Turn `args` | `$('When chat message received').item.json.chatInput` |
| Turn `result` | `$('AI Agent').item.json.output` |
| Turn `event_id` | `sessionId + ':' + $execution.id` |
| Tool `primitive_name` | `$json.action.tool` |
| Tool `args` | `JSON.stringify($json.action.toolInput)` |
| Tool `result` | `$json.observation` |
| Tool `parent_id` | the turn's `event_id` |

Agnost AI maps `session_id`, `event_id`, and `parent_id` strings that are not
UUIDs to a fixed UUID, so the same string always points at the same record.

The Chat Trigger has no user field. The n8n embedded chat (`@n8n/chat`) sends
its `metadata` option with every message, so pass the user ID there:

```javascript theme={null}
import { createChat } from '@n8n/chat';

createChat({
  webhookUrl: 'https://your-n8n-host/webhook/<chat-trigger-id>/chat',
  enableStreaming: true, // matches the Chat Trigger's Streaming response mode
  metadata: { userId: currentUser.id },
});
```

A custom client that posts to the chat webhook sends the same fields itself:
`{"action": "sendMessage", "sessionId": "...", "chatInput": "...", "metadata": {"userId": "..."}}`.

Without a user ID, the conversation is still captured, without a user.

### 4. Keep the chat reply working

The Chat Trigger in the workflow uses the **Streaming** response mode, so the AI
Agent streams its reply to the chat and the Agnost AI nodes run after it.

If your Chat Trigger uses **When Last Node Finishes**, n8n sends the output of
the last node that ran as the reply, which would be an Agnost AI node. Switch
to **Streaming**, or end the workflow with an **Edit Fields** node that sets
`output` to `{{ $('AI Agent').item.json.output }}`.

Each Agnost AI node has **Settings** > **On Error** set to **Continue**, so an
ingest error never fails the chat.

## Manual setup: OpenTelemetry

Self-hosted n8n 2.19 and later can export OpenTelemetry traces of workflow and
node executions. n8n marks this feature as Preview. Agnost AI accepts these
spans: AI Agent, chat model, and tool node spans carry GenAI attributes
(`gen_ai.operation.name`, `gen_ai.agent.name`, `gen_ai.request.model`,
`gen_ai.provider.name`, `gen_ai.tool.name`).

The spans carry no message text, no chat session ID, and no user ID. In Agnost
AI, each workflow execution becomes its own conversation with agent, model, and
tool events, their latency, and the model name, but no transcript. Use
OpenTelemetry when you want execution timing. Use the ingest API above for
conversations, users, and patterns. With both turned on, each chat turn also
shows up as a separate trace conversation without text.

### Install

Nothing to install. OpenTelemetry tracing is built into self-hosted n8n 2.19 and later.

### Setup

Set these environment variables on every n8n process (main, workers, and webhook processes) and restart n8n:

```bash theme={null}
N8N_OTEL_ENABLED=true
N8N_OTEL_EXPORTER_OTLP_ENDPOINT=https://otel.agnost.ai
N8N_OTEL_EXPORTER_OTLP_HEADERS=X-Agnost-Org-ID=<your-org-id>
```

n8n appends the default trace path `/v1/traces` and uses OTLP over HTTP/protobuf
by default. Keep both defaults.

By default, n8n traces only production executions. Runs you start from the
editor are not traced. Set `N8N_OTEL_TRACES_PRODUCTION_ONLY=false` to trace
them too.

### Verify

Ingest API:

1. Open the chat for the published workflow and send a message that makes the agent call a tool.
2. In [app.agnost.ai](https://app.agnost.ai), open **Conversations** and find the conversation for the chat session. It shows the user ID you passed in `metadata.userId`.
3. Open the conversation. The turn event shows your message and the agent's reply. The tool event shows the tool name, input, and result, nested under the turn.

OpenTelemetry:

1. Run the published workflow once.
2. Open **Events** in Agnost AI. Within about a minute you see an `AI Agent` agent event, a `chat` event per model call, and an event per tool call, all in one conversation for the execution.

**Settings** > **OpenTelemetry** > **Send test trace** in n8n checks the
connection only. The test span carries no AI attributes, so it does not appear
in Agnost AI.

### Troubleshooting

* **No conversation appears:** open the execution in n8n and check the output of **Agnost AI: session**. A working call returns `{"status": "accepted"}`. A `400` means the `x-org-id` header is missing or is not a UUID.
* **Calls are accepted but nothing appears in your organization:** check that `x-org-id` matches the organization selected in Agnost AI. The ingest API does not check the ID at request time.
* **Every message is a separate conversation:** the trigger is not sending a stable `sessionId`. The n8n chat keeps one `sessionId` per chat. Custom clients must send the same `sessionId` with every message.
* **No tool events:** turn on **Return Intermediate Steps** in the AI Agent options, and check that the AI Agent's output connects to **Tool steps**.
* **The chat shows `{"status":"accepted"}` or an error instead of the reply:** the Chat Trigger uses **When Last Node Finishes**. See [Keep the chat reply working](#4-keep-the-chat-reply-working).
* **No OpenTelemetry events:** confirm the variables are set on the process that runs the execution (workers in queue mode), that the run was a production execution, and that `N8N_OTEL_EXPORTER_OTLP_HEADERS` is `X-Agnost-Org-ID=<your-org-id>`.

### References

* [n8n Chat Trigger node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-langchain.chattrigger)
* [n8n AI Agent node (Tools Agent)](https://docs.n8n.io/integrations/builtin/cluster-nodes/root-nodes/n8n-nodes-langchain.agent/tools-agent)
* [n8n HTTP Request node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.httprequest)
* [n8n Split Out node](https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.splitout)
* [Trace executions with OpenTelemetry in n8n](https://docs.n8n.io/deploy/host-n8n/keep-n8n-running/trace-executions-with-opentelemetry)
* [n8n OpenTelemetry environment variables](https://docs.n8n.io/deploy/host-n8n/configure-n8n/basic-configuration/use-environment-variables/opentelemetry)
* [Agnost AI Capture Session](https://docs.agnost.ai/capture-session.md) and [Capture Event](https://docs.agnost.ai/capture-event.md)

## Next steps

* [Conversations](https://docs.agnost.ai/using-conversations.md): review each chat session with its turns and tool calls.
* [Events](https://docs.agnost.ai/using-events.md): inspect the turn and tool events n8n sent.
* [Patterns](https://docs.agnost.ai/using-patterns.md): organize production conversations by what users wanted.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.