| TaskAgent | R Documentation |
A 'TaskAgent' is a [ReActAgent] built to carry a long task through to the end. On top of the Think / Act / Observe loop and the agent's own tools, it gets:
- **A todo list.** For a task with several steps, the agent first writes its todo list (with the built-in 'write_todos' tool), then works through it, marking each item 'in_progress' and 'done'. The list is in '$todos'. - **Rounds until the work is done.** When the agent answers while items of its todo list are not done, it is sent back to work on them, up to 'max_rounds' rounds. - **Notes.** The agent saves what it will need later (findings, decisions, intermediate results) with the built-in 'save_note' tool and reads it back with 'read_note'. Notes are in '$notes'; with a 'workspace' folder, they are also saved as Markdown files there and loaded again by the next 'TaskAgent' that uses the folder. - **Automatic compaction.** Before each round, when the conversation is longer than 'max_context_tokens' (estimated at four characters a token), the older messages are replaced by a summary written by the LLM; the last 'keep_recent' messages are kept as they are. Notes and the todo list are not affected. - **Tool approval.** Calls to the tools named in 'require_approval' wait for a human's approval: yes, no, or always (do not ask again for this tool). A refused call is reported to the LLM, which carries on without it.
Everything else works as with any [Agent]: budget, cost, memory, streaming,
structured output, code mode ($set_code_mode(), where the agent's
own tools are called from R code while the todo and note tools stay
separate), and use inside a [LeadAgent] or a [Workflow].
mini007::Agent -> mini007::ReActAgent -> TaskAgent
todosThe todo list of the current or last task: a data frame with columns 'task' and 'status' ('"pending"', '"in_progress"' or '"done"').
notesThe notes the agent saved, as a named list of strings.
workspaceThe folder where notes are saved, or 'NULL'.
max_roundsThe largest number of rounds in one task.
max_context_tokensThe estimated size above which the conversation is compacted.
keep_recentHow many of the latest messages compaction keeps.
require_approvalThe tools whose calls need approval.
approved_toolsThe tools approved for good ("always").
last_taskThe last task: a list with the 'prompt', the number of 'rounds' and 'compactions', the 'todos', the 'answer', whether it 'completed' (every todo item done) 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$validate_response()mini007::ReActAgent$print_trace()mini007::ReActAgent$update_instruction()new()Creates a new TaskAgent.
TaskAgent$new( name, instruction, llm_object, tools = list(), max_steps = 25, max_rounds = 5, max_context_tokens = 60000, keep_recent = 6, workspace = NULL, require_approval = character(), approve = NULL, budget = NA )
nameA short identifier for the agent.
instructionThe agent's role. The guidance about todos, notes and the ReAct loop is added to it to form the system prompt; the role alone is kept in '$role'.
llm_objectThe LLM object generated by ellmer. It must support tool calling.
toolsA list of 'ellmer::tool()' definitions, or a single one. The agent also has its built-in tools ('write_todos', 'save_note', 'read_note'), so it can be created without tools of its own.
max_steps'[integer(1)]' The largest number of tool-using steps in one round. Defaults to 25.
max_rounds'[integer(1)]' The largest number of rounds in one
$invoke(). Defaults to 5.
max_context_tokens'[numeric(1)]' The estimated size of the conversation, in tokens, above which it is compacted before a round. Defaults to 60000. 'Inf' turns compaction off.
keep_recent'[integer(1)]' How many of the latest messages compaction keeps as they are. Defaults to 6.
workspace'[character(1) | NULL]' A folder where notes are saved as Markdown files, and loaded from when the agent is created. It is created if needed. 'NULL' (the default) keeps the notes in memory.
require_approval'[character]' Names of the tools whose calls need a human's approval, eg. 'c("send_email", "run_r_code")'.
approve'[function | NULL]' How approval is asked: a 'function(tool, arguments)' returning 'TRUE' (allow this call), 'FALSE' (refuse it) or '"always"' (allow it and every later call of this tool). 'NULL' (the default) asks in the console.
budgetBudget in US$ for this agent, as in [Agent].
\dontrun{
monument_info <- ellmer::tool(
function(monument) paste("Facts about", monument, "..."),
name = "monument_info",
description = "Facts about a monument of Algeria.",
arguments = list(monument = ellmer::type_string("The monument."))
)
guide <- TaskAgent$new(
name = "guide",
instruction = "You write travel guides about Algeria.",
llm_object = ellmer::chat("openai/gpt-4.1-mini", echo = "none"),
tools = list(monument_info),
workspace = "guide-notes"
)
guide$invoke(paste(
"Write a one-day itinerary for Algiers covering the Casbah,",
"the Martyrs' Memorial and the Jardin d'Essai, with a fact for each."
))
guide$todos
guide$notes
}
invoke()Works on a task until it is done, and returns the final answer.
A round is one ReAct loop ([ReActAgent]'s $invoke()). After a
round, if items of the todo list are not done, another round starts,
reminding the agent of the task and of what is left, up to
'max_rounds' rounds. Before each round, the conversation is compacted
if it is too long. The todo list starts empty for each task.
While streaming, besides the usual Agent events, 'on_event' receives '"round_started"' (with 'round'), '"todos_updated"' (with 'todos') and '"context_compacted"' (with 'removed', the number of messages summarised).
TaskAgent$invoke(prompt, stream = FALSE, on_event = NULL, type = NULL)
promptThe task.
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(): once the
rounds are over, one more call turns the answer into the structure.
The final answer, or, with 'type', the structured data. The run is described in '$last_task', and '$trace' holds the steps of every round, with a 'round' column.
print_todos()Prints the todo list.
TaskAgent$print_todos()
compact()Summarises the older messages of the conversation now, whatever its size, keeping the last 'keep_recent' messages as they are.
TaskAgent$compact()
The agent, invisibly.
clear_notes()Deletes the notes, from '$notes' and from the workspace folder.
TaskAgent$clear_notes(names = NULL)
names'[character | NULL]' The notes to delete; 'NULL' (the default) deletes them all.
The agent, invisibly.
clone()The objects of this class are cloneable with this method.
TaskAgent$clone(deep = FALSE)
deepWhether to make a deep clone.
## ------------------------------------------------
## Method `TaskAgent$new`
## ------------------------------------------------
## Not run:
monument_info <- ellmer::tool(
function(monument) paste("Facts about", monument, "..."),
name = "monument_info",
description = "Facts about a monument of Algeria.",
arguments = list(monument = ellmer::type_string("The monument."))
)
guide <- TaskAgent$new(
name = "guide",
instruction = "You write travel guides about Algeria.",
llm_object = ellmer::chat("openai/gpt-4.1-mini", echo = "none"),
tools = list(monument_info),
workspace = "guide-notes"
)
guide$invoke(paste(
"Write a one-day itinerary for Algiers covering the Casbah,",
"the Martyrs' Memorial and the Jardin d'Essai, with a fact for each."
))
guide$todos
guide$notes
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.