It is recommended to study this section together with the tutorial Branch Structures.
Purpose
In complex RAG reasoning tasks, it is often necessary to dynamically determine the next execution path based on intermediate results (for example, the model’s current output or retrieval results).The Router Server is designed for exactly this purpose — it evaluates the current state based on input information and returns a custom branch label (state identifier) used to drive branching and dynamic control within the Pipeline.
Implementation Example
The following example demonstrates how to implement a simple Router Tool. Suppose that in the current RAG workflow, the model needs to decide whether the retrieved documents already contain sufficient information to answer the question:If the information is sufficient, the process stops; otherwise, it continues retrieval. Here’s how to implement a Router Tool:
servers/router/src/router.py
continue: Information is insufficient — continue retrieval.stop: Information is sufficient — terminate the process.
Usage Example
The definedRouter Tool must be used in combination with the branch: and router: structures to enable state-based dynamic branching.
When
router.check_model_state detects that the model’s output contains the <search> tag, it enters the continue branch to perform another retrieval;otherwise, it follows the
stop branch to exit the loop.