Implement Multi-Model Fallback in n8n

Use a primary model, fall back to a secondary if it fails. Or route by task type: local for simple, cloud for complex.

Scenario

You want: try Ollama first (free, local). If it errors or returns low confidence, fall back to OpenAI.

Step 1: Primary AI node

Add an Ollama node. Set your prompt. Run the workflow. If it succeeds, continue. If it fails, you need to catch that.

Step 2: Error trigger

n8n doesn't have a built-in "on error, try this" for the same node. Use a workaround: run the Ollama node in an Execute Workflow sub-workflow. The parent catches errors from the sub-workflow.

Or: use two branches. Branch A: Ollama. Branch B: OpenAI. Use an IF after a "try" step—but n8n's flow is linear. The cleanest approach: use a Code node that calls Ollama, catches errors, then calls OpenAI. Or use the HTTP Request node with error handling.

Step 3: HTTP Request approach

Use HTTP Request nodes instead of built-in AI nodes. First request: Ollama API. On error (or on 4xx/5xx), route to a second HTTP Request: OpenAI API. Map the same prompt. Merge the outputs.

Step 4: Route by task type

Alternative: use an IF node before the AI step. "If {{ $json.task_type }} is 'simple', use Ollama. Else use OpenAI." Two parallel branches, each with a different AI node; only one runs based on the condition.

Step 5: Logging

Log which model was used and why (primary success, fallback, or routed). Helps with cost analysis and debugging.

Discussion

  • Loading…

← Back to Tutorials