DEV Community

Longlong GUO
Longlong GUO

Posted on

n8n vs Custom Code for AI Agent Workflows: When Each One Wins

n8n vs Custom Code for AI Agent Workflows: When Each One Wins

Low-code tools are great until they aren't. Here's the honest breakdown of when to use n8n and when to write Python.


Every team building AI agents eventually faces this choice: use a visual workflow tool like n8n or write custom code. The answer isn't "it depends" — it's "here's exactly when each one wins."

I've built agent workflows both ways for clients. Here's what I've learned about where the line sits.

Where n8n wins

1. You need it running today. n8n's visual editor means a working prototype in 30 minutes. Drag a webhook trigger, add an OpenAI node, connect to Slack. Done. Custom code takes half a day minimum.

2. Non-developers need to maintain it. If your ops team or a PM needs to modify the workflow after you ship it, n8n's visual interface is a feature. Python code is a liability if nobody can read it.

3. The workflow is mostly integrations. "When a form is submitted, call GPT-4, then post to Slack, then update Airtable" — this is n8n's sweet spot. Every integration is a pre-built node with error handling baked in.

4. You need quick iteration on prompts. Editing a prompt in n8n's UI is faster than deploying code. For early-stage products where prompts change daily, this matters.

Where custom code wins

1. You need bounded retries with tier escalation. n8n's error handling is basic — retry N times or go to error branch. If you need "retry 3 times on 429 with exponential backoff, then escalate to a stronger model on the 4th failure," you're fighting the tool. In Python, it's a 10-line loop.

2. The workflow has loops or conditional branching. n8n supports these, but complex branching quickly becomes unreadable spaghetti. If your agent needs to iterate over a list of items, call different models per item, and merge results, code is cleaner.

3. You need JSONL traces for debugging. n8n logs execution data, but extracting it into a structured trace format for replay is awkward. In code, you append one JSON line per step — trivial.

4. Cost per execution matters. n8n cloud charges per workflow execution. At scale (10k+ runs/month), self-hosted code is significantly cheaper. Even self-hosted n8n adds overhead.

5. You need custom auth patterns. Rotating API keys, provider failover with circuit breakers, token bucket rate limiting — these are code patterns, not drag-and-drop nodes.

The hybrid approach

The best production systems I've built use both:

n8n (orchestration layer)
  ├── Webhook trigger
  ├── Auth validation
  ├── Call Python microservice (the actual agent logic)
  ├── Format response
  └── Send notification
Enter fullscreen mode Exit fullscreen mode

n8n handles the "boring" parts: webhooks, auth, notifications, database updates. The Python microservice handles the "hard" parts: model routing, retries, cost tracking, traces.

This gives you n8n's speed of iteration for the simple stuff and code's power for the complex stuff.

The decision framework

Ask these five questions:

  1. Can a non-developer maintain it? → n8n
  2. Is it mostly API integrations? → n8n
  3. Does it need bounded retries or model routing? → Code
  4. Will it run >1000 times/month? → Code (cost)
  5. Does it need JSONL traces or custom auth? → Code

Two or more "code" answers? Write code. Otherwise, n8n is fine.

What I actually build for clients

For most clients, I build the agent logic in Python (routing, failover, traces, cost tracking) and wire it into n8n or a simple API endpoint for orchestration. This gives them the best of both worlds.

The health probe that powers the routing layer is open-source: llm-endpoint-probe on GitHub — zero dependencies, pure Python stdlib.

If you want an agent workflow built for your stack — n8n, Node.js, or Python — that's what I do on Fiverr: routing, failover, traces, and a handover doc so your team can maintain it.


Previous posts: the health probe, model routing, agent workflow checklist, failover in practice, and LLM cost tracking.

Top comments (0)