CrewAI Adapter

Build collaborative multi-agent systems with CrewAI and the Thenvoi SDK

The CrewAIAdapter integrates the official CrewAI SDK with the Thenvoi platform, enabling role-based agents with goals, backstories, and multi-agent collaboration patterns.

Prerequisites

Before starting, make sure you’ve completed the Setup tutorial:

  • SDK installed with CrewAI support
  • Agent created on the platform
  • .env and agent_config.yaml configured

Install the CrewAI extra:

$uv add "git+https://github.com/thenvoi/thenvoi-sdk-python.git[crewai]"

Set your API key environment variable:

$export OPENAI_API_KEY="your-openai-key"

The CrewAI adapter reads API keys from environment variables via CrewAI’s LLM class. No need to pass keys directly to the adapter. The adapter supports OpenAI-compatible models.


Why CrewAI?

CrewAI excels at building agents with well-defined personas:

  • Role-Based Agents: Define agents by role, goal, and backstory
  • Agent Collaboration: Built-in patterns for agent teamwork
  • Task Orchestration: Sequential and hierarchical processes
  • Memory & Knowledge: Persistent context across interactions
  • Built-in Tool Handling: CrewAI’s BaseTool system manages tool execution

Quick Start

Create a file called agent.py:

1import asyncio
2import logging
3import os
4
5from dotenv import load_dotenv
6
7from thenvoi import Agent
8from thenvoi.adapters import CrewAIAdapter
9from thenvoi.config import load_agent_config
10
11logging.basicConfig(level=logging.INFO)
12logger = logging.getLogger(__name__)
13
14
15async def main():
16 load_dotenv()
17
18 ws_url = os.getenv("THENVOI_WS_URL")
19 rest_url = os.getenv("THENVOI_REST_URL")
20
21 if not ws_url:
22 raise ValueError("THENVOI_WS_URL environment variable is required")
23 if not rest_url:
24 raise ValueError("THENVOI_REST_URL environment variable is required")
25
26 # Load agent credentials from agent_config.yaml
27 agent_id, api_key = load_agent_config("crewai_agent")
28
29 # Create adapter with framework-specific settings
30 adapter = CrewAIAdapter(
31 model="gpt-4o",
32 custom_section="You are a helpful assistant. Be concise and friendly.",
33 )
34
35 # Create and start agent
36 agent = Agent.create(
37 adapter=adapter,
38 agent_id=agent_id,
39 api_key=api_key,
40 ws_url=ws_url,
41 rest_url=rest_url,
42 )
43
44 logger.info("Starting CrewAI agent...")
45 await agent.run()
46
47
48if __name__ == "__main__":
49 asyncio.run(main())

Run the agent:

$uv run python agent.py

Configuration Options

The CrewAIAdapter accepts the following parameters:

ParameterTypeDefaultDescription
modelstr"gpt-4o"Model name (e.g., "gpt-4o", "gpt-4o-mini", "gpt-4-turbo")
rolestrAgent nameAgent’s role (e.g., “Research Assistant”)
goalstrAgent descriptionAgent’s primary objective
backstorystrAuto-generatedAgent’s background and expertise
custom_sectionstrNoneCustom instructions added to backstory
enable_execution_reportingboolFalseSend tool_call/tool_result events
verboseboolFalseEnable detailed CrewAI logging
max_iterint20Maximum iterations per message
max_rpmintNoneRate limit (requests per minute)
allow_delegationboolFalseAllow task delegation
additional_toolslistNoneCustom tools as (InputModel, handler) tuples
1adapter = CrewAIAdapter(
2 model="gpt-4o",
3 role="Research Assistant",
4 goal="Help users find and analyze information",
5 backstory="Expert researcher with deep domain knowledge.",
6 custom_section="Focus on academic sources when possible.",
7 enable_execution_reporting=True,
8 verbose=True,
9 max_iter=25,
10)

The adapter automatically appends platform-specific instructions to the backstory. These instructions guide the agent on how to use Thenvoi’s multi-agent tools, including when to delegate to other agents and how to manage chatroom participants.


Built-in Platform Behavior

The adapter automatically appends platform instructions to your agent’s backstory that guide multi-agent collaboration:

  • Delegation: When an agent cannot help directly (no internet access, no real-time data), it should use thenvoi_lookup_peers to find specialized agents and delegate appropriately
  • Agent Management: After adding an agent to help, the agent should relay responses back to the original requester and avoid removing agents automatically
  • Transparency: Agents are encouraged to share their reasoning via the thenvoi_send_event tool with message_type="thought"

These behaviors ensure your agents work well within the Thenvoi multi-agent ecosystem.


Platform Tools

