Dynamic Anthropic Model Routing and Web Search Agent System - n8n Workflow

Use this advanced n8n workflow to deploy a dynamic AI assistant powered by Anthropic's Claude Sonnet 4 and Opus 4. It includes intelligent routing, web search, memory, and tool integration, leveraging multiple n8n node types for optimal performance.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Automation Specialists: Seeking advanced LangChain implementations within n8n.

  • AI Developers: Needing to dynamically select between high-tier and standard Anthropic models based on query complexity.

  • Technical Content Managers: Requiring an intelligent Q&A system that can perform real-time web searches.

  • Anyone looking for robust n8n templates for building complex AI assistants.

Overview

This automation solves the challenge of optimizing resource usage and latency when interacting with expensive, powerful large language models like Anthropic Opus 4. Instead of sending every query to the highest-tier model, this intelligent n8n workflow uses a dedicated routing agent to analyze the user's prompt.

If the request is complex, critical, or requires deep reasoning (as defined in the system prompt), it is routed to Opus 4. For standard, routine tasks, it uses Sonnet 4. This robust decision-making is implemented entirely using n8n node architecture and LangChain concepts, providing an efficient and flexible AI solution. This powerful n8n template showcases complex conditional logic and tool integration, making it an essential resource for advanced automation.

How it Works

This high-level n8n workflow operates in a multi-stage process:


  1. Trigger and Ingestion: The process starts with the When chat message received n8n trigger, which captures the user's input and session ID.

  2. Routing Decision: The input is immediately passed to the Anthropic Routing Agent. This specialized n8n agent uses a Sonnet 3.7 model and highly specific system instructions to determine whether the user query requires the complexity of Opus 4 or the efficiency of Sonnet 4.

  3. Structured Output: The Structured Output Parser n8n node validates and extracts the routing agent's decision, ensuring the output contains a clean prompt and the selected model name in JSON format.

  4. Main Agent Execution: The parsed output feeds into the AI Agent n8n node. This agent is configured to dynamically load the selected model (Sonnet 4 or Opus 4) and utilizes multiple tools, including websearch, Think, and Calculator.

  5. Context and Tools: The agent uses Simple Memory1 (Buffer Window) to maintain conversation context across multiple turns. If the query is factual or requires current information, the agent's system prompt directs it to utilize the websearch tool.

  6. Response: The main n8n agent generates the final, context-aware response based on the LLM output, memory, and tool results, completing the n8n workflow.

Installation Guide

To deploy this comprehensive n8n template, follow these steps:


  1. Import the n8n Workflow: Copy the provided JSON code and import it into your n8n instance via the 'New' -> 'Import from JSON' option.

  2. Set Anthropic Credentials: You must configure credentials for the Anthropic API service. This n8n workflow uses the 'Anthropic account' credential named NNTZAD0Gmf7lcniq in the provided JSON. Replace this with your valid API key credentials across all nodes that require it (Sonnet 4 or Opus 4, Sonnet 3.7, and web_search).

  3. Activate the Trigger: Ensure the When chat message received n8n trigger is correctly linked to your chat application endpoint (e.g., Slack, custom chat interface).

  4. Activate the n8n Workflow: Set the workflow status to 'Active' to start listening for incoming chat messages.

Node Details

This advanced n8n workflow utilizes several key nodes:

When chat message received (n8n trigger):
Function: Serves as the starting point, listening for incoming messages from a user interface (e.g., Slack or custom endpoint).
Key Configuration: A webhook ID is configured to receive incoming chat messages.
Anthropic Routing Agent (n8n node):
Function: A crucial LangChain agent responsible for analyzing the user query and selecting the optimal Anthropic model (Opus 4 or Sonnet 4) to handle the request.
Key Configuration: System message explicitly defines model strengths and strictly mandates a JSON output format.
Structured Output Parser (n8n node):
Function: Guarantees that the output from the Routing Agent conforms to a strict JSON schema (containing prompt and model fields), ensuring reliability for the subsequent n8n node.
AI Agent (n8n node):
Function: The primary execution agent. It dynamically receives the selected Anthropic model name and the user prompt, then orchestrates the use of connected tools and memory.
Key Configuration: The model parameter is set dynamically using an expression: ={{ $json.output.model }}.
Sonnet 4 or Opus 4 (n8n node):
Function: The dynamic Anthropic language model node. It executes the final generation using the model selected by the routing agent.
Key Configuration: Model selection is driven by the dynamic expression: ={{ $json.output.model }}.
web_search (n8n node):
Function: An HTTP Request Tool configured to perform web searches, vital for providing up-to-date information within the primary AI Agent's thought process.
* Key Configuration: Configured to interact with the Anthropic API, passing the search query and the dynamically selected model.

Related n8n Workflows

Free

Nodes: 9 Nodes
Updated: December 26 2025
View all
Created by

Full-stack Web Developer based in Italy specialising in Marketing & AI-powered automations. For business enquiries, send me an email at [email protected] or add me on Linkedin.com/in/davideboizza

Featured*