Routing to Specialist Agents

A router reads each message and hands it to the right specialist: billing or tech support.

AI · intermediate · 6 min · 20 steps · free

What you'll learn

multi_agent.py

def billing_agent(message):
    return 'Billing: your last invoice was 499 rupees.'

def tech_agent(message):
    return 'Tech: try restarting the router.'

# Stands in for a small, fast model that only decides who should answer.
def router(message):
    if 'bill' in message or 'invoice' in message:
        return billing_agent
    return tech_agent

for message in ['why is my bill so high', 'the internet is down']:
    specialist = router(message)
    print(specialist(message))

Step by step

  1. Python defines the function billing_agent(). Its body does not run yet: it runs each time the function is called.
  2. Python defines the function tech_agent(). Its body does not run yet: it runs each time the function is called.
  3. Python defines the function router(). Its body does not run yet: it runs each time the function is called.
  4. message is created with the value 'why is my bill so high'.
  5. Python calls router(), with message = 'why is my bill so high'. It gets its own names.
  6. "bill" is in the message: billing should handle it.
  7. The function returns billing_agent.
  8. specialist now holds the billing agent itself.
  9. Python calls billing_agent(), with message = 'why is my bill so high'. It gets its own names.
  10. The function returns 'Billing: your last invoice was 499 rupees.'.
  11. billing_agent() has finished, so its names are gone. It prints Billing: your last invoice was 499 rupees..
  12. message changes from 'why is my bill so high' to 'the internet is down'.
  13. Python calls router(), with message = 'the internet is down'. It gets its own names.
  14. No billing words here.
  15. The function returns tech_agent.
  16. router() has finished, so its names are gone. The value it returned, function tech_agent(), is stored in specialist.
  17. Python calls tech_agent(), with message = 'the internet is down'. It gets its own names.
  18. The function returns 'Tech: try restarting the router.'.
  19. tech_agent() has finished, so its names are gone. It prints Tech: try restarting the router..
  20. Python moves to the next item of the loop: for message in ['why is my bill so high', 'the internet is down'].

Common mistakes

Best practices

Key takeaways

Check yourself

Why not one agent for everything?

Answer: More tools and rules mean more mistakes

What does router return?

Answer: An agent (function)

·

More in Agents