LeadAgent: LeadAgent: A Multi-Agent Orchestration Coordinator

LeadAgentR Documentation

LeadAgent: A Multi-Agent Orchestration Coordinator

Description

'LeadAgent' extends 'Agent' to coordinate a group of specialized agents. It decomposes complex prompts into subtasks using LLMs and assigns each subtask to the most suitable registered agent. The lead agent handles response chaining, where each agent can consider prior results.

Details

This class builds intelligent multi-agent workflows by delegating sub-tasks using 'delegate_prompt()', executing them with 'invoke()', and storing the results in the 'agents_interaction' list.

Super class

mini007::Agent -> LeadAgent

Public fields

agents

A named list of registered sub-agents (by UUID).

agents_interaction

A list of delegated task history with agent IDs, prompts, and responses.

plan

A list containing the most recently generated task plan.

hitl_steps

The steps where the workflow should be stopped in order to allow for a human interaction

hitl_mode

Either '"console"' (blocking readline() prompt, the default) or '"pause"' (non-blocking: $invoke() returns a 'mini007_pending' object instead of blocking). Set via $set_hitl().

prompt_for_plan

The prompt used to generate the plan.

agents_for_plan

The agents used for the plan

dialog_history

A list storing the history of agent dialogs

broadcast_history

A list storing the history of broadcast interactions

n_daemons

Number of parallel daemons used for parallel broadcast. Set via $set_daemons().

validation

The validation settings ('criteria', 'threshold', 'max_reassign') set via $set_validation(), or 'NULL'.

Methods

Public methods

Inherited methods

Method new()

Initializes the LeadAgent with a built-in task-decomposition prompt.

Usage
LeadAgent$new(name, llm_object, budget = NA)
Arguments
name

A short name for the coordinator (e.g. '"lead"').

llm_object

The LLM object generate by ellmer (eg. output of ellmer::chat_openai)

budget

Budget in US$ for the lead's own LLM calls (planning, matching agents to tasks, judging and synthesising), as in [Agent]. Each registered agent keeps its own budget.

Examples
  # An API KEY is required in order to invoke the agents
  openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )

 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )



Method clear_agents()

Clear out the registered Agents

Usage
LeadAgent$clear_agents()
Examples
  # An API KEY is required in order to invoke the agents
  openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )
 researcher <- Agent$new(
   name = "researcher",
   instruction = paste0(
   "You are a research assistant. ",
   "Your job is to answer factual questions with detailed and accurate information. ",
   "Do not answer with more than 2 lines"
   ),
   llm_object = openai_4_1_mini
 )

 summarizer <- Agent$new(
   name = "summarizer",
   instruction = paste0(
   "You are an agent designed to summarise ",
   "a given text into 3 distinct bullet points."
   ),
   llm_object = openai_4_1_mini
 )

 translator <- Agent$new(
   name = "translator",
   instruction = "Your role is to translate a text from English to German",
   llm_object = openai_4_1_mini
 )
 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )

 lead_agent$register_agents(c(researcher, summarizer, translator))

 lead_agent$agents

 lead_agent$clear_agents()

 lead_agent$agents


Method remove_agents()

Remove registered agents by IDs

Usage
LeadAgent$remove_agents(agent_ids)
Arguments
agent_ids

The Agent ID to remove from the registered Agents

Examples
  # An API KEY is required in order to invoke the agents
  openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )
 researcher <- Agent$new(
   name = "researcher",
   instruction = paste0(
   "You are a research assistant. ",
   "Your job is to answer factual questions with detailed and accurate information. ",
   "Do not answer with more than 2 lines"
   ),
   llm_object = openai_4_1_mini
 )

 summarizer <- Agent$new(
   name = "summarizer",
   instruction = "You are agent designed to summarise a given text into 3 distinct bullet points.",
   llm_object = openai_4_1_mini
 )

 translator <- Agent$new(
   name = "translator",
   instruction = "Your role is to translate a text from English to German",
   llm_object = openai_4_1_mini
 )


 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )

 lead_agent$register_agents(c(researcher, summarizer, translator))

 lead_agent$agents

 # deleting the translator agent

 id_translator_agent <- translator$agent_id

 lead_agent$remove_agents(id_translator_agent)

 lead_agent$agents


