Thanks to visit codestin.com
Credit goes to agentrouters.github.io

Quick Start

This quickstart takes you from a simple setup to a fully router AI agent in just a few minutes.

Requirements

For these examples, you will need to:

  • install: the Agent Routers Package
  • Choice Pattern: Choice the pattern router

Build a basic router agent with Regex

Start by creating a basic agent router that uses regular expressions (regex) to determine how requests are routed.

router.py
// Basic router
import asyncio
from agent_routers import AgentRouter

async def main():
    router = AgentRouter()

    async def support_handler(text: str):
        return "The support handler was called (async) with input:  %s" % text

    router.register(
        name="support",
        pattern=r"error|bug",
        handler=support_handler,
        priority=10,
    )

    result1 = await router.route("Go to error route!")
    print("Test 1:", result1)

if __name__ == "__main__":
    asyncio.run(main())

Agent Not Found

Next, implement a fallback router agent that is triggered when no route matches — equivalent to a 404 handler.

router.py
// Basic router 404
import asyncio
from agent_routers import AgentRouter
from agent_routers.exceptions import NoAgentMatchedError


async def main():

    router = AgentRouter()

    try:
        await router.route("something unknown happened")
    except NoAgentMatchedError as e:
        print("Test 4: Exception caught ->", e)


if __name__ == "__main__":
    asyncio.run(main())