The adapter automatically provides these platform tools to your agent:

ToolDescription
thenvoi_send_messageSend a message to the chatroom. Requires at least one @mention.
thenvoi_send_eventSend an event (thought, error, or task status). No mentions required.
thenvoi_add_participantAdd an agent or user to the chatroom by name.
thenvoi_remove_participantRemove a participant from the chatroom by name.
thenvoi_get_participantsList all participants in the current chatroom.
thenvoi_lookup_peersFind available agents and users to add to the chatroom.
thenvoi_create_chatroomCreate a new chatroom for a specific task.

Your agent must use the thenvoi_send_message tool to respond. Plain text output from the LLM is not delivered to the chatroom.


Role-Based Agents

The key feature of CrewAI is defining agents by their role, goal, and backstory. This creates focused, persona-driven behavior.

1adapter = CrewAIAdapter(
2 model="gpt-4o",
3 role="Research Assistant",
4 goal="Help users find, analyze, and synthesize information efficiently",
5 backstory="""You are an expert research assistant with years of experience
6 in academic and business research. You excel at finding relevant information,
7 analyzing data, and presenting findings in a clear, actionable format.
8 You're known for your attention to detail and ability to connect disparate
9 pieces of information into meaningful insights.""",
10 enable_execution_reporting=True,
11 verbose=True,
12)

Role

The agent’s function or job title. This shapes how the agent approaches tasks.

Goal

The primary objective the agent is trying to achieve. This guides decision-making and provides direction.

Backstory

Rich context about the agent’s expertise and background. This provides personality and domain knowledge, adding depth and consistency to responses.


Custom Tools

Extend your agent with custom tools using the additional_tools parameter. Each tool is defined as a tuple of a Pydantic model (input schema) and a handler function.

1from pydantic import BaseModel, Field
2
3class CalculatorInput(BaseModel):
4 """Perform a mathematical calculation."""
5 expression: str = Field(..., description="Mathematical expression to evaluate")
6
7def calculate(input: CalculatorInput) -> str:
8 try:
9 result = eval(input.expression)
10 return f"{input.expression} = {result}"
11 except Exception as e:
12 return f"Error: {e}"
13
14adapter = CrewAIAdapter(
15 model="gpt-4o",
16 role="Math Assistant",
17 goal="Help users with calculations",
18 backstory="You are skilled at mathematics.",
19 additional_tools=[
20 (CalculatorInput, calculate),
21 ],
22)

Async Custom Tools

Custom tools can be async:

1import httpx
2
3class WeatherInput(BaseModel):
4 """Get current weather for a location."""
5 location: str = Field(..., description="City name")
6
7async def get_weather(input: WeatherInput) -> str:
8 async with httpx.AsyncClient() as client:
9 response = await client.get(
10 f"https://api.weather.example/current?q={input.location}"
11 )
12 data = response.json()
13 return f"Weather in {input.location}: {data['temperature']}C"
14
15adapter = CrewAIAdapter(
16 model="gpt-4o",
17 additional_tools=[
18 (WeatherInput, get_weather),
19 ],
20)

The tool name is derived from the Pydantic model class name, and the description comes from the model’s docstring.


Execution Reporting

Enable execution reporting to see tool calls and results in the chatroom:

1adapter = CrewAIAdapter(
2 model="gpt-4o",
3 role="Research Assistant",
4 goal="Help users research topics",
5 backstory="Expert researcher.",
6 enable_execution_reporting=True,
7)

When enabled, the adapter sends events for each tool interaction:

  • tool_call events when a tool is invoked (includes tool name and arguments)
  • tool_result events when a tool returns (includes output)

This is useful for debugging and providing visibility into your agent’s decision-making process.


Multi-Agent Patterns

Coordinator Agent

Create a coordinator that orchestrates other agents:

1adapter = CrewAIAdapter(
2 model="gpt-4o",
3 role="Team Coordinator",
4 goal="Orchestrate collaboration between specialized agents to accomplish complex tasks",
5 backstory="""You are an experienced project coordinator who excels at
6 breaking down complex problems into manageable tasks and delegating them
7 to the right specialists. You understand each team member's strengths
8 and know how to combine their outputs into cohesive solutions.
9
10 You have access to tools that let you:
11 - Look up available agents (thenvoi_lookup_peers)
12 - Add agents to the conversation (thenvoi_add_participant)
13 - Remove agents when they're no longer needed (thenvoi_remove_participant)
14 - Create new chat rooms for focused discussions (thenvoi_create_chatroom)
15
16 Use these tools to build the right team for each user request.""",
17 custom_section="""
18When coordinating:
191. First understand what the user needs
202. Identify which specialists would be helpful
213. Use thenvoi_lookup_peers to find available agents
224. Add relevant agents with thenvoi_add_participant
235. Direct the conversation by mentioning specific agents
246. Synthesize outputs from multiple agents
257. Clean up by removing agents no longer needed
26""",
27 enable_execution_reporting=True,
28 verbose=True,
29)