Method register_agents()

Register one or more agents for delegation.

Usage
LeadAgent$register_agents(agents)
Arguments
agents

A vector of 'Agent' objects to register.

Examples
  # An API KEY is required in order to invoke the agents
  openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )
 researcher <- Agent$new(
   name = "researcher",
   instruction = paste0(
   "You are a research assistant. ",
   "Your job is to answer factual questions with detailed and accurate information. ",
   "Do not answer with more than 2 lines"
   ),
   llm_object = openai_4_1_mini
 )

 summarizer <- Agent$new(
   name = "summarizer",
   instruction = "You are agent designed to summarise a given text into 3 distinct bullet points.",
   llm_object = openai_4_1_mini
 )

 translator <- Agent$new(
   name = "translator",
   instruction = "Your role is to translate a text from English to German",
   llm_object = openai_4_1_mini
 )

 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )

 lead_agent$register_agents(c(researcher, summarizer, translator))

 lead_agent$agents

Method visualize_plan()

Draws the plan of the LeadAgent: the request, then one card per step, in the order the steps run. Each card shows the agent, its model and the subtask it receives (hover a card to read the whole subtask). The response of each step is handed to the next one, and the response of the last step is the answer. Steps a human reviews (see $set_hitl()) are outlined in amber.

Usage
LeadAgent$visualize_plan(direction = c("LR", "TB"))
Arguments
direction

'[character(1)]' '"LR"' (left to right, the default) or '"TB"' (top to bottom).

Returns

A 'DiagrammeR' 'grViz' htmlwidget.

Examples
\dontrun{
 lead_agent$generate_plan("Write a short history of Timgad, then translate it into French")
 lead_agent$visualize_plan()
}

Method invoke()

Executes the full prompt pipeline: decomposition → delegation → invocation.

With stream = TRUE (or an 'on_event' callback), progress is delivered as 'mini007_event' objects: the plan, the start/completion of every delegated task, HITL requests, the final output, and the token deltas of every delegated Agent (see mini007_event). The lead agent's own planning calls are not streamed.

Usage
LeadAgent$invoke(
  prompt,
  force_regenerate_plan = FALSE,
  stream = FALSE,
  on_event = NULL
)
Arguments
prompt

The complex user instruction to process.

force_regenerate_plan

If TRUE, regenerate a plan even if one exists, defaults to FALSE.

stream

'[logical(1)]' Whether to stream progress. Without 'on_event', Agent token deltas are printed to the console.

on_event

'[function | NULL]' Callback receiving each 'mini007_event'. Supplying it implies stream = TRUE. Raising an error inside it cancels the run.

Returns

The final response (from the last agent in the sequence).

Examples
\dontrun{
 # An API KEY is required in order to invoke the agents
  openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )
 researcher <- Agent$new(
   name = "researcher",
   instruction = paste0(
   "You are a research assistant. ",
   "Your job is to answer factual questions with detailed ",
   "and accurate information. Do not answer with more than 2 lines"
   ),
   llm_object = openai_4_1_mini
 )

 summarizer <- Agent$new(
   name = "summarizer",
   instruction = "You are agent designed to summarise a given text into 3 distinct bullet points.",
   llm_object = openai_4_1_mini
 )

 translator <- Agent$new(
   name = "translator",
   instruction = "Your role is to translate a text from English to German",
   llm_object = openai_4_1_mini
 )

 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )

 lead_agent$register_agents(c(researcher, summarizer, translator))

 lead_agent$invoke(
 paste0(
  "Describe the economic situation in Algeria in 3 sentences. ",
  "Answer in German"
  )
 )
}

Method resume()

Resume a 'LeadAgent' paused at an HITL step.

