| LeadAgent | R Documentation |
'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.
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.
mini007::Agent -> LeadAgent
agentsA named list of registered sub-agents (by UUID).
agents_interactionA list of delegated task history with agent IDs, prompts, and responses.
planA list containing the most recently generated task plan.
hitl_stepsThe steps where the workflow should be stopped in order to allow for a human interaction
hitl_modeEither '"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_planThe prompt used to generate the plan.
agents_for_planThe agents used for the plan
dialog_historyA list storing the history of agent dialogs
broadcast_historyA list storing the history of broadcast interactions
n_daemonsNumber of parallel daemons used for parallel broadcast. Set via $set_daemons().
validationThe validation settings ('criteria', 'threshold',
'max_reassign') set via $set_validation(), or 'NULL'.
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$update_instruction()mini007::Agent$validate_response()new()Initializes the LeadAgent with a built-in task-decomposition prompt.
LeadAgent$new(name, llm_object, budget = NA)
nameA short name for the coordinator (e.g. '"lead"').
llm_objectThe LLM object generate by ellmer (eg. output of ellmer::chat_openai)
budgetBudget 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.
# 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
)
clear_agents()Clear out the registered Agents
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
remove_agents()Remove registered agents by IDs
LeadAgent$remove_agents(agent_ids)
agent_idsThe Agent ID to remove from the registered 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
register_agents()Register one or more agents for delegation.
LeadAgent$register_agents(agents)
agentsA vector of 'Agent' objects to register.
# 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
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.
LeadAgent$visualize_plan(direction = c("LR", "TB"))direction'[character(1)]' '"LR"' (left to right, the default) or '"TB"' (top to bottom).
A 'DiagrammeR' 'grViz' htmlwidget.
\dontrun{
lead_agent$generate_plan("Write a short history of Timgad, then translate it into French")
lead_agent$visualize_plan()
}
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.
LeadAgent$invoke( prompt, force_regenerate_plan = FALSE, stream = FALSE, on_event = NULL )
promptThe complex user instruction to process.
force_regenerate_planIf 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.
The final response (from the last agent in the sequence).
\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"
)
)
}
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.
LeadAgent$resume(
request_id,
action = c("continue", "edit", "abort"),
value = NULL,
stream = FALSE,
on_event = NULL
)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.
'[character(1)]' The final response, or another 'mini007_pending' object if a further HITL step is reached.
generate_plan()Generates a task execution plan without executing the subtasks. It returns a structured list containing the subtask, the selected agent, and metadata.
LeadAgent$generate_plan(prompt)
promptA complex instruction to be broken into subtasks.
A list of lists containing agent_id, agent_name, model_name, model_provider, and the assigned prompt.
\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"
)
)
}
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.
LeadAgent$set_daemons(n = 0L)
n'[integerish(1)]' Number of daemon processes (default '0'). Use '0' to shut down daemons.
Invisibly returns 'self'.
\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)
}
broadcast()Broadcasts a prompt to all registered agents and collects their responses. This does not affect the main agent orchestration logic or history.
LeadAgent$broadcast(prompt, parallel = FALSE, synthesize = FALSE)
promptA 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'.
A list of responses from all agents, or a single character string when 'synthesize = TRUE'.
\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?"
)
)
}
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.
LeadAgent$set_hitl(steps, mode = c("console", "pause"))stepsAt which steps the Human In The Loop is required?
mode'[character(1)]' '"console"' (default) or '"pause"'.
A list of responses from all agents.
\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"
)
)
}
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.
LeadAgent$set_validation(criteria, threshold = 0.7, max_reassign = 1)
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.
Invisibly, 'self'.
\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
}
agent_track_record()Summarise the recorded outcomes of every registered agent. Requires a
memory (see $set_memory()).
LeadAgent$agent_track_record()
A data frame with columns 'agent_name', 'n_tasks', 'n_success', 'success_rate' and 'mean_score', one row per registered agent.
judge_and_choose_best_response()The Lead Agent send a prompt to its registered agents and choose the best response from the agents' responses
LeadAgent$judge_and_choose_best_response(prompt)
promptThe prompt to send to the registered agents
A list of responses from all agents, including the chosen response
\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?")
}
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.
LeadAgent$agents_dialog(prompt, agent_1_id, agent_2_id, max_iterations = 5)
promptThe initial task or question for the agents to discuss.
agent_1_idThe ID of the first agent to participate in the dialog.
agent_2_idThe ID of the second agent to participate in the dialog.
max_iterationsMaximum number of back-and-forth exchanges (default: 5).
A list containing the final response, consensus status, and complete dialog history.
\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
}
clone()The objects of this class are cloneable with this method.
LeadAgent$clone(deep = FALSE)
deepWhether to make a deep clone.
## ------------------------------------------------
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.