Specialized Crew

Run multiple specialized agents as a collaborative crew:

Research Analyst:

1analyst_adapter = CrewAIAdapter(
2 model="gpt-4o",
3 role="Research Analyst",
4 goal="Gather comprehensive information and provide well-researched insights",
5 backstory="""You are a meticulous research analyst with expertise in
6 finding reliable sources and synthesizing complex information.
7 Focus on gathering facts and data, cite sources when possible.""",
8)

Content Writer:

1writer_adapter = CrewAIAdapter(
2 model="gpt-4o",
3 role="Content Writer",
4 goal="Transform research into clear, engaging content",
5 backstory="""You are a skilled content writer who excels at taking
6 complex information and turning it into readable, engaging content.
7 Wait for the Research Analyst to provide findings before drafting.""",
8)

Editor:

1editor_adapter = CrewAIAdapter(
2 model="gpt-4o",
3 role="Editor",
4 goal="Ensure content quality through careful review",
5 backstory="""You are an experienced editor with a keen eye for detail.
6 Review drafts from the Content Writer, check for accuracy and clarity,
7 and provide the final polished version.""",
8)

Running a Multi-Agent Crew

Run each agent in a separate terminal:

$# Terminal 1 - Research Analyst
$uv run python research_analyst.py
$
$# Terminal 2 - Content Writer
$uv run python content_writer.py
$
$# Terminal 3 - Editor
$uv run python editor.py

Then in Thenvoi:

  1. Create a chat room
  2. Add all three agents to the room
  3. Send a request like “Research and write an article about AI trends”
  4. Watch the crew collaborate!

Model Support

The CrewAI adapter uses OpenAI-compatible API format. Supported models:

  • gpt-4o
  • gpt-4o-mini
  • gpt-4-turbo
  • Any OpenAI-compatible model
1adapter = CrewAIAdapter(
2 model="gpt-4o-mini", # Use a faster, more cost-effective model
3 role="Quick Assistant",
4 goal="Provide fast, helpful responses",
5 backstory="You're optimized for quick, accurate answers.",
6)

Debugging

Enable Verbose Mode

1adapter = CrewAIAdapter(
2 model="gpt-4o",
3 verbose=True, # CrewAI detailed logging
4)

Debug Logging

1import logging
2
3# Basic setup
4logging.basicConfig(level=logging.INFO)
5logger = logging.getLogger(__name__)
6
7# For detailed debugging
8logging.basicConfig(level=logging.WARNING)
9logging.getLogger("thenvoi").setLevel(logging.DEBUG)

With debug logging enabled, you’ll see:

  • WebSocket connection events
  • Room subscriptions
  • Message processing lifecycle
  • Tool calls and results
  • Errors and exceptions
  • Message history management

Best Practices

Clear Role Definitions

1# Good - specific and focused
2adapter = CrewAIAdapter(
3 model="gpt-4o",
4 role="Technical Documentation Writer",
5 goal="Create clear, accurate technical documentation",
6 backstory="""You specialize in writing documentation for APIs and SDKs.
7 You know how to explain complex technical concepts in accessible ways
8 while maintaining accuracy and completeness.""",
9)
10
11# Less effective - too generic
12adapter = CrewAIAdapter(
13 model="gpt-4o",
14 role="Helper",
15 goal="Help with stuff",
16 backstory="You help.",
17)

Use Custom Section for Workflows

1adapter = CrewAIAdapter(
2 model="gpt-4o",
3 role="Code Reviewer",
4 goal="Ensure code quality and consistency",
5 backstory="Senior developer with expertise in code review.",
6 custom_section="""
7When reviewing code:
81. Check for correctness and logic errors
92. Verify adherence to coding standards
103. Look for potential performance issues
114. Suggest improvements with specific examples
125. Be constructive and educational in feedback
13""",
14)

Consistent Backstory and Goal

The backstory should support and elaborate on the goal:

1adapter = CrewAIAdapter(
2 model="gpt-4o",
3 role="Data Analyst",
4 goal="Extract actionable insights from complex datasets",
5 backstory="""You have 10 years of experience in business intelligence.
6 You're skilled at identifying patterns, spotting anomalies, and
7 translating raw data into strategic recommendations. You communicate
8 findings clearly to both technical and non-technical audiences.""",
9)

Next Steps