Only relevant when hitl_mode = "pause" (see $set_hitl()). $invoke() returns a 'mini007_pending' object when it reaches a paused HITL step; call $resume() with that object's 'request_id' and a chosen 'action' to continue delegation from that point.

Usage
LeadAgent$resume(
  request_id,
  action = c("continue", "edit", "abort"),
  value = NULL,
  stream = FALSE,
  on_event = NULL
)
Arguments
request_id

'[character(1)]' The 'request_id' from the 'mini007_pending' object returned by $invoke().

action

'[character(1)]' One of '"continue"' (use the proposed response as-is), '"edit"' (replace it with 'value'), or '"abort"' (raise an error, stopping delegation).

value

'[character(1) | NULL]' Required when action = "edit": the replacement response.

stream

'[logical(1)]' Whether to stream the remaining steps (see $invoke()).

on_event

'[function | NULL]' Callback receiving each 'mini007_event'. Supplying it implies stream = TRUE.

Returns

'[character(1)]' The final response, or another 'mini007_pending' object if a further HITL step is reached.


Method generate_plan()

Generates a task execution plan without executing the subtasks. It returns a structured list containing the subtask, the selected agent, and metadata.

Usage
LeadAgent$generate_plan(prompt)
Arguments
prompt

A complex instruction to be broken into subtasks.

Returns

A list of lists containing agent_id, agent_name, model_name, model_provider, and the assigned prompt.

Examples
\dontrun{
 # An API KEY is required in order to invoke the agents
  openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )
 researcher <- Agent$new(
   name = "researcher",
   instruction = paste0(
   "You are a research assistant. Your job is to answer factual questions ",
   "with detailed and accurate information. Do not answer with more than 2 lines"
   ),
   llm_object = openai_4_1_mini
 )

 summarizer <- Agent$new(
   name = "summarizer",
   instruction = "You are agent designed to summarise a given text into 3 distinct bullet points.",
   llm_object = openai_4_1_mini
 )

 translator <- Agent$new(
   name = "translator",
   instruction = "Your role is to translate a text from English to German",
   llm_object = openai_4_1_mini
 )

 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )

 lead_agent$register_agents(c(researcher, summarizer, translator))

 lead_agent$generate_plan(
 paste0(
  "Describe the economic situation in Algeria in 3 sentences. ",
  "Answer in German"
  )
 )
}

Method set_daemons()

Configure parallel execution daemons for parallel broadcast using mirai. The daemons belong to the R session and are shared with every ‘Workflow' and 'LeadAgent'; the last call sets the pool’s size.

Usage
LeadAgent$set_daemons(n = 0L)
Arguments
n

'[integerish(1)]' Number of daemon processes (default '0'). Use '0' to shut down daemons.

Returns

Invisibly returns 'self'.

Examples
\dontrun{
openai_4_1_mini <- ellmer::chat(
  name = "openai/gpt-4.1-mini",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)
lead_agent <- LeadAgent$new(name = "Leader", llm_object = openai_4_1_mini)
lead_agent$set_daemons(4)
}

Method broadcast()

Broadcasts a prompt to all registered agents and collects their responses. This does not affect the main agent orchestration logic or history.

Usage
LeadAgent$broadcast(prompt, parallel = FALSE, synthesize = FALSE)
Arguments
prompt

A user prompt to send to all agents.

parallel

'[logical(1)]' When 'TRUE', agents are invoked concurrently via mirai daemons. Requires '$set_daemons(n > 0)' to be called first. Default 'FALSE'.

synthesize

'[logical(1)]' When 'TRUE', returns a single character string summarising each agent's response in the form "agent_name answered with ...". Default 'FALSE'.

Returns

A list of responses from all agents, or a single character string when 'synthesize = TRUE'.

Examples
\dontrun{
 # An API KEY is required in order to invoke the agents
openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )
openai_4_1 <- ellmer::chat(
  name = "openai/gpt-4.1",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)

