library(mini007)
retrieve_open_ai_credential <- function() {
Sys.getenv("OPENAI_API_KEY")
}
openai_4_1_mini <- ellmer::chat(
name = "openai/gpt-4.1-mini",
credentials = retrieve_open_ai_credential,
echo = "none"
)ReActAgent
A ReActAgent is an Agent that reasons and acts. Instead of answering in one go, it works in a loop:
- Think: it states what it knows so far and what it needs next.
- Act: it calls one or more of its tools.
- Observe: it reads what the tools returned.
- It repeats steps 1 to 3 until it has enough to answer, then gives a final answer without calling a tool.
Use it when answering a question takes several lookups, and each one depends on what the previous one found.
A career advisor
A person asks: “I’m creative and love helping people. What career paths might be a good fit, what programs can help me get there, and how can I pay for it?”
Answering takes three lookups, one after the other: find careers that match, find programs that train for those careers, then find funding for those programs. We give the agent one tool per lookup.
The data
Small, made-up tables stand in for real services, so the example runs anywhere:
careers <- data.frame(
career = c("UX Designer", "Teacher", "Art Therapist", "Data Analyst"),
interests = c(
"creative design technology helping",
"helping teaching children creative",
"creative art helping psychology",
"technology math analysis"
),
median_salary_usd = c(98000, 62000, 58000, 85000)
)
programs <- data.frame(
career = c("UX Designer", "UX Designer", "Teacher", "Art Therapist", "Data Analyst"),
program = c(
"UX Design Bootcamp", "UX Online Certificate", "Teaching Certification",
"M.A. Art Therapy", "Data Analytics Bootcamp"
),
type = c("bootcamp", "online", "online", "college", "bootcamp"),
months = c(4, 6, 9, 24, 3),
cost_usd = c(14000, 300, 5000, 45000, 11000)
)
funding <- data.frame(
name = c("Pell Grant", "Creative Futures Scholarship", "TEACH Grant", "Income Share Agreement"),
program_type = c("college", "bootcamp", "online", "bootcamp"),
amount = c("up to $7,395 per year", "$2,500", "up to $4,000 per year", "paid back as a % of salary")
)The tools
Each tool is an ordinary R function wrapped with ellmer::tool(). Its description tells the LLM what the tool does and when to call it:
search_careers <- ellmer::tool(
function(interest) {
careers[grepl(tolower(interest), careers$interests), c("career", "median_salary_usd")]
},
name = "search_careers",
description = "Find careers that match one interest or trait. Call it once per interest.",
arguments = list(
interest = ellmer::type_string("One word, e.g. 'creative' or 'helping'.")
)
)
find_programs <- ellmer::tool(
function(career) {
programs[programs$career == career, c("program", "type", "months", "cost_usd")]
},
name = "find_programs",
description = "Find training programs for one career, using its exact title.",
arguments = list(
career = ellmer::type_string("Exact career title, e.g. 'UX Designer'.")
)
)
find_funding <- ellmer::tool(
function(program_type) {
funding[funding$program_type == program_type, c("name", "amount")]
},
name = "find_funding",
description = "Find scholarships, grants and loans for one type of program.",
arguments = list(
program_type = ellmer::type_enum(c("bootcamp", "online", "college"), "Program type.")
)
)The agent
instruction is the agent’s role. The ReActAgent adds the Think / Act / Observe guidance to it. max_steps caps how many tool-using steps one run can take:
advisor <- ReActAgent$new(
name = "career_advisor",
instruction = "You are a career guidance advisor. Keep your final answer under 150 words.",
llm_object = openai_4_1_mini,
tools = list(search_careers, find_programs, find_funding),
max_steps = 6
)answer <- advisor$invoke(
"I'm creative and love helping people. What career paths might be a good fit,
what programs can help me get there, and how can I pay for it?"
)
cat(answer)Creative and helping careers fit well with roles like UX Designer, Teacher, and Art Therapist. To pursue these:
- UX Designer: Bootcamp ($14,000, 4 months) with Creative Futures Scholarship ($2,500) or Income Share Agreements; Online Certificate ($300).
- Teacher: Online Certification ($5,000, 9 months) with TEACH Grant (up to $4,000/year).
- Art Therapist: M.A. program ($45,000, 24 months) with Pell Grant (up to $7,395/year).
These paths combine your creativity and desire to help, with flexible training options and funding aid available.
Following the reasoning
$print_trace() shows each step of the last run: what the agent thought, which tools it called with which arguments, and what it observed:
advisor$print_trace()── Step 1 ──────────────────────────────────────────────────────────────────────
Think: You have two interests: creativity and helping people. I'll first look for career paths that match each interest separately. Then, I'll find training programs for the careers that seem a suitable overlap of both interests. Finally, I'll look for funding options for the training programs.
Act: search_careers({"interest":"creative"})
Act: search_careers({"interest":"helping"})
Observe search_careers: [{"career":"UX Designer","median_salary_usd":98000},{"career":"Teacher","median_salary_usd":62000},{"career":"Art Therapist","median_salary_usd":58000}]
Observe search_careers: [{"career":"UX Designer","median_salary_usd":98000},{"career":"Teacher","median_salary_usd":62000},{"career":"Art Therapist","median_salary_usd":58000}]
── Step 2 ──────────────────────────────────────────────────────────────────────
Think: Creative and helping people careers identified include UX Designer, Teacher, and Art Therapist. Next, I'll find training programs for these three careers to see what options are available. Then, I'll look into funding options for these programs.
Act: find_programs({"career":"UX Designer"})
Act: find_programs({"career":"Teacher"})
Act: find_programs({"career":"Art Therapist"})
Observe find_programs: [{"program":"UX Design Bootcamp","type":"bootcamp","months":4,"cost_usd":14000},{"program":"UX Online Certificate","type":"online","months":6,"cost_usd":300}]
Observe find_programs: [{"program":"Teaching Certification","type":"online","months":9,"cost_usd":5000}]
Observe find_programs: [{"program":"M.A. Art Therapy","type":"college","months":24,"cost_usd":45000}]
── Step 3 ──────────────────────────────────────────────────────────────────────
Think: Training programs are UX Design Bootcamp (bootcamp, 4 months, $14,000), UX Online Certificate (online, 6 months, $300), Teaching Certification (online, 9 months, $5,000), and M.A. Art Therapy (college, 24 months, $45,000). Next, I will look for funding options for bootcamp, online, and college progr...
Act: find_funding({"program_type":"bootcamp"})
Act: find_funding({"program_type":"online"})
Act: find_funding({"program_type":"college"})
Observe find_funding: [{"name":"Creative Futures Scholarship","amount":"$2,500"},{"name":"Income Share Agreement","amount":"paid back as a % of salary"}]
Observe find_funding: [{"name":"TEACH Grant","amount":"up to $4,000 per year"}]
Observe find_funding: [{"name":"Pell Grant","amount":"up to $7,395 per year"}]
── Step 4 ──────────────────────────────────────────────────────────────────────
Answer: Creative and helping careers fit well with roles like UX Designer, Teacher, and Art Therapist. To pursue these: - UX Designer: Bootcamp ($14,000, 4 months) with Creative Futures Scholarship ($2,500) or Income Share Agreements; Online Certificate ($300). - Teacher: Online Certification ($5,000, 9 mon...
The same steps are available as a data frame in $trace, with one row per entry:
advisor$trace[, c("step", "type", "tool")] step type tool
1 1 think <NA>
2 1 act search_careers
3 1 act search_careers
4 1 observe search_careers
5 1 observe search_careers
6 2 think <NA>
7 2 act find_programs
8 2 act find_programs
9 2 act find_programs
10 2 observe find_programs
11 2 observe find_programs
12 2 observe find_programs
13 3 think <NA>
14 3 act find_funding
15 3 act find_funding
16 3 act find_funding
17 3 observe find_funding
18 3 observe find_funding
19 3 observe find_funding
20 4 answer <NA>
| Column | Meaning |
|---|---|
step |
The step number. Several tool calls made together share a step. |
type |
"think", "act", "observe" or "answer". |
tool |
The tool called (for "act" and "observe" rows). |
content |
The thought, the tool’s arguments, its result, or the answer. |
is_error |
TRUE when a tool failed. The agent sees the error and can adapt. |
Limiting the number of steps
When an agent reaches max_steps, its next tool calls are refused and it is told to answer with the information it already has. With a single step allowed, the advisor can search for careers but not look up programs or funding:
quick_advisor <- ReActAgent$new(
name = "quick_advisor",
instruction = "You are a career guidance advisor. Keep your final answer under 100 words.",
llm_object = openai_4_1_mini,
tools = list(search_careers, find_programs, find_funding),
max_steps = 1
)
answer <- quick_advisor$invoke(
"I'm creative and love helping people. Which careers fit me, and which programs should I look at?"
)
quick_advisor$print_trace()── Step 1 ──────────────────────────────────────────────────────────────────────
Think: You are interested in creative and helping-related careers. I will search for careers that match each of these interests.
Act: search_careers({"interest":"creative"})
Act: search_careers({"interest":"helping"})
Observe search_careers: [{"career":"UX Designer","median_salary_usd":98000},{"career":"Teacher","median_salary_usd":62000},{"career":"Art Therapist","median_salary_usd":58000}]
Observe search_careers: [{"career":"UX Designer","median_salary_usd":98000},{"career":"Teacher","median_salary_usd":62000},{"career":"Art Therapist","median_salary_usd":58000}]
── Step 2 ──────────────────────────────────────────────────────────────────────
Think: Creative and helping interests both align with careers such as UX Designer, Teacher, and Art Therapist. Next, I will look for training programs for these careers.
Act: find_programs({"career":"UX Designer"})
Act: find_programs({"career":"Teacher"})
Act: find_programs({"career":"Art Therapist"})
Observe find_programs (error): Tool call rejected. The step limit is reached. Do not call more tools: give your final answer with the information you have, and say what is missing.
Observe find_programs (error): Tool call rejected. The step limit is reached. Do not call more tools: give your final answer with the information you have, and say what is missing.
Observe find_programs (error): Tool call rejected. The step limit is reached. Do not call more tools: give your final answer with the information you have, and say what is missing.
── Step 3 ──────────────────────────────────────────────────────────────────────
Answer: Careers matching your interests include UX Designer, Teacher, and Art Therapist. Each involves creativity and helping others. UX Designers focus on user experience and design, Teachers educate and support students, and Art Therapists use art to aid healing. You should look for programs in UX design,...
If the LLM still keeps calling tools after that, the run stops with an error of class mini007_react_step_limit.
Using it with other mini007 classes
A ReActAgent is an Agent, so everything on the Agents page applies to it: budgets, memory, streaming, cloning and conversation history. It can also be registered with a LeadAgent or used as a Workflow station, like any other agent.