Router
On this page
A hierarchical, hybrid-scoring query router for LLM applications.
router (part of the Zerda ecosystem) decides which piece of code should handle a user's query — a RAG pipeline, a tool call, a chat handler, or anything else you register — using a two-level decision process:
- Group selection — pick the best-matching
RouteGroup(e.g.rag,tools,chat) based on the group's intent examples and keywords. - Route selection — within that group, pick the best-matching
Routeusing a hybrid of semantic similarity, keyword signals, and (optionally) an LLM classifier.
Every decision is scored, cached, logged, and fed back into an adaptive bias system so routing accuracy improves as your application is used.
Why a router?
LLM applications typically need to dispatch a user's message to one of many possible handlers — retrieve from a knowledge base, call a tool, run code, or simply chat. Doing this well means answering three questions on every request:
- Which handler is the best fit? — combining embedding similarity with cheap keyword heuristics avoids paying for an LLM call on every message.
- How confident are we? — a low-confidence decision should behave differently from a high-confidence one (route with a warning, fall back to an LLM classifier, or give up entirely).
- What happens after? — retries, timeouts, streaming, and multi-route execution all need a consistent contract regardless of which handler was chosen.
router answers all three with a single, typed API surface.
Core capabilities
A minimal example
from zerda.router import HierarchicalRouter, RouteGroup, Route
from zerda.router import BaseHandler, HandlerRequest, HandlerResponse
class GreetHandler(BaseHandler):
def handle(self, request: HandlerRequest) -> HandlerResponse:
return HandlerResponse.ok(f"Hello! You said: {request.query}")
router = HierarchicalRouter()
chat = RouteGroup("chat", "General conversation")
chat.add_route(Route(
name="greet",
description="Greet the user",
handler=GreetHandler(),
examples=["hello", "hi there", "good morning"],
))
router.register_group(chat)
result = router.route_query("Hey, how are you?")
print(result.content)
Continue to Installation to get set up, or jump straight to the Quickstart for a fuller walkthrough.
Documentation map
- Installation — dependencies and setup.
- Quickstart — build a working router in a few minutes.
- Architecture — how the pieces fit together, with diagrams.
- Concepts — routes, groups, handlers, scoring, confidence.
- Configuration — every configuration option, documented.
- Advanced Usage — streaming, tool chaining, feedback tuning.
- API Reference — every public class and function.
- Tutorials — step-by-step guides.
- Cookbook — production recipes.
- FAQ / Troubleshooting
[!NOTE] Scope of this documentation This documentation covers the
routerpackage as found in the analyzed source tree (router/). It does not cover other zerda packages (chain,embeddings,document_loaders, etc.) except whererouterdirectly imports from them.