openai_4_1_agent <- Agent$new(
  name = "openai_4_1_agent",
  instruction = "You are an AI assistant. Answer in 1 sentence max.",
  llm_object = openai_4_1
)

openai_4_1_nano <- ellmer::chat(
  name = "openai/gpt-4.1-nano",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)

openai_4_1_nano_agent <- Agent$new(
  name = "openai_4_1_nano_agent",
  instruction = "You are an AI assistant. Answer in 1 sentence max.",
  llm_object = openai_4_1_nano
  )

 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )

lead_agent$register_agents(c(openai_4_1_agent, openai_4_1_nano_agent))
lead_agent$broadcast(
  prompt = paste0(
    "If I were Algerian, which song would I like to sing ",
    "when running under the rain? how about a flower?"
  )
  )
}

Method set_hitl()

Set Human In The Loop (HITL) interaction at determined steps within the workflow. With mode = "console" (default) a paused step blocks on readline(), unchanged from previous behavior. With mode = "pause", $invoke() instead returns a 'mini007_pending' object describing the paused step; call $resume() with the chosen action to continue delegation. This allows HITL to work outside an interactive console.

Usage
LeadAgent$set_hitl(steps, mode = c("console", "pause"))
Arguments
steps

At which steps the Human In The Loop is required?

mode

'[character(1)]' '"console"' (default) or '"pause"'.

Returns

A list of responses from all agents.

Examples
\dontrun{
 # An API KEY is required in order to invoke the agents
  openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )
 researcher <- Agent$new(
   name = "researcher",
   instruction = paste0(
    "You are a research assistant. ",
    "Your job is to answer factual questions with detailed and accurate information. ",
    "Do not answer with more than 2 lines"
   ),
   llm_object = openai_4_1_mini
 )

 summarizer <- Agent$new(
   name = "summarizer",
   instruction = paste0(
   "You are agent designed to summarise a give text ",
   "into 3 distinct bullet points."
   ),
   llm_object = openai_4_1_mini
 )

 translator <- Agent$new(
   name = "translator",
   instruction = "Your role is to translate a text from English to German",
   llm_object = openai_4_1_mini
 )

 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )

 lead_agent$register_agents(c(researcher, summarizer, translator))

 # setting a human in the loop in step 2
 lead_agent$set_hitl(1)

 # The execution will stop at step 2 and a human will be able
 # to either accept the answer, modify it or stop the execution of
 # the workflow

 lead_agent$invoke(
 paste0(
  "Describe the economic situation in Algeria in 3 sentences. ",
  "Answer in German"
  )
 )
}

Method set_validation()

Validate every delegated step of $invoke(). Each response is scored from 0 to 1 against ‘criteria' by the lead agent’s LLM. A response scoring below 'threshold' is handed to the next best agent, up to 'max_reassign' times, and the best-scoring attempt is kept. The score, feedback and every attempt are stored in 'agents_interaction', and when another agent did best, 'plan' is updated so that re-running the same prompt starts with that agent.

Combined with $set_memory(), this is how a 'LeadAgent' learns:

  • every judged step is recorded as an outcome, building each agent's track record (see $agent_track_record()), which is shown to the LLM when it chooses an agent for a task;

  • the same judging call distills a short lesson from the step, and the lessons most relevant to a task are added to the prompt of the agent it is delegated to;

  • HITL decisions are recorded as outcomes too, and an HITL edit is distilled into a lesson.

With a 'Memory' saved to a file, all of this carries over to the next session.

Usage
LeadAgent$set_validation(criteria, threshold = 0.7, max_reassign = 1)
Arguments
criteria

'[character(1) | NULL]' What a good response looks like. 'NULL' turns validation off.

threshold

'[numeric(1)]' Minimum score (0 to 1) for a response to pass. Default 0.7.

max_reassign

'[integerish(1)]' How many times a rejected step can be handed to another agent. Default 1. Use 0 to only score responses.

Returns

Invisibly, 'self'.

