ReActAgent: ReActAgent: An Agent that Reasons and Acts with Tools

ReActAgentR Documentation

ReActAgent: An Agent that Reasons and Acts with Tools

Description

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.

Super class

mini007::Agent -> ReActAgent

Public fields

role

The agent's role, without the ReAct guidance.

max_steps

The largest number of tool-using steps in one run.

trace

A 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'.

Methods

Public methods

Inherited methods

Method new()

Creates a new ReActAgent.

Usage
ReActAgent$new(
  name,
  instruction,
  llm_object,
  tools = list(),
  max_steps = 10,
  budget = NA
)
Arguments
name

A short identifier for the agent.

instruction

The 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_object

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

tools

A 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.

budget

Budget in US$ for this agent, as in [Agent].

Examples
\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
}

Method 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.

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

A 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.

Returns

The final answer as a character string, or, with 'type', the structured data.


Method update_instruction()

Updates the agent's role. The ReAct guidance is kept.

Usage
ReActAgent$update_instruction(new_instruction)
Arguments
new_instruction

The new role.


Method print_trace()

Prints the steps of the last run: Think, Act, Observe and the final answer.

Usage
ReActAgent$print_trace(max_chars = 300)
Arguments
max_chars

'[integer(1)]' Longest text shown for one entry.


Method clone()

The objects of this class are cloneable with this method.

Usage
ReActAgent$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples


## ------------------------------------------------
## 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)

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