Agentic Design Patterns using N8N – Routing Pattern

The routing pattern directs an incoming request to a suitable handler. A model or predefined rules determine the request’s category, then the application selects the instructions, tool, or workflow that should process it.

In the Java implementation, classes and conditional logic perform these steps. This example uses n8n nodes and connections to express the same flow: classify a request, select a branch, and generate a response.

Info: The workflow and supporting documentation are available in the project’s n8n directory.

Organizing the workflow

Two Basic LLM Chain nodes handle the model calls. The first classifies each request. A Switch node after the first LLM node selects a branch containing the appropriate handler instructions. All three branches connect to a shared second chain that generates the answer. The final nodes format each response, convert it into a text file, and save it to disk.

The overview shows the complete path from the Manual Trigger to file output. The model connections beneath the LLM Chains supply model configuration.

CategoryResponsibility
BOOKINGAcknowledge requests concerning flights or hotels.
INFOAnswer general knowledge questions.
UNCLEARRespond to ambiguous requests or requests outside the other categories.

Info: The booking branch does not make reservations, and the information branch does not search the web. This keeps the example focused on routing.

Creating the sample requests

Execute Routing Example is a Manual Trigger. It starts the workflow and passes control to Requests, a Code node that creates the three sample items. Coordinator Instructions then adds the classification prompt.

The connection labels show the transition from one trigger item to three request items. Each request travels through the classification and routing steps independently.

return [
  { json: { request: "Book me a flight to London." } },
  { json: { request: "What is the capital of Italy?" } },
  { json: { request: "Do that thing for me." } }
];

The Code node is configured to Run Once for All Items. It returns three separate n8n items, each with a json.request field.

Defining the coordinator’s instructions

Coordinator Instructions is an Edit Fields node in JSON mode. It adds an INSTRUCTIONS field containing the classification prompt.

{
  "INSTRUCTIONS": "Classify the user's request into exactly one category:\nBOOKING: Requests related to booking flights or hotels.\nINFO: General information questions.\nUNCLEAR: Ambiguous requests or requests outside those categories.\nReturn only BOOKING, INFO, or UNCLEAR. Do not answer the request itself."
}

Include Other Input Fields is enabled, so the original request remains in the output. The screenshot shows both fields on each item. This JSON defines workflow fields; it does not constrain the model’s answer to a JSON schema.

Configuring the model

The OpenAI Chat Model supplies credentials and model settings to the first chain. The screenshot selects gpt-5-mini and enables the Responses API. Select an OpenAI credential and a model available to your account.

The model is connected to the chain’s Model input, as shown below. It supplies the language model used by the chain.

Classifying each request

The first Basic LLM Chain acts as the coordinator. Set the prompt source to Define below, use the current request as the user prompt, and add a System message containing the classification instructions.

SettingValue
Prompt (User Message){{ $json.request }}
System message{{ $json.INSTRUCTIONS }}
Require Specific Output FormatOff

The output pane shows three items whose text values are BOOKING, INFO, and UNCLEAR. The coordinator returns the classification rather than answering the original request. The Basic LLM Chain documentation describes these prompt and message settings.

Selecting a route

Route by Request Category is a Switch node. Its three outputs connect to Booking Instructions, Info Instructions, and Unclear Instructions.

Use Rules mode and compare the classifier’s returned text with each supported category. Each rule reads the following expression:

{{ $json.text.trim() }}
ComparisonOutput
Is equal to BOOKINGBOOKING
Is equal to INFOINFO
Is equal to UNCLEARUNCLEAR

The model determines the category. The Switch performs the comparison and sends the item through the matching connection without making another model call. The screenshot shows one item on each category output.

trim() removes surrounding whitespace. The comparisons remain case-sensitive and do not accept explanatory sentences or quoted category names. The exported workflow has no fallback output, so unexpected classification text does not reach a handler. UNCLEAR is an explicit category, not a fallback for malformed output. See the Switch documentation for fallback options.

Preparing the selected handler