Examples
\dontrun{
openai_4_1_mini <- ellmer::chat(
  name = "openai/gpt-4.1-mini",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)
researcher <- Agent$new(
  name = "researcher",
  instruction = "You answer factual questions in at most 2 lines.",
  llm_object = openai_4_1_mini
)
translator <- Agent$new(
  name = "translator",
  instruction = "You translate English text to German.",
  llm_object = openai_4_1_mini
)
lead_agent <- LeadAgent$new(name = "Leader", llm_object = openai_4_1_mini)
lead_agent$register_agents(c(researcher, translator))

lead_agent$set_memory(Memory$new(path = "team_memory.json"))
lead_agent$set_validation(
  criteria = "Accurate, complete, and in the language the task asks for.",
  threshold = 0.7
)

lead_agent$invoke("Describe Algeria's economy in 3 sentences, in German.")
lead_agent$agent_track_record()
lead_agent$memory$lessons
}

Method agent_track_record()

Summarise the recorded outcomes of every registered agent. Requires a memory (see $set_memory()).

Usage
LeadAgent$agent_track_record()
Returns

A data frame with columns 'agent_name', 'n_tasks', 'n_success', 'success_rate' and 'mean_score', one row per registered agent.


Method judge_and_choose_best_response()

The Lead Agent send a prompt to its registered agents and choose the best response from the agents' responses

Usage
LeadAgent$judge_and_choose_best_response(prompt)
Arguments
prompt

The prompt to send to the registered agents

Returns

A list of responses from all agents, including the chosen response

Examples
\dontrun{
openai_4_1_mini <- ellmer::chat(
  name = "openai/gpt-4.1-mini",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)
openai_4_1 <- ellmer::chat(
  name = "openai/gpt-4.1",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)

stylist <- Agent$new(
  name = "stylist",
  instruction = "You are an AI assistant. Answer in 1 sentence max.",
  llm_object = openai_4_1
)

openai_4_1_nano <- ellmer::chat(
  name = "openai/gpt-4.1-nano",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)

stylist2 <- Agent$new(
  name = "stylist2",
  instruction = "You are an AI assistant. Answer in 1 sentence max.",
  llm_object = openai_4_1_nano
)

lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
)

lead_agent$register_agents(c(stylist, stylist2))

lead_agent$judge_and_choose_best_response("what's the best way to war a kalvin klein shirt?")

}

Method agents_dialog()

Facilitates a collaborative dialog between two agents to refine a response. The agents take turns building on each other's responses until they reach consensus or the maximum iterations are reached. Agents can signal consensus by starting their response with "CONSENSUS:". If max iterations is reached without consensus, the lead agent synthesizes a final response.

Usage
LeadAgent$agents_dialog(prompt, agent_1_id, agent_2_id, max_iterations = 5)
Arguments
prompt

The initial task or question for the agents to discuss.

agent_1_id

The ID of the first agent to participate in the dialog.

agent_2_id

The ID of the second agent to participate in the dialog.

max_iterations

Maximum number of back-and-forth exchanges (default: 5).

Returns

A list containing the final response, consensus status, and complete dialog history.

Examples
\dontrun{
# An API KEY is required in order to invoke the agents
openai_4_1_mini <- ellmer::chat(
  name = "openai/gpt-4.1-mini",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)
openai_4_1_nano <- ellmer::chat(
  name = "openai/gpt-4.1-nano",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)

creative_writer <- Agent$new(
  name = "creative_writer",
  instruction = "You are a creative writer. Focus on engaging storytelling.",
  llm_object = openai_4_1_nano
)

editor <- Agent$new(
  name = "editor",
  instruction = "You are an editor. Focus on clarity and conciseness.",
  llm_object = openai_4_1_mini
)

lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
)

lead_agent$register_agents(c(creative_writer, editor))

result <- lead_agent$agents_dialog(
  prompt = "Write a compelling opening sentence for a sci-fi novel.",
  agent_1_id = creative_writer$agent_id,
  agent_2_id = editor$agent_id,
  max_iterations = 3
)

