| ReActAgent | R Documentation |
A 'ReActAgent' answers a prompt by looping through the ReAct (Reason + Act) cycle until it can give a final answer:
1. **Think**: the LLM states what it knows and what it needs next. 2. **Act**: it calls one or more of its tools. 3. **Observe**: the tool results are sent back to it. 4. Steps 1 to 3 repeat until the LLM answers without calling a tool.
It is an [Agent] with tools: it keeps the same budget, memory, streaming, history and cloning behaviour, and can be registered with a [LeadAgent] or used as a [Workflow] station like any other agent. On top of that it records each step of its last run in '$trace', and stops a run that goes past 'max_steps' tool-using steps.
The tool calls themselves go through the LLM provider's native tool calling (via ellmer), not through parsed text, so any ellmer chat model that supports tools can drive it.
mini007::Agent -> ReActAgent
roleThe agent's role, without the ReAct guidance.
max_stepsThe largest number of tool-using steps in one run.
traceA data frame with one row per Think, Act, Observe and answer entry of the last run, with columns 'step', 'type', 'tool', 'content' and 'is_error'.
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$validate_response()new()Creates a new ReActAgent.
ReActAgent$new( name, instruction, llm_object, tools = list(), max_steps = 10, budget = NA )
nameA short identifier for the agent.
instructionThe agent's role (eg. '"You are a career advisor."'). The ReAct guidance is added to it to form the system prompt; the role alone is kept in '$role'.
llm_objectThe LLM object generated by ellmer (eg. the output of 'ellmer::chat_openai()'). It must support tool calling.
toolsA list of 'ellmer::tool()' definitions, or a single one. More can be added later with '$register_tools()'.
max_steps'[integer(1)]' The largest number of tool-using steps in one '$invoke()' call. Once reached, further tool calls are refused and the LLM is told to answer with what it has; if it keeps calling tools, the run stops with an error of class 'mini007_react_step_limit'. Defaults to 10.
budgetBudget in US$ for this agent, as in [Agent].
\dontrun{
search_careers <- ellmer::tool(
function(interests) {
if (grepl("help", interests)) c("Teacher", "Counselor") else "Data Analyst"
},
description = "Search careers that match a person's interests.",
arguments = list(interests = ellmer::type_string("The person's interests."))
)
advisor <- ReActAgent$new(
name = "career_advisor",
instruction = "You are a career guidance advisor.",
llm_object = ellmer::chat_openai(model = "gpt-4.1-mini", echo = "none"),
tools = list(search_careers)
)
advisor$invoke("I love helping people. Which careers fit me?")
advisor$trace
}
invoke()Runs the ReAct loop on a prompt and returns the final answer.
Takes the same arguments as 'Agent$invoke()'. While streaming, tool calls and results arrive as '"tool_call"' and '"tool_result"' events. When the run ends (or fails), '$trace' holds its steps.
ReActAgent$invoke(prompt, stream = FALSE, on_event = NULL, type = NULL)
promptA character string prompt.
stream'[logical(1)]' Whether to stream the response.
on_event'[function | NULL]' Callback receiving each 'mini007_event'; supplying it implies 'stream = TRUE'.
type'[ellmer type | NULL]' To get the final answer as structured data, its shape, as in 'Agent$invoke()'. The loop runs with the tools as usual, then one more call, without tools, turns the answer into the structure.
The final answer as a character string, or, with 'type', the structured data.
update_instruction()Updates the agent's role. The ReAct guidance is kept.
ReActAgent$update_instruction(new_instruction)
new_instructionThe new role.
print_trace()Prints the steps of the last run: Think, Act, Observe and the final answer.
ReActAgent$print_trace(max_chars = 300)
max_chars'[integer(1)]' Longest text shown for one entry.
clone()The objects of this class are cloneable with this method.
ReActAgent$clone(deep = FALSE)
deepWhether to make a deep clone.
## ------------------------------------------------
## Method `ReActAgent$new`
## ------------------------------------------------
## Not run:
search_careers <- ellmer::tool(
function(interests) {
if (grepl("help", interests)) c("Teacher", "Counselor") else "Data Analyst"
},
description = "Search careers that match a person's interests.",
arguments = list(interests = ellmer::type_string("The person's interests."))
)
advisor <- ReActAgent$new(
name = "career_advisor",
instruction = "You are a career guidance advisor.",
llm_object = ellmer::chat_openai(model = "gpt-4.1-mini", echo = "none"),
tools = list(search_careers)
)
advisor$invoke("I love helping people. Which careers fit me?")
advisor$trace
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.