| RouterAgent | R Documentation |
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.
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.
mini007::Agent -> RouterAgent
agentsThe registered agents, one per route.
fallbackName of the agent that handles requests no route fits,
or 'NULL'. Set via $set_fallback().
routing_historyOne 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'.
mini007::Agent$add_message()mini007::Agent$as_tool()mini007::Agent$clear_and_summarise_messages()mini007::Agent$clear_tools()mini007::Agent$clone_agent()mini007::Agent$export_messages_history()mini007::Agent$generate_and_register_tool()mini007::Agent$generate_execute_r_code()mini007::Agent$get_usage_stats()mini007::Agent$keep_last_n_messages()mini007::Agent$learn()mini007::Agent$list_tools()mini007::Agent$load_messages_history()mini007::Agent$plan_and_execute()mini007::Agent$register_tools()mini007::Agent$remove_tools()mini007::Agent$reset_conversation_history()mini007::Agent$set_budget()mini007::Agent$set_budget_policy()mini007::Agent$set_code_mode()mini007::Agent$set_memory()mini007::Agent$share_context_with()mini007::Agent$update_instruction()mini007::Agent$validate_response()new()Creates a new RouterAgent.
RouterAgent$new(name, llm_object, budget = NA)
nameA short name for the router (eg. '"router"').
llm_objectThe LLM object generated by ellmer (eg. the output of 'ellmer::chat_openai()'). It must support structured output.
budgetBudget in US$ for the routing calls, as in [Agent].
\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()
}
register_agents()Registers one or more agents, each as a route.
RouterAgent$register_agents(agents, descriptions = NULL)
agentsAn '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"'.
descriptionsOptional character vector, one per agent, telling the router when to pick each one. Defaults to each agent's instruction (its role, for a [ReActAgent]).
The router, invisibly.
remove_agents()Removes routes by agent name.
RouterAgent$remove_agents(agent_names)
agent_namesNames of the agents to remove.
The router, invisibly.
clear_agents()Removes every route, and the fallback.
RouterAgent$clear_agents()
The router, invisibly.
route_names()The route labels: the names of the registered agents.
RouterAgent$route_names()
A character vector.
set_fallback()Sets the agent that handles requests no route fits.
RouterAgent$set_fallback(agent_name = NULL)
agent_nameThe name of a registered agent, or 'NULL' to make an unroutable request an error again.
The router, invisibly.
route()Chooses the route for a request without running it.
RouterAgent$route(prompt)
promptThe request.
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).
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.
RouterAgent$invoke(prompt, stream = FALSE, on_event = NULL)
promptThe 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'.
The chosen agent's response as a character string.
last_route()The most recent routing decision.
RouterAgent$last_route()
The last entry of 'routing_history', or 'NULL'.
clone()The objects of this class are cloneable with this method.
RouterAgent$clone(deep = FALSE)
deepWhether to make a deep clone.
## ------------------------------------------------
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.