Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:CrewAIInc CrewAI Delegation Tools

From Leeroopedia

Overview

Concrete tool classes for task delegation and information querying between agents in hierarchical crews provided by the CrewAI framework.

Source

Import

from crewai.tools.agent_tools import DelegateWorkTool, AskQuestionTool

Note: Users typically do not import or instantiate these tools directly. They are automatically assigned to the manager agent by the framework when a hierarchical crew is created. The import is shown here for reference purposes.

Signatures

DelegateWorkTool

Attribute Value
Class DelegateWorkTool (extends BaseTool)
name "Delegate work to coworker"
description "Useful to delegate a specific task to one of the following coworkers: {coworkers}..."
Schema DelegateWorkToolSchema

_run method:

def _run(
    self,
    task: str,
    context: str,
    coworker: str | None = None,
) -> str:

DelegateWorkToolSchema:

Field Type Description
task str The specific task to delegate to the coworker.
context str All necessary context and information for the coworker to complete the task.
coworker str The role name of the specialist agent to delegate to.

AskQuestionTool

Attribute Value
Class AskQuestionTool (extends BaseTool)
name "Ask question to coworker"
description "Useful to ask a question, opinion or take from one of the following coworkers: {coworkers}..."
Schema AskQuestionToolSchema

_run method:

def _run(
    self,
    question: str,
    context: str,
    coworker: str | None = None,
) -> str:

AskQuestionToolSchema:

Field Type Description
question str The specific question to ask the coworker.
context str All necessary context and background information for the coworker to answer the question.
coworker str The role name of the specialist agent to ask.

Base Agent Tools (Shared Logic)

Both tools inherit from BaseAgentTools which provides the shared execution logic in base_agent_tools.py:

class BaseAgentTools:
    agents: list[Agent]

    def _run(
        self,
        agent_role: str,
        task: str,
        context: str,
    ) -> str:
        # 1. Find agent by role name (fuzzy matching)
        # 2. Execute the task/question using the found agent
        # 3. Return the agent's response as a string

The base class handles:

  • Agent lookup -- Searches the list of available agents for one whose role matches the coworker parameter. Uses case-insensitive fuzzy matching to tolerate minor variations in role name spelling.
  • Task execution -- Invokes the found agent's execution method, passing the task/question and context.
  • Error handling -- If no matching agent is found, returns an error message indicating the coworker was not found and listing available coworkers.

Key Behaviors

  • Automatic injection -- When a hierarchical crew is assembled, the framework automatically creates instances of DelegateWorkTool and AskQuestionTool, configures them with references to the specialist agents, and adds them to the manager agent's tool set.
  • Dynamic coworker list -- The tool descriptions include a list of available coworkers (by role name), helping the manager's LLM understand who it can delegate to.
  • Fuzzy role matching -- The coworker parameter does not need to exactly match the specialist's role. The framework performs case-insensitive matching and can tolerate minor variations. However, unique and distinct role names are strongly recommended.
  • Context propagation -- Both tools accept a context parameter that passes relevant information to the specialist. This allows the manager to provide task-specific guidance beyond the original task description.
  • String return -- Both tools return the specialist's response as a string, which becomes part of the manager's observation in the ReAct loop. The manager then reasons about this result to decide the next action.

Example

The following example shows how these tools are used internally by the manager agent during hierarchical execution. Users do not call these tools directly; the manager's LLM invokes them as part of its reasoning loop.

# This is a conceptual illustration of the manager's internal tool usage.
# Users do NOT write this code -- it happens automatically during crew execution.

# Step 1: Manager receives a task:
#   "Research and write an article about quantum computing trends"

# Step 2: Manager reasons and decides to delegate research first.
# The manager's LLM generates a tool call like:
delegate_work_tool._run(
    task="Research the latest quantum computing trends in 2024",
    context=(
        "Focus on breakthroughs in error correction, major industry players, "
        "and practical applications. Include specific data points and sources."
    ),
    coworker="Senior Research Analyst",
)
# The Senior Research Analyst executes the task and returns results.

# Step 3: Manager reviews research results and asks a follow-up question.
ask_question_tool._run(
    question="What are the most commercially viable quantum computing applications right now?",
    context="Based on your research findings, I need to focus the article on practical impact.",
    coworker="Senior Research Analyst",
)
# The analyst responds with targeted information.

# Step 4: Manager delegates writing with the research context.
delegate_work_tool._run(
    task="Write a 1500-word article on quantum computing trends",
    context=(
        "Use the following research findings as the basis for the article: "
        "[research results from steps 2 and 3]. Focus on commercially viable "
        "applications and recent breakthroughs in error correction."
    ),
    coworker="Expert Content Writer",
)
# The writer produces the article.

# Step 5: Manager synthesizes the final result and completes the task.

User-Facing Setup

From the user's perspective, the setup that enables the above internal behavior is straightforward:

from crewai import Crew, Agent, Task, Process

researcher = Agent(
    role="Senior Research Analyst",
    goal="Find comprehensive information on any topic",
    backstory="Expert researcher with 15 years of analytical experience.",
    allow_delegation=False,
)

writer = Agent(
    role="Expert Content Writer",
    goal="Produce clear, engaging written content",
    backstory="Accomplished writer skilled at turning research into articles.",
    allow_delegation=False,
)

task = Task(
    description="Research and write an article about quantum computing trends.",
    expected_output="A polished 1500-word article with research-backed insights.",
)

crew = Crew(
    agents=[researcher, writer],
    tasks=[task],
    process=Process.hierarchical,
    manager_llm="openai/gpt-4o",
    verbose=True,
)

# The framework automatically:
# 1. Creates a manager agent with the specified LLM
# 2. Creates DelegateWorkTool with agents=[researcher, writer]
# 3. Creates AskQuestionTool with agents=[researcher, writer]
# 4. Adds both tools to the manager agent
# 5. During execution, the manager uses these tools to coordinate

result = crew.kickoff()

Notes

  • The DelegateWorkTool is for assigning work -- the specialist executes a task and returns a result. The AskQuestionTool is for gathering information -- the specialist answers a question based on its expertise. The manager chooses which tool to use based on what it needs.
  • If the coworker name does not match any available specialist, the tool returns an error message listing all available coworkers. The manager can then retry with a corrected name.
  • These tools contribute to the manager's token usage and iteration count. Each tool call is a reasoning step that counts toward the manager's max_iter limit.
  • In verbose mode, the tool calls and their results are logged, making it possible to observe the manager's delegation strategy in real time.

Principle:CrewAIInc_CrewAI_Managed_Execution

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment