Hand Off to a Specialist Agent
One agent with one enormous system prompt covering billing, engineering and legal does all three badly. Instead, a small router hands off to a specialist with a focused prompt.
Implement route_and_answer(llm, question, specialists) where specialists maps a name to a system prompt. Return {"specialist": name, "answer": text}.
Two calls, deliberately:
- Route. Ask the model which specialist should take the question, giving it the names and one line on each. Ask for the name alone.
- Answer. Ask the question again, with the chosen specialist's system prompt as the
systemargument.
The catch: the router's reply is free text: "billing" or "Billing" or "I'd route this to the billing team." Match it back to a real name case-insensitively and by containment, and when nothing matches, fall back to the first specialist rather than raising. The name must come from the dict you were given, never from the model's text directly.