Module 3 · Index types
Composable indexes and routing
4 min read
Lumen now has three indexes, each excellent for one type of question and mediocre for the others. What's left is to decide, for each question, which one to query. That's the role of routing: a component reads the question and each engine's description, then chooses. This lesson builds Lumen's router and gives the rules for describing engines so that a model picks them well.
Engines described as tools
Routing rests on an abstraction you'll meet again with agents (module 5): the QueryEngineTool, a query engine with a name and a description.
def tools(knowledge, summaries, catalog) -> list[QueryEngineTool]:
return [
QueryEngineTool.from_defaults(
knowledge_engine(knowledge), name="knowledge_base",
description="Questions précises sur un produit, une règle ou une procédure : "
"garantie d'un article, délai de retour, frais de livraison, "
"réglages, entretien, montage.",
),
QueryEngineTool.from_defaults(
summaries.as_query_engine(llm=models.main_llm(), response_mode="tree_summarize"),
name="policy_summaries",
description="Résumé ou vue d'ensemble d'une ou plusieurs politiques "
"(retours, garantie, livraison) ; comparaison entre politiques.",
),
QueryEngineTool.from_defaults(
catalog.as_query_engine(llm=models.main_llm(), include_text=False),
name="product_relations",
description="Relations entre produits et fournisseurs : qui fabrique un produit, "
"de quel pays, quels produits sont compatibles entre eux.",
),
]
The routing model sees only these descriptions. They are therefore the decisive factor in routing quality — exactly like tool descriptions for an agent.
The RouterQueryEngine
from llama_index.core.query_engine import RouterQueryEngine
from llama_index.core.selectors import LLMSingleSelector
def router(query_tools: list[QueryEngineTool]) -> RouterQueryEngine:
return RouterQueryEngine(selector=LLMSingleSelector.from_defaults(llm=models.fast_llm()),
query_engine_tools=query_tools, llm=models.main_llm())
The selector presents the question and the numbered list of descriptions to the model, which returns its choice and a justification. The router then forwards the question to the chosen engine. The choice can be read in the response, which is precious for debugging and evaluation:
response = await kb.router.aquery("Qui est le fournisseur de la chaise CHAIR-7 ?")
selected = response.metadata["selector_result"] # index of the chosen engine and justification
engine = kb.tools[selected.ind].metadata.name # "product_relations"
The test_router_picks_the_right_engine test checks three cases, one per engine. Lumen's HTTP service returns the engine used with each answer.
Selectors
| Selector | How it works |
|---|---|
LLMSingleSelector | The model answers in text (JSON), parsed by LlamaIndex; a single engine |
LLMMultiSelector | Same, several engines possible; answers are then combined |
PydanticSingleSelector / PydanticMultiSelector | The choice goes through a structured tool call (function calling) |
EmbeddingSingleSelector | No LLM: the description closest to the question by embedding |
The LLM-based selector is the most reliable on close descriptions; the embedding selector is almost free and instant, useful when engines cover very distinct domains. The multi-selector can answer "What is the chair's warranty, and who makes it?" by querying two engines — lesson 4.2 presents a more robust approach to such compound questions.
Writing good descriptions
- Describe the questions, not the technology. "Precise questions about a product" helps the model; "Qdrant vector index" doesn't.
- Give concrete examples of topics covered: "warranty of an item, return window, shipping fees".
- Make the boundaries explicit between close engines: "overview" versus "precise questions".
- Plan the default case. The generalist engine (
knowledge_base) must cover everything that fits nowhere else, and its description should suggest it. - Test routing on a list of real questions with the expected engine, as you would test a classifier. A correct-routing rate below 90% is almost always fixed in the descriptions.
Routing retrievers instead of engines
The RouterQueryEngine picks a complete engine, synthesis included. LlamaIndex also provides the RouterRetriever, which picks among retrievers: you retrieve the passages, then synthesize them with a single common engine. Useful when you want a homogeneous synthesis (same prompt, same citation format) whatever the source of the passages.
What about "composable indexes"?
Older versions of LlamaIndex offered composable index graphs (ComposableGraph), which stacked indexes on top of each other. This approach is now superseded by routers, query engine tools and agents, which are simpler and more flexible. If you come across it in existing code, it's a good candidate for migration.
Key takeaways
- A
QueryEngineToolassociates an engine with a name and a description; the router only sees the descriptions.RouterQueryEngine+ selector: the fast model picks an engine; the choice can be read in the response.- LLM selectors (text or structured), multi-selectors, or embedding selectors depending on how close the engines are.
- Good descriptions talk about questions, give examples, mark boundaries and plan a default.
- Test routing like a classifier;
RouterRetrieverfor a common synthesis.
Practice
- Run
examples/03_router.pyand check the engine chosen for each question. - Write ten varied questions with the expected engine, measure the correct-routing rate (with a real model), then improve the descriptions.
- Replace the selector with
EmbeddingSingleSelectorand compare correct-routing rate and latency.