# Access the final response
result$final_response

# View the dialog history
result$dialog_history
}

Method clone()

The objects of this class are cloneable with this method.

Usage
LeadAgent$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples


## ------------------------------------------------
## Method `LeadAgent$new`
## ------------------------------------------------


  # An API KEY is required in order to invoke the agents
  openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )

 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )



## ------------------------------------------------
## Method `LeadAgent$clear_agents`
## ------------------------------------------------

  # An API KEY is required in order to invoke the agents
  openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )
 researcher <- Agent$new(
   name = "researcher",
   instruction = paste0(
   "You are a research assistant. ",
   "Your job is to answer factual questions with detailed and accurate information. ",
   "Do not answer with more than 2 lines"
   ),
   llm_object = openai_4_1_mini
 )

 summarizer <- Agent$new(
   name = "summarizer",
   instruction = paste0(
   "You are an agent designed to summarise ",
   "a given text into 3 distinct bullet points."
   ),
   llm_object = openai_4_1_mini
 )

 translator <- Agent$new(
   name = "translator",
   instruction = "Your role is to translate a text from English to German",
   llm_object = openai_4_1_mini
 )
 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )

 lead_agent$register_agents(c(researcher, summarizer, translator))

 lead_agent$agents

 lead_agent$clear_agents()

 lead_agent$agents


## ------------------------------------------------
## Method `LeadAgent$remove_agents`
## ------------------------------------------------

  # An API KEY is required in order to invoke the agents
  openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )
 researcher <- Agent$new(
   name = "researcher",
   instruction = paste0(
   "You are a research assistant. ",
   "Your job is to answer factual questions with detailed and accurate information. ",
   "Do not answer with more than 2 lines"
   ),
   llm_object = openai_4_1_mini
 )

 summarizer <- Agent$new(
   name = "summarizer",
   instruction = "You are agent designed to summarise a given text into 3 distinct bullet points.",
   llm_object = openai_4_1_mini
 )

 translator <- Agent$new(
   name = "translator",
   instruction = "Your role is to translate a text from English to German",
   llm_object = openai_4_1_mini
 )


 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )

 lead_agent$register_agents(c(researcher, summarizer, translator))

 lead_agent$agents

 # deleting the translator agent

 id_translator_agent <- translator$agent_id

 lead_agent$remove_agents(id_translator_agent)

 lead_agent$agents


## ------------------------------------------------
## Method `LeadAgent$register_agents`
## ------------------------------------------------

  # An API KEY is required in order to invoke the agents
  openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )
 researcher <- Agent$new(
   name = "researcher",
   instruction = paste0(
   "You are a research assistant. ",
   "Your job is to answer factual questions with detailed and accurate information. ",
   "Do not answer with more than 2 lines"
   ),
   llm_object = openai_4_1_mini
 )

 summarizer <- Agent$new(
   name = "summarizer",
   instruction = "You are agent designed to summarise a given text into 3 distinct bullet points.",
   llm_object = openai_4_1_mini
 )

 translator <- Agent$new(
   name = "translator",
   instruction = "Your role is to translate a text from English to German",
   llm_object = openai_4_1_mini
 )

 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )

 lead_agent$register_agents(c(researcher, summarizer, translator))

 lead_agent$agents

## ------------------------------------------------
## Method `LeadAgent$visualize_plan`
## ------------------------------------------------

## Not run: 
 lead_agent$generate_plan("Write a short history of Timgad, then translate it into French")
 lead_agent$visualize_plan()

## End(Not run)

## ------------------------------------------------
## Method `LeadAgent$invoke`
## ------------------------------------------------

