TaskAgent: TaskAgent: An Agent for Long, Multi-Step Tasks

TaskAgentR Documentation

TaskAgent: An Agent for Long, Multi-Step Tasks

Description

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

Super classes

mini007::Agent -> mini007::ReActAgent -> TaskAgent

Public fields

todos

The todo list of the current or last task: a data frame with columns 'task' and 'status' ('"pending"', '"in_progress"' or '"done"').

notes

The notes the agent saved, as a named list of strings.

workspace

The folder where notes are saved, or 'NULL'.

max_rounds

The largest number of rounds in one task.

max_context_tokens

The estimated size above which the conversation is compacted.

keep_recent

How many of the latest messages compaction keeps.

require_approval

The tools whose calls need approval.

approved_tools

The tools approved for good ("always").

last_task

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

Methods

Public methods

Inherited methods

Method new()

Creates a new TaskAgent.

Usage
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
)
Arguments
name

A short identifier for the agent.

instruction

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

The LLM object generated by ellmer. It must support tool calling.

tools

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

budget

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

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

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

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

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

Returns

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.


Method print_todos()

Prints the todo list.

Usage
TaskAgent$print_todos()

Method compact()

Summarises the older messages of the conversation now, whatever its size, keeping the last 'keep_recent' messages as they are.

Usage
TaskAgent$compact()
Returns

The agent, invisibly.


Method clear_notes()

Deletes the notes, from '$notes' and from the workspace folder.

Usage
TaskAgent$clear_notes(names = NULL)
Arguments
names

'[character | NULL]' The notes to delete; 'NULL' (the default) deletes them all.

Returns

The agent, invisibly.


Method clone()

The objects of this class are cloneable with this method.

Usage
TaskAgent$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples


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

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