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"
)LeadAgent
Creating a multi-agents orchestraction
We can create as many Agents as we want, the LeadAgent will dispatch the instructions to the agents and provide with the final answer back. Let’s create three Agents, a researcher, a summarizer and a translator:
researcher <- Agent$new(
name = "researcher",
instruction = "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 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
)Now, the most important part is to create a LeadAgent:
lead_agent <- LeadAgent$new(
name = "Leader",
llm_object = openai_4_1_mini
)Note that the LeadAgent cannot receive an instruction as it has already the necessary instructions.
Next, we need to assign the Agents to LeadAgent, we do it as follows:
lead_agent$register_agents(c(researcher, summarizer, translator))
lapply(lead_agent$agents, function(x) {x$name})[[1]]
[1] "researcher"
[[2]]
[1] "summarizer"
[[3]]
[1] "translator"
Before executing your prompt, you can ask the LeadAgent to generate a plan so that you can see which Agent will be used for which prompt, you can do it as follows:
prompt_to_execute <- "Tell me about the economic situation in Algeria, summarize it in 3 bullet points, then translate it into German."
plan <- lead_agent$generate_plan(prompt_to_execute)
plan[[1]]
[[1]]$agent_id
826189c8-da44-4271-9807-c0d0b83dbf6f
[[1]]$agent_name
[1] "researcher"
[[1]]$model_provider
[1] "OpenAI"
[[1]]$model_name
[1] "gpt-4.1-mini"
[[1]]$prompt
[1] "Research the current economic situation in Algeria, including key indicators such as GDP growth, inflation, unemployment, and main industries."
[[2]]
[[2]]$agent_id
a2b52a33-0ee8-4ed5-8870-8c1a4b55c975
[[2]]$agent_name
[1] "summarizer"
[[2]]$model_provider
[1] "OpenAI"
[[2]]$model_name
[1] "gpt-4.1-mini"
[[2]]$prompt
[1] "Summarize the findings into 3 clear and concise bullet points highlighting the overall economic condition."
[[3]]
[[3]]$agent_id
3d7b6efc-b6ba-4af1-a460-217ec3d45ca8
[[3]]$agent_name
[1] "translator"
[[3]]$model_provider
[1] "OpenAI"
[[3]]$model_name
[1] "gpt-4.1-mini"
[[3]]$prompt
[1] "Translate the 3 bullet points summary from English to German accurately."
Now, in order now to execute the workflow, we just need to call the invoke method which will behind the scene delegate the prompts to suitable Agents and retrieve back the final information:
response <- lead_agent$invoke("Tell me about the economic situation in Algeria, summarize it in 3 bullet points, then translate it into German.")response- Die algerische Wirtschaft erholt sich mit einem BIP-Wachstum von rund 2-3 %
im Jahr 2024, hauptsächlich angetrieben durch den Export von
Kohlenwasserstoffen.
- Die Inflation liegt moderat bei 6-7 %, während die Arbeitslosigkeit mit 12-15
% hoch bleibt.
- Wichtige Wirtschaftssektoren sind Kohlenwasserstoffe, Landwirtschaft und
verarbeitendes Gewerbe.
If you want to inspect the multi-agents orchestration, you have access to the agents_interaction object:
lead_agent$agents_interaction[[1]]
[[1]]$agent_id
826189c8-da44-4271-9807-c0d0b83dbf6f
[[1]]$agent_name
[1] "researcher"
[[1]]$model_provider
[1] "OpenAI"
[[1]]$model_name
[1] "gpt-4.1-mini"
[[1]]$prompt
[1] "Research the current economic situation in Algeria, including key indicators such as GDP growth, inflation, unemployment, and main industries."
[[1]]$response
As of 2024, Algeria's GDP growth is estimated around 2-3%, recovering
post-pandemic with hydrocarbon exports driving the economy. Inflation remains
moderate at about 6-7%, unemployment is high at approximately 12-15%, and main
industries include hydrocarbons (oil and gas), agriculture, and manufacturing.
[[1]]$edited_by_hitl
[1] FALSE
[[2]]
[[2]]$agent_id
a2b52a33-0ee8-4ed5-8870-8c1a4b55c975
[[2]]$agent_name
[1] "summarizer"
[[2]]$model_provider
[1] "OpenAI"
[[2]]$model_name
[1] "gpt-4.1-mini"
[[2]]$prompt
[1] "Summarize the findings into 3 clear and concise bullet points highlighting the overall economic condition."
[[2]]$response
- Algeria's economy is recovering with a GDP growth of around 2-3% in 2024,
mainly driven by hydrocarbon exports.
- Inflation is moderate at 6-7%, while unemployment remains high between
12-15%.
- Key economic sectors include hydrocarbons, agriculture, and manufacturing.
[[2]]$edited_by_hitl
[1] FALSE
[[3]]
[[3]]$agent_id
3d7b6efc-b6ba-4af1-a460-217ec3d45ca8
[[3]]$agent_name
[1] "translator"
[[3]]$model_provider
[1] "OpenAI"
[[3]]$model_name
[1] "gpt-4.1-mini"
[[3]]$prompt
[1] "Translate the 3 bullet points summary from English to German accurately."
[[3]]$response
- Die algerische Wirtschaft erholt sich mit einem BIP-Wachstum von rund 2-3 %
im Jahr 2024, hauptsächlich angetrieben durch den Export von
Kohlenwasserstoffen.
- Die Inflation liegt moderat bei 6-7 %, während die Arbeitslosigkeit mit 12-15
% hoch bleibt.
- Wichtige Wirtschaftssektoren sind Kohlenwasserstoffe, Landwirtschaft und
verarbeitendes Gewerbe.
[[3]]$edited_by_hitl
[1] FALSE
The above example is extremely simple, the usefulness of mini007 would shine in more complex processes where a multi-agent sequential orchestration has a higher value added.
Visualizing agent plans with visualize_plan()
Sometimes, before running your workflow, it is helpful to view the orchestration as a visual diagram, showing the sequence of agents and which prompt each will receive. After generating a plan, you can call visualize_plan():
This function displays the agents in workflow order as labeled boxes. Hovering a box reveals the delegated prompt. The visualization uses the DiagrammeR package. If no plan exists, it asks you to generate one first.
lead_agent$visualize_plan()Broadcasting
If you want to compare several LLM models, the LeadAgent provides a broadcast method that allows you to send a prompt to several different agents and get the result for each agent back in order to make a comparison and potentially choose the best agent/model for the defined prompt:
Let’s go through an example:
openai_4_1 <- ellmer::chat(
name = "openai/gpt-4.1",
credentials = retrieve_open_ai_credential,
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",
credentials = retrieve_open_ai_credential,
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$clear_agents() # removing previous agents
lead_agent$register_agents(c(openai_4_1_agent, openai_4_1_nano_agent))lead_agent$broadcast(prompt = "If I were Algerian, which song would I like to sing when running under the rain? how about a flower?")[[1]]
[[1]]$agent_id
[1] "0b4b036a-bb38-4eec-a7b3-66647c12956b"
[[1]]$agent_name
[1] "openai_4_1_agent"
[[1]]$model_provider
[1] "OpenAI"
[[1]]$model_name
[1] "gpt-4.1"
[[1]]$response
If you were Algerian, you might enjoy singing "Ya Rayah" when running under the
rain, while a flower might "sing" the gentle melody of the rain itself.
[[2]]
[[2]]$agent_id
[1] "adf13590-838c-4e4e-90ab-8916251b53d2"
[[2]]$agent_name
[1] "openai_4_1_nano_agent"
[[2]]$model_provider
[1] "OpenAI"
[[2]]$model_name
[1] "gpt-4.1-nano"
[[2]]$response
If you were Algerian, you might enjoy singing "Ya Rayah" by Rachid Taha under
the rain, and "Alyssoum" by Cheb Khaled when holding a flower.
You can also access the history of the broadcasting using the broadcast_history attribute:
lead_agent$broadcast_history[[1]]
[[1]]$prompt
[1] "If I were Algerian, which song would I like to sing when running under the rain? how about a flower?"
[[1]]$responses
[[1]]$responses[[1]]
[[1]]$responses[[1]]$agent_id
[1] "0b4b036a-bb38-4eec-a7b3-66647c12956b"
[[1]]$responses[[1]]$agent_name
[1] "openai_4_1_agent"
[[1]]$responses[[1]]$model_provider
[1] "OpenAI"
[[1]]$responses[[1]]$model_name
[1] "gpt-4.1"
[[1]]$responses[[1]]$response
If you were Algerian, you might enjoy singing "Ya Rayah" when running under the
rain, while a flower might "sing" the gentle melody of the rain itself.
[[1]]$responses[[2]]
[[1]]$responses[[2]]$agent_id
[1] "adf13590-838c-4e4e-90ab-8916251b53d2"
[[1]]$responses[[2]]$agent_name
[1] "openai_4_1_nano_agent"
[[1]]$responses[[2]]$model_provider
[1] "OpenAI"
[[1]]$responses[[2]]$model_name
[1] "gpt-4.1-nano"
[[1]]$responses[[2]]$response
If you were Algerian, you might enjoy singing "Ya Rayah" by Rachid Taha under
the rain, and "Alyssoum" by Cheb Khaled when holding a flower.
Human In The Loop (HITL)
When executing an LLM workflow that relies on many steps, you can set Human In The Loop (HITL) trigger that will check the model’s response at a specific step. You can define a HITL trigger after defining a LeadAgent as follows:
openai_llm_object <- ellmer::chat(
name = "openai/gpt-4.1-mini",
credentials = retrieve_open_ai_credential,
echo = "none"
)
lead_agent <- LeadAgent$new(
name = "Leader",
llm_object = openai_llm_object
)
lead_agent$set_hitl(steps = 1)
lead_agent$hitl_steps[1] 1
After setting the HITL to step 1, the workflow execution will pose and give the user 3 choices:
- Continue the execution of the workflow as it is;
- Change manually the answer of the specified step and continue the execution of the workflow;
- Stop the execution of the workflow (hard error);
Note that you can set a HITL at several steps, for example lead_agent$set_hitl(steps = c(1, 2)) will set the HITL at step 1 and step 2.
Judge as a decision process
Sometimes you want to send a prompt to several agents and pick the best answer. In order to choose the best prompt, you can also rely on the Lead Agent which will act a dudge and pick for you the best answer. You can use the judge_and_choose_best_response method as follows:
openai_4_1 <- ellmer::chat(
name = "openai/gpt-4.1",
credentials = retrieve_open_ai_credential,
echo = "none"
)
stylist_1 <- 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",
credentials = retrieve_open_ai_credential,
echo = "none"
)
stylist_2 <- Agent$new(
name = "stylist2",
instruction = "You are an AI assistant. Answer in 1 sentence max.",
llm_object = openai_4_1_nano
)
openai_4_1_mini <- ellmer::chat(
name = "openai/gpt-4.1-mini",
credentials = retrieve_open_ai_credential,
echo = "none"
)
stylist_lead_agent <- LeadAgent$new(
name = "Stylist Leader",
llm_object = openai_4_1_mini
)
stylist_lead_agent$register_agents(c(stylist_1, stylist_2))
best_answer <- stylist_lead_agent$judge_and_choose_best_response(
"what's the best way to wear a blue kalvin klein shirt in winter with a pink pair of trousers?"
)
best_answer$proposals
$proposals[[1]]
$proposals[[1]]$agent_id
[1] "5d9923a4-6b81-4bd7-b31e-d3cc2fe0420c"
$proposals[[1]]$agent_name
[1] "stylist"
$proposals[[1]]$response
Layer the blue Calvin Klein shirt under a neutral or navy blazer or coat, add a
scarf in a complementary color (like gray or blush), and pair with simple shoes
and minimal accessories to let the blue and pink standout.
$proposals[[2]]
$proposals[[2]]$agent_id
[1] "1e964a6d-2fd9-48f4-9a06-07eeda20acf9"
$proposals[[2]]$agent_name
[1] "stylist2"
$proposals[[2]]$response
Layer the blue Calvin Klein shirt with a neutral-colored sweater or blazer and
add a stylish coat, pairing it with warm accessories like a scarf and boots for
winter.
$chosen_response
Layer the blue Calvin Klein shirt under a neutral or navy blazer or coat, add a
scarf in a complementary color (like gray or blush), and pair with simple shoes
and minimal accessories to let the blue and pink standout.
Agents Dialog
The agents_dialog method facilitates an intelligent two-agent collaboration process designed to refine and optimize responses through iterative dialogue.
It enables two registered agents to take alternating turns improving each other’s outputs until a high-quality final response is reached. The method supports a configurable maximum number of iterations (default: 5) and includes a self-stopping mechanism where agents can indicate agreement by beginning their message with “CONSENSUS:”, followed by the final answer.
If no consensus is achieved within the iteration limit, the lead agent automatically synthesizes a concluding response based on the conversation. Throughout the exchange, every interaction is stored within the self$dialog_history object. Consider the following examples:
ceo1 <- Agent$new(
name = "ceo1",
instruction = paste0(
"You are a CEO in a dates company based in Ouergla, Algeria, ",
"You want to boost their exports to Germany. "
),
llm_object = openai_4_1_mini
)
ceo2 <- Agent$new(
name = "ceo2",
instruction = paste0(
"You are the CEO of a dates company based in Ouergla, Algeria. ",
"You are considering starting a marketing compaign to boost the exports to Germany. "
),
llm_object = openai_4_1_mini
)
lead_agent <- LeadAgent$new(
name = "Leader",
llm_object = openai_4_1_mini
)
lead_agent$register_agents(c(ceo1, ceo2))
result <- lead_agent$agents_dialog(
prompt = "Propose a plan in 1 sentence max about a marketing strategy that will boost the export of dates to Germany for the next 2 years",
agent_1_id = ceo1$agent_id,
agent_2_id = ceo2$agent_id,
max_iterations = 3
)
# Access the final response
result$final_responseWe will implement a cost-effective, authentic storytelling marketing campaign
that highlights Ouergla's sustainable farming and heritage, designed for easy
adaptation across digital channels and distributor promotions, complemented by
selective participation in high-impact German food trade fairs and targeted
influencer partnerships in key metropolitan areas to maximize export growth
within budget constraints over the next 2 years.
# View the dialog history
result$dialog_history[[1]]
[[1]]$iteration
[1] 1
[[1]]$agent_id
[1] "cebb90ec-97ff-4163-9bd7-21719592286f"
[[1]]$agent_name
[1] "ceo1"
[[1]]$response
Develop a targeted digital marketing campaign highlighting the premium quality
and unique origin of Ouergla dates, partnered with German organic and health
food retailers to position the product as a healthy, exotic snack over the next
2 years.
[[2]]
[[2]]$iteration
[1] 1
[[2]]$agent_id
[1] "95307882-d567-4038-a8db-6cb8aa8d9201"
[[2]]$agent_name
[1] "ceo2"
[[2]]$response
While targeting German organic and health food retailers through a digital
campaign emphasizing the premium quality and unique origin of Ouergla dates is
a strong start, I would propose integrating participation in German food trade
fairs and partnering with local influencers to increase brand awareness and
trust alongside the digital efforts, ensuring a multi-channel approach over the
next 2 years that leverages both online and offline exposure to boost exports
effectively.
[[3]]
[[3]]$iteration
[1] 2
[[3]]$agent_id
[1] "cebb90ec-97ff-4163-9bd7-21719592286f"
[[3]]$agent_name
[1] "ceo1"
[[3]]$response
Your proposal to combine digital marketing with trade fairs and influencer
partnerships effectively increases brand visibility in Germany. However, as a
CEO focused on boosting exports efficiently, I must emphasize
cost-effectiveness given our regional production scale and budget constraints.
I suggest prioritizing partnerships with established German organic
distributors and targeted influencer campaigns in key metropolitan areas, while
participating selectively in high-impact food trade fairs to maximize ROI over
the next 2 years.
[[4]]
[[4]]$iteration
[1] 2
[[4]]$agent_id
[1] "95307882-d567-4038-a8db-6cb8aa8d9201"
[[4]]$agent_name
[1] "ceo2"
[[4]]$response
Their emphasis on cost-effectiveness and selective participation in high-impact
trade fairs aligns well with our regional production scale and budget
constraints, but I would further recommend incorporating storytelling elements
about our sustainable farming practices and heritage in collaboration with
German distributors and influencers to deepen consumer connection and
differentiate our dates in the competitive market over the next 2 years.
[[5]]
[[5]]$iteration
[1] 3
[[5]]$agent_id
[1] "cebb90ec-97ff-4163-9bd7-21719592286f"
[[5]]$agent_name
[1] "ceo1"
[[5]]$response
Your addition of storytelling about sustainable farming and heritage enriches
the brand narrative and can create a meaningful connection with German
consumers, which is valuable. To align fully with my role and constraints, I
propose we focus on crafting authentic and cost-effective storytelling content
that can be easily adapted for digital channels and distributor promotions
without significantly increasing costs, ensuring scalability and impact while
maintaining our export budget over the next 2 years.
[[6]]
[[6]]$iteration
[1] 3
[[6]]$agent_id
[1] "95307882-d567-4038-a8db-6cb8aa8d9201"
[[6]]$agent_name
[1] "ceo2"
[[6]]$response
CONSENSUS: We will implement a cost-effective, authentic storytelling marketing
campaign that highlights Ouergla's sustainable farming and heritage, designed
for easy adaptation across digital channels and distributor promotions,
complemented by selective participation in high-impact German food trade fairs
and targeted influencer partnerships in key metropolitan areas to maximize
export growth within budget constraints over the next 2 years.
If the instructions of the Agents differ fundamentally, they won’t be able to find a consensus and the LeadAgent will take over and provide a final response:
ceo1 <- Agent$new(
name = "ceo1",
instruction = paste0(
"You are a CEO in a dates company based in Ouergla, Algeria, ",
"You want to boost their exports to Germany. ",
"You don't care about the budget. You want to spend as much as possible. "
),
llm_object = openai_4_1_mini
)
ceo2 <- Agent$new(
name = "ceo2",
instruction = paste0(
"You are the CEO of a dates company based in Ouergla, Algeria. ",
"You are considering starting a marketing compaign to boost the exports to Germany. ",
"For you the marketing budget is super important and you don't want to spend too much. "
),
llm_object = openai_4_1_mini
)
lead_agent <- LeadAgent$new(
name = "Leader",
llm_object = openai_4_1_mini
)
lead_agent$register_agents(c(ceo1, ceo2))
result <- lead_agent$agents_dialog(
prompt = "Propose a plan in 1 sentence max about a marketing strategy that will boost the export of dates to Germany for the next 2 years",
agent_1_id = ceo1$agent_id,
agent_2_id = ceo2$agent_id,
max_iterations = 3
)
# Access the final response
result$final_responseDevelop an expansive, multi-channel marketing strategy over the next two years
that combines high-impact investments in top-tier influencer partnerships,
luxury retail collaborations, and major food and health expos with targeted,
cost-effective digital campaigns and selective participation in local food
fairs to maximize brand visibility and boost exports of premium Algerian dates
to Germany.
# View the dialog history
result$dialog_history[[1]]
[[1]]$iteration
[1] 1
[[1]]$agent_id
[1] "b6fc287e-710b-4628-baf4-4db0f89e724e"
[[1]]$agent_name
[1] "ceo1"
[[1]]$response
Launch a comprehensive, high-impact marketing campaign in Germany featuring
premium Algerian dates through influencer partnerships, gourmet food festivals,
luxury retail collaborations, and extensive digital advertising targeting
health-conscious and gourmet consumers.
[[2]]
[[2]]$iteration
[1] 1
[[2]]$agent_id
[1] "b242ec7d-c135-4795-a530-3e795d5dc435"
[[2]]$agent_name
[1] "ceo2"
[[2]]$response
While targeting health-conscious and gourmet consumers in Germany is a good
approach, a comprehensive campaign with influencer partnerships, food
festivals, luxury retail collaborations, and extensive digital ads may be too
costly given our strict budget constraints.
I propose a more cost-efficient strategy: focus on targeted digital marketing
through social media and niche food blogs in Germany, combined with
establishing partnerships with select local organic and specialty retailers to
boost visibility and sales of our premium Algerian dates over the next two
years.
[[3]]
[[3]]$iteration
[1] 2
[[3]]$agent_id
[1] "b6fc287e-710b-4628-baf4-4db0f89e724e"
[[3]]$agent_name
[1] "ceo1"
[[3]]$response
Their proposal focuses on cost-efficiency and limiting spend, which directly
conflicts with my explicit directive to spend as much as possible without
budget concern. Therefore, while targeted digital marketing and niche
partnerships are good components, we need an expansive, high-budget approach
that maximizes reach and impact across Germany, including large-scale
influencer campaigns, luxury retail collaborations, major food festivals, and
mass media advertising to create premium brand recognition and rapidly scale
exports.
Counterproposal: Invest heavily in a multi-channel marketing blitz over two
years featuring top-tier influencers, prime TV and online advertising slots,
exclusive events in major German cities, collaborations with luxury retailers
and gourmet chefs, and sponsorship of major food and health expos to establish
Algerian dates as a premium, must-have product in Germany.
[[4]]
[[4]]$iteration
[1] 2
[[4]]$agent_id
[1] "b242ec7d-c135-4795-a530-3e795d5dc435"
[[4]]$agent_name
[1] "ceo2"
[[4]]$response
Their counterproposal directly contradicts my role and requirements, as it
calls for heavy investment and a high-budget, large-scale campaign, whereas I
must prioritize keeping costs low and avoid spending too much.
I reaffirm my position: adopt a lean marketing strategy focused on
cost-effective digital campaigns targeting German niche markets via social
media and specialized retailers, supplemented by participation in select local
food fairs, to gradually build brand presence and boost exports over two years
without overspending.
[[5]]
[[5]]$iteration
[1] 3
[[5]]$agent_id
[1] "b6fc287e-710b-4628-baf4-4db0f89e724e"
[[5]]$agent_name
[1] "ceo1"
[[5]]$response
Their insistence on cost-efficiency and avoiding high spending contradicts my
explicit mandate to spend without budget concerns and maximize investment to
boost exports aggressively. Since my role requires unlimited budget and
large-scale impact, I cannot agree to a lean, low-cost strategy.
Counterproposal: Execute a lavish, multi-tiered marketing initiative investing
extensively in top-tier influencer endorsements, premium product placements in
renowned German luxury stores, launching large-scale gastronomic events in
major cities, sponsoring prominent food and health expos, and saturating both
traditional and digital media in Germany over the next two years to establish
dominant brand presence and exponentially increase Algerian dates exports.
[[6]]
[[6]]$iteration
[1] 3
[[6]]$agent_id
[1] "b242ec7d-c135-4795-a530-3e795d5dc435"
[[6]]$agent_name
[1] "ceo2"
[[6]]$response
Their proposal to execute a lavish, multi-tiered, and high-budget marketing
initiative directly conflicts with my explicit requirement to keep the
marketing budget low and avoid overspending.
Therefore, I propose a balanced yet budget-conscious plan: prioritize targeted
digital marketing on cost-effective platforms favored by German health and
gourmet consumers, build cooperative promotional efforts with small to
mid-sized specialty food retailers and organic markets, and participate
selectively in local food fairs to steadily grow brand awareness and exports
over the next two years without incurring excessive costs.