## Not run: 
 # An API KEY is required in order to invoke the agents
  openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )
 researcher <- Agent$new(
   name = "researcher",
   instruction = paste0(
   "You are a research assistant. ",
   "Your job is to answer factual questions with detailed ",
   "and accurate information. Do not answer with more than 2 lines"
   ),
   llm_object = openai_4_1_mini
 )

 summarizer <- Agent$new(
   name = "summarizer",
   instruction = "You are agent designed to summarise a given text into 3 distinct bullet points.",
   llm_object = openai_4_1_mini
 )

 translator <- Agent$new(
   name = "translator",
   instruction = "Your role is to translate a text from English to German",
   llm_object = openai_4_1_mini
 )

 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )

 lead_agent$register_agents(c(researcher, summarizer, translator))

 lead_agent$invoke(
 paste0(
  "Describe the economic situation in Algeria in 3 sentences. ",
  "Answer in German"
  )
 )

## End(Not run)

## ------------------------------------------------
## Method `LeadAgent$generate_plan`
## ------------------------------------------------

## Not run: 
 # An API KEY is required in order to invoke the agents
  openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )
 researcher <- Agent$new(
   name = "researcher",
   instruction = paste0(
   "You are a research assistant. Your job is to answer factual questions ",
   "with detailed and accurate information. Do not answer with more than 2 lines"
   ),
   llm_object = openai_4_1_mini
 )

 summarizer <- Agent$new(
   name = "summarizer",
   instruction = "You are agent designed to summarise a given text into 3 distinct bullet points.",
   llm_object = openai_4_1_mini
 )

 translator <- Agent$new(
   name = "translator",
   instruction = "Your role is to translate a text from English to German",
   llm_object = openai_4_1_mini
 )

 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )

 lead_agent$register_agents(c(researcher, summarizer, translator))

 lead_agent$generate_plan(
 paste0(
  "Describe the economic situation in Algeria in 3 sentences. ",
  "Answer in German"
  )
 )

## End(Not run)

## ------------------------------------------------
## Method `LeadAgent$set_daemons`
## ------------------------------------------------

## Not run: 
openai_4_1_mini <- ellmer::chat(
  name = "openai/gpt-4.1-mini",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)
lead_agent <- LeadAgent$new(name = "Leader", llm_object = openai_4_1_mini)
lead_agent$set_daemons(4)

## End(Not run)

## ------------------------------------------------
## Method `LeadAgent$broadcast`
## ------------------------------------------------

## Not run: 
 # An API KEY is required in order to invoke the agents
openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )
openai_4_1 <- ellmer::chat(
  name = "openai/gpt-4.1",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)

openai_4_1_agent <- Agent$new(
  name = "openai_4_1_agent",
  instruction = "You are an AI assistant. Answer in 1 sentence max.",
  llm_object = openai_4_1
)

openai_4_1_nano <- ellmer::chat(
  name = "openai/gpt-4.1-nano",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)

openai_4_1_nano_agent <- Agent$new(
  name = "openai_4_1_nano_agent",
  instruction = "You are an AI assistant. Answer in 1 sentence max.",
  llm_object = openai_4_1_nano
  )

 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )

lead_agent$register_agents(c(openai_4_1_agent, openai_4_1_nano_agent))
lead_agent$broadcast(
  prompt = paste0(
    "If I were Algerian, which song would I like to sing ",
    "when running under the rain? how about a flower?"
  )
  )

## End(Not run)

## ------------------------------------------------
## Method `LeadAgent$set_hitl`
## ------------------------------------------------

## Not run: 
 # An API KEY is required in order to invoke the agents
  openai_4_1_mini <- ellmer::chat(
    name = "openai/gpt-4.1-mini",
    api_key = Sys.getenv("OPENAI_API_KEY"),
    echo = "none"
  )
 researcher <- Agent$new(
   name = "researcher",
   instruction = paste0(
    "You are a research assistant. ",
    "Your job is to answer factual questions with detailed and accurate information. ",
    "Do not answer with more than 2 lines"
   ),
   llm_object = openai_4_1_mini
 )

 summarizer <- Agent$new(
   name = "summarizer",
   instruction = paste0(
   "You are agent designed to summarise a give text ",
   "into 3 distinct bullet points."
   ),
   llm_object = openai_4_1_mini
 )

 translator <- Agent$new(
   name = "translator",
   instruction = "Your role is to translate a text from English to German",
   llm_object = openai_4_1_mini
 )

 lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
 )

 lead_agent$register_agents(c(researcher, summarizer, translator))

 # setting a human in the loop in step 2
 lead_agent$set_hitl(1)

 # The execution will stop at step 2 and a human will be able
 # to either accept the answer, modify it or stop the execution of
 # the workflow

 lead_agent$invoke(
 paste0(
  "Describe the economic situation in Algeria in 3 sentences. ",
  "Answer in German"
  )
 )

