# n8n Webhook-Triggered AI Agent

> Expose an n8n AI Agent (with memory and tools) behind a webhook that returns the answer synchronously.

Category: n8n Automation Blueprints  
Tags: n8n, webhook, ai-agent, api  
Setup time (estimate): 1 hour  
Estimated cost: n8n plus LLM tokens per request.  
Last verified: 2026-10-14

## Nodes

1. **Webhook** (POST `/agent`, *Respond: Using 'Respond to Webhook' node*).
2. **AI Agent** (`@n8n/n8n-nodes-langchain.agent`), prompt from `{{ $json.body.message }}`.
   - Sub-node **Chat Model** (OpenAI, Anthropic, Ollama, ...).
   - Sub-node **Window Buffer Memory** keyed on `{{ $json.body.session_id }}`.
   - Tool sub-nodes, e.g. **HTTP Request Tool**, **Calculator**.
3. **Respond to Webhook** – JSON `{ "reply": "{{ $json.output }}" }`.

## Test

```bash
curl -X POST https://YOUR-N8N/webhook/agent \
  -H 'Content-Type: application/json' \
  -d '{"session_id":"u123","message":"What is 17% of 2,340?"}'
```

## Hardening

- Add Header Auth on the Webhook node (shared secret) before exposing it publicly.
- Set a max iterations limit in the AI Agent options.
- Use the test URL (`/webhook-test/`) during development, and activate the workflow for the production URL.

## Failure modes

- Unauthenticated webhooks let anyone spend your tokens; add header auth.
- Agent loops on tool errors without an iteration limit.
- Test vs production webhook URLs differ; activate the workflow before going live.
