https://discord.gg/botpress logo
How to implement Multi-Agents in Botpress?
# 🤝help
f
Hello, I am currently developing a lead-qualification WhatsApp chatbot, and since the system prompt/instructions are getting increasingly long, complex, and costly, I've been thinking of using multiple "agents"/autonomous nodes (ANs) instead of letting only one AN handle all the tasks. To start, I'd like to "delegate" the user-classification task to a separate AN. Specifically, I'd like the first AN to make a call to the second one with the necessary classification criteria. Then I'd like the second AN to return the group where the user falls into, according to the provided criteria. Then the first AN should proceed with the necessary steps, based on the group. My question is, how do I ideally go about this?. I've tried using the "Generate Content" card, but it's quite limited for more complex tasks, in particular, because it cannot have its own tools. I've also tried using a transition to another AN, but then the first AN doesn't really wait for a response, and I'm not sure how to send the second AN's response back to the first one in this case. Thanks for any hints!
p
Hi! A recommended approach would be to create workflows for the different tasks that your bot might execute during a conversation. Then your first autonomous node can route the conversation to the other workflows depending on what the user is expressing. Those workflows can also contain autonomous nodes if that's your preferred implementation. Hope this helps!
f
Hi, thanks alot for sharing your solution! If I use this method, how would I go about passing data from one to the other workflow and back again? And how does the "main" AN know when the sub-workflow/task is complete, so that it can continue the conversation? For example, I'd like to instruct the "main" agent to "route to the classification workflow to classify the user based on the following criteria: A, B, C, ... . Then, based on the result of the user classification, perform task1 or task2 and continue the converdation ...".
p
The "main" AN node regains control of the conversation when the workflows complete meaning the exit nodes are reached. There can be variables associated to exit nodes which will be returned. However, something to consider is that the autonomous node runs in an iterative manner which means it might overlook incoming information past the first iteration. To avoid loosing information about workflows when it returns, I would recommend adding a second router within a workflow that needs to do different actions based on the type of user let's say. Therefore, when the main node is returned to, it would be to end the conversation or do something that doesn't depend on the information gathered in the prior workflow.
f
Thank you so much for the insightful explanation! I was unaware that workflows can be used in such a way (input & output variables) and return straight to the AN that called it instead of the main workflow. This opens so many possibilities. As you can see in the screenshots attached, I've played around with a simple user classification agent/workflow for testing, and it works just fine! I've instructed the main AN to collect the user data and then call the classification workflow. To handle the output, I've written something like "Store the workflow's output in the variable 'workflow.user_group' and then inform the user about their group.". Is this the correct way to handle the output of the second workflow or is there a better, more elegant way?. Also, I'm not quite sure what you meant by "adding a second router" to avoid losing information about workflows. My setup seems to work just fine. Could you expand on that? https://cdn.discordapp.com/attachments/1366110612140068894/1368170011721465887/image.png?ex=68173f1e&is=6815ed9e&hm=f864782d199cd487c985612796d8e950344824da1e106fdd16f66fd467e2883a& https://cdn.discordapp.com/attachments/1366110612140068894/1368170011990036551/image.png?ex=68173f1e&is=6815ed9e&hm=1852ea0a3df5c5d2fd17348f8fccd3c916478b06a052bc39c1e42ac2861ff766&
p
Hey, glad to see your bot working as expected!! You indeed are passing the variables as expected. If you wanted to exit the router AN after the workflow completed you could enable the exit node to proceed to a different AN that had instructions regarding a second step as seen in the attached picture. The other possibility as mentioned above is to develop a nested version of the main router and children workflows, which would help further distinguish the possible decisions. This involves having another router within a workflow that leads to more workflows. I only mentioned this because the main AN sometimes struggles to execute different instructions based on the information it has when it returns from a workflow, and those options help avoid providing complex instructions to the AN since that's when it starts answering poorly. https://cdn.discordapp.com/attachments/1366110612140068894/1369021476371693721/Screenshot_2025-05-05_at_2.41.13_PM.png?ex=681a581b&is=6819069b&hm=e474a55ea7c65400744e8495f78ce15ff586d7dfdc3b606478fa7c05e579fb60&
f
Thank you again for taking the time to explain it. I now understand what you meant by adding a router, and I'll definitely give it a try. Might save some LLM cost too, since the number of tokens the system prompt of my main AN takes is already reaching the 5-digit mark. All my questions are answered and the threat can be closed 👍