## End(Not run)

## ------------------------------------------------
## Method `LeadAgent$set_validation`
## ------------------------------------------------

## Not run: 
openai_4_1_mini <- ellmer::chat(
  name = "openai/gpt-4.1-mini",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)
researcher <- Agent$new(
  name = "researcher",
  instruction = "You answer factual questions in at most 2 lines.",
  llm_object = openai_4_1_mini
)
translator <- Agent$new(
  name = "translator",
  instruction = "You translate English text to German.",
  llm_object = openai_4_1_mini
)
lead_agent <- LeadAgent$new(name = "Leader", llm_object = openai_4_1_mini)
lead_agent$register_agents(c(researcher, translator))

lead_agent$set_memory(Memory$new(path = "team_memory.json"))
lead_agent$set_validation(
  criteria = "Accurate, complete, and in the language the task asks for.",
  threshold = 0.7
)

lead_agent$invoke("Describe Algeria's economy in 3 sentences, in German.")
lead_agent$agent_track_record()
lead_agent$memory$lessons

## End(Not run)

## ------------------------------------------------
## Method `LeadAgent$judge_and_choose_best_response`
## ------------------------------------------------

## Not run: 
openai_4_1_mini <- ellmer::chat(
  name = "openai/gpt-4.1-mini",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)
openai_4_1 <- ellmer::chat(
  name = "openai/gpt-4.1",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)

stylist <- Agent$new(
  name = "stylist",
  instruction = "You are an AI assistant. Answer in 1 sentence max.",
  llm_object = openai_4_1
)

openai_4_1_nano <- ellmer::chat(
  name = "openai/gpt-4.1-nano",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)

stylist2 <- Agent$new(
  name = "stylist2",
  instruction = "You are an AI assistant. Answer in 1 sentence max.",
  llm_object = openai_4_1_nano
)

lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
)

lead_agent$register_agents(c(stylist, stylist2))

lead_agent$judge_and_choose_best_response("what's the best way to war a kalvin klein shirt?")


## End(Not run)

## ------------------------------------------------
## Method `LeadAgent$agents_dialog`
## ------------------------------------------------

## Not run: 
# An API KEY is required in order to invoke the agents
openai_4_1_mini <- ellmer::chat(
  name = "openai/gpt-4.1-mini",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)
openai_4_1_nano <- ellmer::chat(
  name = "openai/gpt-4.1-nano",
  api_key = Sys.getenv("OPENAI_API_KEY"),
  echo = "none"
)

creative_writer <- Agent$new(
  name = "creative_writer",
  instruction = "You are a creative writer. Focus on engaging storytelling.",
  llm_object = openai_4_1_nano
)

editor <- Agent$new(
  name = "editor",
  instruction = "You are an editor. Focus on clarity and conciseness.",
  llm_object = openai_4_1_mini
)

lead_agent <- LeadAgent$new(
  name = "Leader",
  llm_object = openai_4_1_mini
)

lead_agent$register_agents(c(creative_writer, editor))

result <- lead_agent$agents_dialog(
  prompt = "Write a compelling opening sentence for a sci-fi novel.",
  agent_1_id = creative_writer$agent_id,
  agent_2_id = editor$agent_id,
  max_iterations = 3
)

# Access the final response
result$final_response

# View the dialog history
result$dialog_history

## End(Not run)

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