Dynamic Agent Selection in CrewAI: Enhancing Efficiency Without Hardcoding

I’ve been working on an AI application that uses CrewAI for various tasks, such as analyzing data, managing schedules, and more. However, I’ve encountered a challenge in dynamically selecting and running only the agents relevant to the user’s query. the user enters a query, and the crew should use only related agents and tasks

Currently, when using the Crew class, all agents are initialized and executed, even if only one or two are needed for a specific task. I want to implement a seamless way to dynamically select agents based on the query’s intent without resorting to hardcoding the logic.

Here’s what I’m aiming for:

  1. Analyze the user query to determine the intent (e.g., data analysis, scheduling).
  2. Dynamically select the relevant agents for that intent.
  3. Pass only the selected agents and their tasks to the Crew instance.

I’ve tried creating a mapping between intents and agents, but it feels cumbersome and not as elegant as I’d like. I’m curious to know if anyone has implemented a similar solution or if there’s a built-in way in CrewAI to handle this efficiently.

1 Like

Did you try using the Process.hierarchial with a manager agent or manager llm.?

I would try either conditional tasks or the use of flows.
My preference would be the use of flows.
The first flow to identify the tasks to be executed.
The second one lists all my tasks with a router-type decorator.

@Mada I would like to know how you are doing this, i have a similar requirement.

I have a similar requirement and I tried Process.hierarchial with a manager agent. It was a disaster, tasks were not being delegated and sometimes they were not executed or were executed by the wrong agent. Sometimes the manager would hallucinate, sometimes the agent would completely lose context.

I would use Flows for this with an intent classifier step and a router for which crew needs to be kicked off. This is more deterministic

1 Like