AI Automation
Integrating Cloudflare Workers AI with n8n: A Practical Guide for Small Companies
TL;DR: Use a Cloudflare Workers AI endpoint as an HTTP request node in n8n, store the API token in n8n’s encrypted credentials, design concise prompts, add retry logic, and monitor usage with Cloudflare analytics and n8n’s execution logs.
Why Connect Cloudflare Workers AI to n8n?
n8n gives you a visual, self‑hosted workflow engine that can call any HTTP API. Cloudflare Workers AI provides low‑latency, on‑edge LLM inference without managing servers. By wiring the two together you get a cheap, scalable AI layer that can be triggered from spreadsheets, webhooks, or internal tools—all while keeping data inside your own network.
Prerequisites
- A Cloudflare account with Workers AI enabled (see the Workers AI documentation).
- An n8n instance – self‑hosted or Cloud (see the n8n documentation).
- Basic knowledge of HTTP request formatting and JSON.
Step 1 – Create a Workers AI Model Endpoint
1. In the Cloudflare dashboard go to Workers & Pages → Workers → Create a Worker.
2. Choose the AI template. The generated script contains a fetch handler that forwards the request to the selected model.
3. Replace the placeholder model name with the one you need, for example @cf/meta/llama-2-7b-chat-fp16 (see the Models list).
4. Deploy the worker and note the URL – it will look like https://my‑worker.my‑account.workers.dev/.
Step 2 – Secure the API Token
Cloudflare authenticates API calls with an Authorization: Bearer <TOKEN> header. Never hard‑code the token in the workflow.
- Generate a token with the Workers AI → Tokens page. Grant only the
Workers AI:Readpermission. - In n8n, go to Credentials → New Credential → HTTP Header Auth and store the token as
Bearer YOUR_TOKEN. n8n encrypts credentials at rest. - Reference the credential in the HTTP Request node – this keeps the token out of the workflow definition.
Step 3 – Build the n8n Workflow
Below is a minimal workflow that receives a text payload, forwards it to Workers AI, and returns the generated answer.
{
"nodes": [
{
"name": "Trigger",
"type": "n8n-nodes-base.webhook",
"parameters": { "path": "ai-input" }
},
{
"name": "Call Workers AI",
"type": "n8n-nodes-base.httpRequest",
"parameters": {
"url": "{{ $credentials.workerUrl }}",
"method": "POST",
"jsonParameters": true,
"options": {
"bodyContentType": "json",
"retryOnFail": true,
"maxTries": 3,
"retryInterval": 2000
},
"bodyParametersJson": "{{
\"messages\": [{\"role\": \"user\", \"content\": $json[\"text\"]}]
}}"
},
"credentials": { "httpHeaderAuth": "WorkersAI" }
},
{
"name": "Respond",
"type": "n8n-nodes-base.httpResponse",
"parameters": { "responseCode": 200 }
}
],
"connections": {
"Trigger": { "main": [[{"node": "Call Workers AI", "type": "main"}]] },
"Call Workers AI": { "main": [[{"node": "Respond", "type": "main"}]] }
}
}
Key points:
- Use the
jsonParametersflag so n8n sends a proper JSON body. - The
retryOnFailoptions protect against transient edge errors. - Keep the prompt short – Workers AI charges per token, and shorter prompts reduce latency.
Step 4 – Prompt Design for Consistency
Even a simple chat model can drift. Adopt a “system prompt” pattern:
{
"messages": [
{"role": "system", "content": "You are a concise assistant that replies in plain English and never includes markup."},
{"role": "user", "content": "{{ $json[\"text\"] }}"}
]
}
Store the system message in an n8n Set node so you can update it centrally without editing the HTTP request.
Step 5 – Error Handling and Fallbacks
Workers AI can return 429 (rate‑limit) or 500 errors. In n8n you can add a IF node after the request to inspect statusCode. If the call fails, route the payload to a fallback node – for example a static response or an email alert to the ops team.
Step 6 – Observability and Cost Monitoring
Both platforms expose metrics:
- Cloudflare: The Workers AI analytics page shows request count, token usage, and latency per worker.
- n8n: Execution logs (available in the UI or via the
/executionsAPI) record request/response payloads and duration.
Set up a weekly n8n workflow that pulls the Cloudflare analytics endpoint (requires a separate API token with Analytics:Read) and sends a summary to Slack or email. This keeps costs transparent for founders.
Step 7 – Maintenance Checklist
- Rotate the Workers AI token every 90 days and update the n8n credential.
- Review model deprecation notices – Cloudflare occasionally retires older models.
- Test prompt changes in a sandbox worker before pushing to production.
- Verify that the webhook URL is protected by basic auth or IP allow‑list.
- Archive execution logs older than 30 days to comply with data‑retention policies.
FAQ
- Can I use a private LLM instead of Cloudflare’s hosted models? Yes, but you would need to host the model yourself (e.g., on a Cloudflare Workers KV cache) and expose a custom endpoint. The integration steps remain the same – only the worker script changes.
- Do I need to worry about GDPR when sending user data to Workers AI? Cloudflare processes data in the region you select for the worker. Ensure you choose a region that complies with your jurisdiction and avoid sending personally identifiable information unless you have a lawful basis.
- What’s the latency difference between Workers AI and calling OpenAI directly? Workers AI runs at edge locations, typically 50‑150 ms for short prompts, whereas OpenAI’s public API averages 300‑500 ms. The exact numbers depend on model size and network conditions.
- How do I limit token usage per request? Include a
max_tokensfield in the request body. Example:{"max_tokens": 200}. This prevents runaway costs. - Can I chain multiple AI calls in a single n8n workflow? Absolutely. Use the output of one
HTTP Requestnode as the input to the next. Just watch the cumulative token count.
Want this kind of automation built for your workflow?
AISecAll designs, builds, deploys, and maintains focused AI automations for small companies and independent entrepreneurs.