# Routing

Routing determines whether a message is handled by a Pipeline or an Agent. The decision is made at the very beginning of the flow and applied after preprocessing completes.

### How Routing Works — Two-Pass Design

Routing is not a single step — it happens in two passes through the same `Route` processor:

**First pass (at the start):** The system reads the chatbot configuration and decides the destination — Pipeline or Agent. This decision is stored internally as `PREROUTE_NEXT_STATE_KEY` but not yet applied. The flow continues normally through input processing, validation, and preprocessing.

**Second pass (after preprocessing):** After preprocessing finishes, the flow returns to `Route`. This time, the stored destination is applied and the message is directed to the correct handler.

### When Does Each Path Run?

| Condition                          | Destination                                        |
| ---------------------------------- | -------------------------------------------------- |
| Chatbot has `agent_ids` configured | **Agent**                                          |
| Standard chatbot (no agent IDs)    | **Pipeline**                                       |
| Deep research search type enabled  | **Pipeline** (with a specialized pipeline handler) |
| GLChat Live                        | **Pipeline** (with the Live pipeline handler)      |

### Pipeline vs Agent

<table><thead><tr><th width="221.5"></th><th>Pipeline</th><th>Agent</th></tr></thead><tbody><tr><td><strong>Behavior</strong></td><td>Structured, deterministic flow — retrieval then generation</td><td>Flexible, multi-step — can call tools, loop, reason</td></tr><tr><td><strong>Best for</strong></td><td>Knowledge base Q&#x26;A, document search, structured data queries</td><td>Complex tasks requiring tool use, multi-step reasoning</td></tr><tr><td><strong>Response</strong></td><td>Streamed directly from generation</td><td>Streamed after agentic process completes</td></tr><tr><td><strong>Goes to postprocessing</strong></td><td>Yes</td><td>Yes</td></tr></tbody></table>

### Future: Smart Routing

The current routing model is **configuration-driven** — the destination (Pipeline or Agent, and which specific pipeline) is determined entirely by the chatbot's static configuration at the time the request arrives.

A planned improvement is **smart routing**: instead of relying solely on preconfigured rules, the system would analyze the content and intent of each incoming message at runtime and automatically select the most appropriate pipeline or agent. For example:

* A factual question about internal documents → Standard RAG
* A complex research query → Deep Research
* A task requiring tool use or multi-step reasoning → Agent
* A general conversation with no knowledge base → No-Op

This would allow a single chatbot configuration to adapt its execution path dynamically based on what the user is actually asking, rather than routing every message through the same fixed pipeline.

{% hint style="info" %}
Smart Routing is not implemented yet and we will update this documentation once it's implemented
{% endhint %}