Each instruction branch uses an Edit Fields node to prepare request, category, and handlerInstructions. The booking branch is shown below.

The classifier returns its answer in text. To recover the original request, the branch references the linked item from Coordinator Instructions:

{{ $('Coordinator Instructions').item.json.request }}

n8n’s item linking associates the classification with the request that produced it. Using .item preserves that relationship rather than retrieving the first sample request for every branch.

{{ JSON.stringify({
  request: $('Coordinator Instructions').item.json.request,
  category: $json.text.trim(),
  handlerInstructions: "You are a booking assistant. Acknowledge the user's travel request briefly. Do not ask questions or request additional details. Do not claim to have booked anything or checked live availability."
}) }}

JSON.stringify() produces the JSON text expected by this node’s JSON-mode parameter. The resulting output exposes the three values as workflow fields.

The information branch supplies instructions to answer briefly, acknowledge uncertainty, and avoid claims of live research. The unclear branch asks for the fixed answer Your request was unclear. without follow-up questions.

Generating the answer

All three instruction nodes connect to Handle Request, a second Basic LLM Chain with its own connected OpenAI Chat Model node.

The selected branch supplies the system instructions, while the original request remains the user prompt. Set the prompt source to Define below and configure these fields:

SettingValue
Prompt (User Message){{ $json.request }}
System message{{ $json.handlerInstructions }}

The input pane exposes the branch data, and the output pane shows the generated text. The selected run in this screenshot includes additional wording after the unclear response. That differs from the requested fixed answer and illustrates why successful node execution still requires output review.

The shared chain keeps model-call configuration in one place. The branches vary its behavior through their instructions. These converging connections do not collect all answers into one item or synchronize the branches. The chain processes the items arriving from each branch.

Normal successful processing of the three samples requires six model calls: three classifications and three responses, excluding retries. The unclear branch also makes a second call, even though its instructions request fixed wording.

Formatting and saving responses

The final three nodes separate presentation from response generation. Format Final Response creates a String field named message with a heading, the handler’s returned text, and a footer. This step formats the answer without making another model call.

Plain Text Response uses Convert to File with Convert to Text File selected. Its Text Input Field is message, and the output binary field is data. The input setting contains the field name, not an expression containing the text. The binary output can be viewed or downloaded.

Read/Write Files from Disk then writes that binary data using this filename expression:

{{ '/files/output/output-' + $execution.id + '-' + ($runIndex + 1) + '.txt' }}

The execution ID distinguishes workflow executions. The run index distinguishes separate invocations of the writing node within an execution. For the three sample requests taking separate branches, filenames can look like:

Running the example

Import the workflow JSON, then select your OpenAI credential in both model nodes.

Info: This example runs in Docker. /files/output is a container path mapped to a host folder by the project’s Compose configuration. Confirm that the mount exists and is writable before running the workflow.

The credential screen below shows a successful connection test. Store the key in n8n’s credential configuration rather than in workflow fields.

Start Execute Routing Example, then inspect the classification output, selected branches, generated responses, and saved files. Execution makes live model requests and writes output files.

RequestExpected route
Book me a flight to London.BOOKING
What is the capital of Italy?INFO
Do that thing for me.UNCLEAR

Successful execution shows that the nodes completed. Review the generated text to confirm that each answer follows its selected instructions. This example uses ordinary text classification and has no configured fallback route or conversation memory.

Info: View the complete workflow as JSON at routing-example-workflow.json.

Summary

The n8n implementation separates classification, routing, and response generation into visible workflow steps. A Basic LLM Chain classifies each request, a Switch selects the instructions, and a shared second chain generates the answer. The same routing pattern used in Java is expressed through items, expressions, and node connections, with each stage available for inspection.

Info: Codex generated and edited substantial portions of this document.

One thought on “Agentic Design Patterns using N8N – Routing Pattern”

  1. Pingback: Agentic Design Patterns Using Java - Routing Pattern - Constructing Truth Digitally - Automating Intelligence

Leave a Reply

Your email address will not be published. Required fields are marked *