Control flow · Established
Router
Also known as: Content-based router, Dispatcher, Classifier routing
A router inspects each incoming request or intermediate state and sends it to exactly one of several downstream paths, based on rules, a classifier or a model decision.
01Problem it solves
Different inputs need different handling, but sending everything through one universal path is slow, expensive or inaccurate, and callers should not have to know which handler to choose.
02Use when / Avoid when
Use when
- Inputs fall into distinguishable classes that need different handlers, tools or models.
- You want to send cheap cases to cheap paths and hard cases to expensive ones.
- The branch decision is a single step; after routing, the chosen path runs on its own.
Avoid when
- Most inputs need several handlers; use fan-out/fan-in instead.
- The chosen handler must keep consulting a coordinator; that is a supervisor.
- Routing criteria cannot be made observable or testable.
03How it works
- 1ReceiveAn input or state arrives at the router.
- 2ClassifyRules, a classifier or a model assign it to a route, with a default for unknown cases.
- 3DispatchThe input is forwarded to the selected path.
- 4RecordThe routing decision is logged so misroutes can be measured.
04Capabilities
| Capability | What it means |
|---|---|
| Runtime branching | The next step is chosen at runtime from the current state, input or classification result. |
05Tradeoffs
| Aspect | You gain | You pay |
|---|---|---|
| Efficiency | Each input takes only the path it needs. | Classification adds a step and can be wrong. |
| Simplicity | Downstream handlers stay specialised and independent. | Route definitions drift as new cases appear. |
06Failure considerations
| Failure mode | Mitigation |
|---|---|
| Misclassification sends inputs to the wrong handler silently. | Log decisions, sample-review them and add a confidence threshold with a safe default route. |
| No route matches and the input is dropped. | Always define a fallback route and alert on its use. |
| LLM-based routing is non-deterministic between runs. | Constrain output to an enumerated set of routes and evaluate on a fixed test set. |
07Implementations
Examples of products and frameworks that implement this pattern. Listed as evidence, not endorsement.
| Implementation | Mechanism |
|---|---|
| LangGraphAgent framework | Conditional edges choose the next node from current state. |
| CrewAIAgent framework | In Flows, the @router decorator selects the next step from state. |
| Apache AirflowWorkflow scheduler | Branching tasks select which downstream tasks run. |
| AWS Step FunctionsCloud workflow service | Choice states branch on input data. |
| CamundaProcess orchestration (BPMN) engine | Exclusive gateways choose one path from process variables. |