Sub topic 9
Choosing between several sources
This chapter assumes you can write a basic agent loop (the previous chapters covered it) and know what a retrieval tool looks like. Here the agent has more than one place to search, and the model must pick.
Why routing matters
Real systems rarely have one index. You might have product docs, a support-ticket archive, a SQL database of orders, and a web search API. Searching all of them for every question wastes money and fills the context with noise. In agentic RAG, source routing means the model decides which source to query, based on the question and on what each tool says it is for.
Option 1: one tool per source
Give each source its own tool. The tool descriptions are the routing policy, so write them to say what the source contains and what it does not contain.
TOOL_SPECS = [
{
"name": "search_docs",
"description": "Search the official product documentation. Use for configuration, limits and documented behavior. Not for individual customer cases.",
"parameters": {
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"],
},
},
{
"name": "search_tickets",
"description": "Search resolved support tickets. Use for known bugs, workarounds and error messages seen in practice. Not for official limits.",
"parameters": {
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"],
},
},
]
def search_docs(query):
return [{"id": "doc-12", "source": "docs", "text": "Max upload size is 50 MB."}]
def search_tickets(query):
return [{"id": "tkt-904", "source": "tickets", "text": "Uploads over 50 MB fail with error 413."}]
tools_by_name = {"search_docs": search_docs, "search_tickets": search_tickets}You pass tools_by_name and TOOL_SPECS to the same run_agent loop from earlier. The loop needs no changes, because it already looks tools up by name.
Option 2: one tool with a source parameter
If you have many sources, a single tool with an enum keeps the tool list short.
{
"name": "search",
"description": "Search one knowledge source. docs: official documentation. tickets: resolved support cases. orders: order status records.",
"parameters": {
"type": "object",
"properties": {
"source": {"type": "string", "enum": ["docs", "tickets", "orders"]},
"query": {"type": "string"}
},
"required": ["source", "query"]
}
}| Approach | Strength | Weakness |
|---|---|---|
| One tool per source | Each description can be detailed. Easy to add per-source limits. | Many tools crowd the prompt and confuse the model. |
| One tool with an enum | Compact. Easy to add a source. | Source descriptions must fit in one description. |
How a routed query flows
Common errors
- Overlapping descriptions. If two tools both say "search for help", the model guesses. State what each source is for and what it is not for.
- Unlabeled results. Return a
sourcefield with each passage. Without it, the model cannot tell an official limit from a ticket workaround, and neither can the reader of the citations. - No fallback. If the first source returns nothing, the model should try another. Say so in the system prompt, and keep the step budget in force so it cannot loop forever.
- Unreliable sources treated as equal. Tell the model which source wins when they disagree, for example "documentation overrides tickets".
Exercise
A user asks, "Why does my 80 MB upload fail with error 413?" Using the two tools above, which one should the agent call first, and what should it do with the results from both?
Show answer
Start with searchtickets, since error 413 is a symptom seen in practice. The agent should then call searchdocs to confirm the documented 50 MB limit. In the answer it should state the limit from the documentation, cite the ticket as supporting evidence, and use the source labels to keep the two apart.