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
d7c8dfdd-0981-494c-b566-c79cda798196
[[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 like GDP, inflation, employment, and major industries."
[[2]]
[[2]]$agent_id
d25a8a57-05e4-4b43-af4b-8aa71a4bcfc5
[[2]]$agent_name
[1] "summarizer"
[[2]]$model_provider
[1] "OpenAI"
[[2]]$model_name
[1] "gpt-4.1-mini"
[[2]]$prompt
[1] "Summarize the researched information into 3 concise bullet points highlighting the main aspects of Algeria's economic situation."
[[3]]
[[3]]$agent_id
1c6dc8e6-3ed5-49f4-aaf1-d868062c80fe
[[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 from English into German accurately, maintaining the original meaning."
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 Wirtschaft Algeriens ist stark von Kohlenwasserstoffen abhängig, die
30-40 % des BIP und mehr als 90 % der Exporte ausmachen.
- Das Land verzeichnet ein moderates BIP-Wachstum (~2-3 %) bei einer Inflation
zwischen 5-7 %.
- Die Arbeitslosigkeit bleibt mit etwa 11-12 % hoch, besonders betroffen ist
die Jugend, ebenso wie wichtige Sektoren wie Öl und Gas, Landwirtschaft und
Bauwesen.
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
d7c8dfdd-0981-494c-b566-c79cda798196
[[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 like GDP, inflation, employment, and major industries."
[[1]]$response
As of early 2024, Algeria's economy is driven by hydrocarbons, contributing
about 30-40% of GDP and over 90% of exports. GDP growth is moderate (~2-3%),
inflation is around 5-7%, and unemployment remains high (~11-12%), especially
among youth. Key industries include oil and gas, agriculture, and construction.
[[1]]$edited_by_hitl
[1] FALSE
[[2]]
[[2]]$agent_id
d25a8a57-05e4-4b43-af4b-8aa71a4bcfc5
[[2]]$agent_name
[1] "summarizer"
[[2]]$model_provider
[1] "OpenAI"
[[2]]$model_name
[1] "gpt-4.1-mini"
[[2]]$prompt
[1] "Summarize the researched information into 3 concise bullet points highlighting the main aspects of Algeria's economic situation."
[[2]]$response
- Algeria's economy heavily relies on hydrocarbons, accounting for 30-40% of
GDP and more than 90% of exports.
- The country experiences moderate GDP growth (~2-3%) with inflation between
5-7%.
- Unemployment remains high at around 11-12%, particularly affecting the youth,
alongside key sectors like oil and gas, agriculture, and construction.
[[2]]$edited_by_hitl
[1] FALSE
[[3]]
[[3]]$agent_id
1c6dc8e6-3ed5-49f4-aaf1-d868062c80fe
[[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 from English into German accurately, maintaining the original meaning."
[[3]]$response
- Die Wirtschaft Algeriens ist stark von Kohlenwasserstoffen abhängig, die
30-40 % des BIP und mehr als 90 % der Exporte ausmachen.
- Das Land verzeichnet ein moderates BIP-Wachstum (~2-3 %) bei einer Inflation
zwischen 5-7 %.
- Die Arbeitslosigkeit bleibt mit etwa 11-12 % hoch, besonders betroffen ist
die Jugend, ebenso wie wichtige Sektoren wie Öl und Gas, Landwirtschaft und
Bauwesen.
[[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] "bf421e87-b811-48ae-9394-bae3cd4f0add"
[[1]]$agent_name
[1] "openai_4_1_agent"
[[1]]$model_provider
[1] "OpenAI"
[[1]]$model_name
[1] "gpt-4.1"
[[1]]$response
As an Algerian, you might love singing "Ya Rayah" when running under the rain,
while a flower might prefer to "sing" its own song of blooming in the freshness
of the rain.
[[2]]
[[2]]$agent_id
[1] "983e2eed-ae6a-4ab1-a459-482a7c33d61e"
[[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 when
running under the rain, and "Ahlam" by Cheb Khaled for 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] "bf421e87-b811-48ae-9394-bae3cd4f0add"
[[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
As an Algerian, you might love singing "Ya Rayah" when running under the rain,
while a flower might prefer to "sing" its own song of blooming in the freshness
of the rain.
[[1]]$responses[[2]]
[[1]]$responses[[2]]$agent_id
[1] "983e2eed-ae6a-4ab1-a459-482a7c33d61e"
[[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 when
running under the rain, and "Ahlam" by Cheb Khaled for 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] "27528433-4b44-40f5-b9ba-d04fedd250f0"
$proposals[[1]]$agent_name
[1] "stylist"
$proposals[[1]]$response
Pair the blue Calvin Klein shirt and pink trousers with a tailored navy or grey
overcoat, neutral shoes (like white or tan), and a scarf in a complementary
shade to create a stylish, balanced winter look.
$proposals[[2]]
$proposals[[2]]$agent_id
[1] "7a4e8023-b87f-430e-9afc-c89bcd6ae1b8"
$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 warm coat, keeping the pink trousers as a vibrant focal point.
$chosen_response
Pair the blue Calvin Klein shirt and pink trousers with a tailored navy or grey
overcoat, neutral shoes (like white or tan), and a scarf in a complementary
shade to create a stylish, balanced winter look.
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_responseLaunch a targeted digital marketing campaign in Germany highlighting premium
Ouergla dates’ unique heritage, traditional cultivation, and organic qualities,
partnered with German gourmet retailers, influencers, and active participation
in leading food trade fairs to build brand awareness and secure direct export
partnerships.
# View the dialog history
result$dialog_history[[1]]
[[1]]$iteration
[1] 1
[[1]]$agent_id
[1] "a330fc9e-4a3e-492f-8c0d-d6df8dc359c0"
[[1]]$agent_name
[1] "ceo1"
[[1]]$response
Launch a targeted digital marketing campaign in Germany highlighting the
premium quality and unique origin of Ouergla dates, partnered with German
gourmet retailers and influencers to build brand awareness and trust.
[[2]]
[[2]]$iteration
[1] 1
[[2]]$agent_id
[1] "941091d7-ad87-4977-9e26-2b91864f0b58"
[[2]]$agent_name
[1] "ceo2"
[[2]]$response
The proposal is strong in targeting digital channels and leveraging local
German influencers and retailers, which aligns well with boosting exports;
however, it could be enhanced by incorporating participation in key German
trade fairs and food expos to directly connect with distributors and retailers,
thereby fostering stronger business-to-business relationships critical for
sustained export growth.
Counterproposal: Launch a targeted digital marketing campaign in Germany
highlighting premium Ouergla dates, partnered with German gourmet retailers,
influencers, and active participation in leading food trade fairs to
simultaneously build brand awareness and establish direct export partnerships.
[[3]]
[[3]]$iteration
[1] 2
[[3]]$agent_id
[1] "a330fc9e-4a3e-492f-8c0d-d6df8dc359c0"
[[3]]$agent_name
[1] "ceo1"
[[3]]$response
The counterproposal effectively aligns with my role and requirements by adding
trade fairs participation, which is crucial for establishing direct export
partnerships and deeper market penetration in Germany, thus complementing
digital marketing and influencer engagement; I accept this enhanced strategy as
it respects our company’s export growth goals without imposing conflicting
constraints.
CONSENSUS: Launch a targeted digital marketing campaign in Germany highlighting
premium Ouergla dates, partnered with German gourmet retailers, influencers,
and active participation in leading food trade fairs to simultaneously build
brand awareness and establish direct export partnerships.
[[4]]
[[4]]$iteration
[1] 2
[[4]]$agent_id
[1] "941091d7-ad87-4977-9e26-2b91864f0b58"
[[4]]$agent_name
[1] "ceo2"
[[4]]$response
The consensus proposal aligns well with our goal as a dates company in Ouergla
aiming to boost German exports through a multi-channel approach, but to further
leverage Ouergla’s unique regional heritage and ensure sustainability appeal in
Germany’s market, I propose adding storytelling about traditional cultivation
practices and organic certifications in the messaging.
Counterproposal: Launch a targeted digital marketing campaign in Germany
highlighting premium Ouergla dates’ unique heritage, traditional cultivation,
and organic qualities, partnered with German gourmet retailers, influencers,
and active participation in leading food trade fairs to build brand awareness
and secure direct export partnerships.
[[5]]
[[5]]$iteration
[1] 3
[[5]]$agent_id
[1] "a330fc9e-4a3e-492f-8c0d-d6df8dc359c0"
[[5]]$agent_name
[1] "ceo1"
[[5]]$response
The addition of storytelling around Ouergla’s unique heritage, traditional
cultivation, and organic certification directly supports our brand’s
distinctiveness and matches growing consumer preferences in Germany, enhancing
the original strategy without creating conflicts or constraints; therefore,
this enriched messaging combined with digital campaigns, retail partnerships,
influencer engagement, and trade fair participation fully aligns with our
export growth goals.
CONSENSUS: Launch a targeted digital marketing campaign in Germany highlighting
premium Ouergla dates’ unique heritage, traditional cultivation, and organic
qualities, partnered with German gourmet retailers, influencers, and active
participation in leading food trade fairs to build brand awareness and secure
direct export partnerships.
[[6]]
[[6]]$iteration
[1] 3
[[6]]$agent_id
[1] "941091d7-ad87-4977-9e26-2b91864f0b58"
[[6]]$agent_name
[1] "ceo2"
[[6]]$response
CONSENSUS: Launch a targeted digital marketing campaign in Germany highlighting
premium Ouergla dates’ unique heritage, traditional cultivation, and organic
qualities, partnered with German gourmet retailers, influencers, and active
participation in leading food trade fairs to build brand awareness and secure
direct export partnerships.
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_responseImplement a phased, balanced marketing strategy over the next two years that
begins with cost-effective targeted digital campaigns and micro-influencer
partnerships within niche German markets, gradually scaling to include premium
events, collaborations with renowned chefs, luxury retail placement, and
broader multimedia storytelling based on export growth and budget performance
to maximize impact while controlling costs.
# View the dialog history
result$dialog_history[[1]]
[[1]]$iteration
[1] 1
[[1]]$agent_id
[1] "bb1e6fef-6439-4379-a850-d5be37edba65"
[[1]]$agent_name
[1] "ceo1"
[[1]]$response
Launch a premium, culturally immersive marketing campaign in Germany featuring
high-end sampling events, collaborations with top German chefs and influencers,
and extensive multimedia storytelling highlighting the unique quality and
heritage of Ouergla dates.
[[2]]
[[2]]$iteration
[1] 1
[[2]]$agent_id
[1] "1e6066a3-20d5-4d83-b7a4-424e23f9ce07"
[[2]]$agent_name
[1] "ceo2"
[[2]]$response
While your proposal to launch a premium campaign with high-end events and
collaborations could strongly boost our brand image, it seems costly and risks
exceeding our strict budget constraints, so I suggest a more cost-effective
strategy focusing on targeted digital marketing through German social media
platforms, leveraging affordable influencer partnerships and storytelling that
highlights Ouergla's dates' unique heritage and quality to stimulate demand
over the next two years.
[[3]]
[[3]]$iteration
[1] 2
[[3]]$agent_id
[1] "bb1e6fef-6439-4379-a850-d5be37edba65"
[[3]]$agent_name
[1] "ceo1"
[[3]]$response
Your suggestion to focus on cost-effective digital marketing and affordable
influencer partnerships conflicts with my unlimited budget approach and desire
to maximize spending for greater impact; therefore, I propose simultaneously
executing a large-scale premium campaign across Germany involving exclusive
high-end tastings, partnerships with Michelin-starred chefs, luxury retail
placements, plus a comprehensive digital and traditional media blitz to deeply
embed Ouergla dates into the German market over the next two years.
[[4]]
[[4]]$iteration
[1] 2
[[4]]$agent_id
[1] "1e6066a3-20d5-4d83-b7a4-424e23f9ce07"
[[4]]$agent_name
[1] "ceo2"
[[4]]$response
Your large-scale, high-budget approach clearly conflicts with my core
constraint of maintaining a low marketing spend; therefore, I counterpropose a
phased strategy that begins with focused digital marketing and micro-influencer
collaborations to build brand awareness cost-effectively in Germany, with
potential gradual scaling depending on export growth and budget availability
over the next two years.
[[5]]
[[5]]$iteration
[1] 3
[[5]]$agent_id
[1] "bb1e6fef-6439-4379-a850-d5be37edba65"
[[5]]$agent_name
[1] "ceo1"
[[5]]$response
Your phased, low-budget approach directly conflicts with my position of having
no budget constraints and the intent to maximize spending and impact
immediately; thus, I firmly propose implementing a full-scale, multi-channel
marketing blitz from the outset, including luxury events, top-tier influencer
partnerships, extensive traditional and digital advertising, and strategic
collaborations within Germany's premium retail and culinary sectors to
exponentially increase Ouergla dates exports over the next two years.
[[6]]
[[6]]$iteration
[1] 3
[[6]]$agent_id
[1] "1e6066a3-20d5-4d83-b7a4-424e23f9ce07"
[[6]]$agent_name
[1] "ceo2"
[[6]]$response
Your insistence on a full-scale, high-cost marketing blitz conflicts with my
fixed priority of controlling expenses to avoid overspending; therefore, I
propose a balanced approach: initiate a targeted, low-cost digital campaign
focusing on niche German markets and build incremental collaborations with
mid-tier influencers and specialty retailers, allowing us to closely monitor
export growth and optimize budget allocation over two years without risking
financial strain.