RouterAgent: RouterAgent: Send Each Request to the Right Agent

RouterAgentR Documentation

RouterAgent: Send Each Request to the Right Agent

Description

A 'RouterAgent' classifies each request and hands it, unchanged, to the one registered agent best suited to it. Unlike a [LeadAgent], it never splits the request into subtasks: its LLM only picks a route, and the chosen agent writes the whole answer.

Each registered agent is one route. The route's label is the agent's name, and its description, which the LLM reads when choosing, is the agent's instruction (the role, for a [ReActAgent]) unless another description is given to '$register_agents()'.

Choosing a route takes one structured LLM call: the LLM must return one of the route labels, or '"none"' when no route fits, along with a short reason. A '"none"' goes to the fallback agent set with '$set_fallback()', and is an error of class 'mini007_no_route' when there is none.

Details

The routing calls go through the router's own 'llm_object', so they add to its turn history and its cost. That history lets the router route a follow-up such as "and how do I pay for it?" in the light of the previous requests.

A 'RouterAgent' is an [Agent]: it can itself be registered with a [LeadAgent] or used as a [Workflow] station.

Super class

mini007::Agent -> RouterAgent

Public fields

agents

The registered agents, one per route.

fallback

Name of the agent that handles requests no route fits, or 'NULL'. Set via $set_fallback().

routing_history

One entry per routed request: the 'prompt', the ‘agent_name' and 'agent_id' it went to, the LLM’s 'reason', whether the 'fallback' was used, the 'response' and a 'timestamp'.

Methods

Public methods

Inherited methods

Method new()

Creates a new RouterAgent.

Usage
RouterAgent$new(name, llm_object, budget = NA)
Arguments
name

A short name for the router (eg. '"router"').

llm_object

The LLM object generated by ellmer (eg. the output of 'ellmer::chat_openai()'). It must support structured output.

budget

Budget in US$ for the routing calls, as in [Agent].

Examples
\dontrun{
llm <- ellmer::chat("openai/gpt-4.1-mini", echo = "none")

career <- Agent$new("career", "You recommend careers that fit a person.", llm)
training <- Agent$new("training", "You recommend schools, bootcamps and courses.", llm)
funding <- Agent$new("funding", "You explain scholarships, grants and loans.", llm)

router <- RouterAgent$new(name = "router", llm_object = llm)
router$register_agents(list(career, training, funding))

router$invoke("Which scholarships can help me pay for a nursing degree?")
router$last_route()
}

Method register_agents()

Registers one or more agents, each as a route.

Usage
RouterAgent$register_agents(agents, descriptions = NULL)
Arguments
agents

An 'Agent' (or [ReActAgent], [WorkflowAgent], ...) or a list of them. Any object with a 'name' and an '$invoke(prompt)' method works. Agent names are the route labels, so they must be unique and cannot be '"none"'.

descriptions

Optional character vector, one per agent, telling the router when to pick each one. Defaults to each agent's instruction (its role, for a [ReActAgent]).

Returns

The router, invisibly.


Method remove_agents()

Removes routes by agent name.

Usage
RouterAgent$remove_agents(agent_names)
Arguments
agent_names

Names of the agents to remove.

Returns

The router, invisibly.


Method clear_agents()

Removes every route, and the fallback.

Usage
RouterAgent$clear_agents()
Returns

The router, invisibly.


Method route_names()

The route labels: the names of the registered agents.

Usage
RouterAgent$route_names()
Returns

A character vector.


Method set_fallback()

Sets the agent that handles requests no route fits.

Usage
RouterAgent$set_fallback(agent_name = NULL)
Arguments
agent_name

The name of a registered agent, or 'NULL' to make an unroutable request an error again.

Returns

The router, invisibly.


Method route()

Chooses the route for a request without running it.

Usage
RouterAgent$route(prompt)
Arguments
prompt

The request.

Returns

A list with 'agent_name', 'agent_id' ('NA' for agents without one), 'reason', and 'fallback' ('TRUE' when no route fitted and the fallback agent was chosen).


Method invoke()

Routes a request to one agent and returns that agent's answer.

While streaming, a '"route_selected"' event (with 'agent' and ‘reason' in its 'data') comes first, then the chosen agent’s own events, each tagged with 'agent', then a '"router_output"' event.

Usage
RouterAgent$invoke(prompt, stream = FALSE, on_event = NULL)
Arguments
prompt

The request.

stream

‘[logical(1)]' Whether to stream the chosen agent’s response.

on_event

'[function | NULL]' Callback receiving each 'mini007_event'; supplying it implies 'stream = TRUE'.

Returns

The chosen agent's response as a character string.


Method last_route()

The most recent routing decision.

Usage
RouterAgent$last_route()
Returns

The last entry of 'routing_history', or 'NULL'.


Method clone()

The objects of this class are cloneable with this method.

Usage
RouterAgent$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples


## ------------------------------------------------
## Method `RouterAgent$new`
## ------------------------------------------------

## Not run: 
llm <- ellmer::chat("openai/gpt-4.1-mini", echo = "none")

career <- Agent$new("career", "You recommend careers that fit a person.", llm)
training <- Agent$new("training", "You recommend schools, bootcamps and courses.", llm)
funding <- Agent$new("funding", "You explain scholarships, grants and loans.", llm)

router <- RouterAgent$new(name = "router", llm_object = llm)
router$register_agents(list(career, training, funding))

router$invoke("Which scholarships can help me pay for a nursing degree?")
router$last_route()

## End(Not run)

mini007 documentation built on Dec. 3, 2026, 5:10 p.m.