⚒️ Route AI prompts between Anthropic, Google Gemini, Mistral and OpenAI

⚡ 77 views · ⚒️ Engineering

Description

Quick overview

A reusable sub-workflow that routes text prompts to Anthropic Claude, Google Gemini, Mistral, or OpenAI and returns a normalised response - including estimated token usage and cost - regardless of the provider. Drop it into any workflow via Execute Workflow to add LLM capability without rebuilding the API plumbing each time.

How it works

  1. The workflow is called as a sub-workflow from other workflows using n8n’s Execute Workflow node. It accepts a prompt payload and routes it through five stages:
  2. Initialise — a CONFIG node exposes all defaults (provider, model, temperature, token limit, response format) in one place.
  3. Normalise & validate — the inbound payload is merged with CONFIG defaults, prompts are coerced to strings and stripped of null bytes, and model-specific quirks are handled (OpenAI o-series reasoning models omit temperature; GPT-5.x uses max_completion_tokens).
  4. Token budget check — an estimated input token count is compared against the selected model’s context limit. If the prompt would exceed 70% of the limit, the workflow errors early with a clear message rather than sending an oversized request.
  5. Route to provider — a Switch node dispatches to one of four HTTP Request nodes, each pre-configured for its provider’s API shape. Google Gemini enforces application/json response MIME type when JSON output is requested; all other providers use prompt-based JSON guidance.
  6. Merge & return — a single Code node extracts the response text from whichever provider ran, normalises it (strips code fences, optionally repairs malformed JSON), pulls actual token counts from the API response, calculates an estimated cost, and returns a consistent output shape regardless of provider.
  7. Every call returns the original payload plus:
FieldDescription
llm_responseModel text output, or [API ERROR] ... on failure
model_usedModel that handled the request
provider_usedProvider that handled the request
llm_response_lengthCharacter length of llm_response
llm_response_emptytrue if response is empty or an API error occurred
_input_tokensInput token count - actual from API if available, otherwise estimated
_output_tokensOutput token count - actual from API if available, otherwise estimated
_estimated_cost_usdEstimated cost in USD. null if model not in pricing table

Setup

  1. Create credentials for each provider you plan to use: Anthropic API, Google AI (PaLM) API for Gemini, Mistral Cloud API, and/or OpenAI API.
  2. Set your preferred defaults in the CONFIG step (default provider, default model per provider, temperature, max tokens, and response format).
  3. Call this workflow from other workflows using Execute Workflow and pass at minimum a userPrompt field (optionally systemPrompt, llm_provider, model overrides, temperature, max_tokens, and response_format).
  4. Activate the workflow so it can be executed as a sub-workflow.

Customization

Additional info

Who’s it for

🔗 Nodes Used

HTTP Request, Execute Workflow Trigger

📥 Import

Download workflow.json and import into n8n: Workflow menu → Import from File

📖 Importing guide · 🔑 Credential setup