Bring your own agent
Learn how to bring your own agents that work within a Crew.
Interoperability is a core concept in CrewAI. This guide will show you how to bring your own agents that work within a Crew.
Adapter Guide for Bringing your own agents (Langgraph Agents, OpenAI Agents, etc…)
We require 3 adapters to turn any agent from different frameworks to work within crew.
- BaseAgentAdapter
- BaseToolAdapter
- BaseConverter
BaseAgentAdapter
This abstract class defines the common interface and functionality that all agent adapters must implement. It extends BaseAgent to maintain compatibility with the CrewAI framework while adding adapter-specific requirements.
Required Methods:
def configure_tools
def configure_structured_output
Creating your own Adapter
To integrate an agent from a different framework (e.g., LangGraph, Autogen, OpenAI Assistants) into CrewAI, you need to create a custom adapter by inheriting from BaseAgentAdapter
. This adapter acts as a compatibility layer, translating between the CrewAI interfaces and the specific requirements of your external agent.
Here’s how you implement your custom adapter:
-
Inherit from
BaseAgentAdapter
: -
Implement
__init__
: The constructor should call the parent class constructorsuper().__init__(**kwargs)
and perform any initialization specific to your external agent. You can use the optionalagent_config
dictionary passed during CrewAI’sAgent
initialization to configure your adapter and the underlying agent. -
Implement
configure_tools
: This abstract method is crucial. It receives a list of CrewAIBaseTool
instances. Your implementation must convert or adapt these tools into the format expected by your external agent framework. This might involve wrapping them, extracting specific attributes, or registering them with the external agent instance. -
Implement
configure_structured_output
: This method is called when the CrewAIAgent
is configured with structured output requirements (e.g.,output_json
oroutput_pydantic
). Your adapter needs to ensure the external agent is set up to comply with these requirements. This might involve setting specific parameters on the external agent or ensuring its underlying model supports the requested format. If the external agent doesn’t support structured output in a way compatible with CrewAI’s expectations, you might need to handle the conversion or raise an appropriate error.
By implementing these methods, your MyCustomAgentAdapter
will allow your custom agent implementation to function correctly within a CrewAI crew, interacting with tasks and tools seamlessly. Remember to replace the example comments and print statements with your actual adaptation logic specific to the external agent framework you are integrating.
BaseToolAdapter implementation
The BaseToolAdapter
class is responsible for converting CrewAI’s native BaseTool
objects into a format that your specific external agent framework can understand and utilize. Different agent frameworks (like LangGraph, OpenAI Assistants, etc.) have their own unique ways of defining and handling tools, and the BaseToolAdapter
acts as the translator.
Here’s how you implement your custom tool adapter:
-
Inherit from
BaseToolAdapter
: -
Implement
configure_tools
: This is the core abstract method you must implement. It receives a list of CrewAIBaseTool
instances provided to the agent. Your task is to iterate through this list, adapt eachBaseTool
into the format expected by your external framework, and store the converted tools in theself.converted_tools
list (which is initialized in the base class constructor). -
Using the Adapter: Typically, you would instantiate your
MyCustomToolAdapter
within yourMyCustomAgentAdapter
’sconfigure_tools
method and use it to process the tools before configuring your external agent.
By creating a BaseToolAdapter
, you decouple the tool conversion logic from the agent adaptation, making the integration cleaner and more modular. Remember to replace the placeholder examples with the actual conversion logic required by your specific external agent framework.
BaseConverter
The BaseConverterAdapter
plays a crucial role when a CrewAI Task
requires an agent to return its final output in a specific structured format, such as JSON or a Pydantic model. It bridges the gap between CrewAI’s structured output requirements and the capabilities of your external agent.
Its primary responsibilities are:
- Configuring the Agent for Structured Output: Based on the
Task
’s requirements (output_json
oroutput_pydantic
), it instructs the associatedBaseAgentAdapter
(and indirectly, the external agent) on what format is expected. - Enhancing the System Prompt: It modifies the agent’s system prompt to include clear instructions on how to generate the output in the required structure.
- Post-processing the Result: It takes the raw output from the agent and attempts to parse, validate, and format it according to the required structure, ultimately returning a string representation (e.g., a JSON string).
Here’s how you implement your custom converter adapter:
-
Inherit from
BaseConverterAdapter
: -
Implement
__init__
: The constructor must accept the correspondingagent_adapter
instance it will work with. -
Implement
configure_structured_output
: This method receives the CrewAITask
object. You need to check the task’soutput_json
andoutput_pydantic
attributes to determine the required output structure. Store this information (e.g., in_output_type
and_output_schema
) and potentially call configuration methods on yourself.agent_adapter
if the external agent needs specific setup for structured output (which might have been partially handled in the agent adapter’sconfigure_structured_output
already). -
Implement
enhance_system_prompt
: This method takes the agent’s base system prompt string and should append instructions tailored to the currently configured_output_type
and_output_schema
. The goal is to guide the LLM powering the agent to produce output in the correct format.Note: The exact prompt engineering might need tuning based on the agent/LLM being used.
-
Implement
post_process_result
: This method receives the raw string output from the agent. If structured output was requested (json
orpydantic
), you should attempt to parse the string into the expected format. Handle potential parsing errors (e.g., log them, attempt simple fixes, or raise an exception). Crucially, the method must always return a string, even if the intermediate format was a dictionary or Pydantic object (e.g., by serializing it back to a JSON string).
By implementing these methods, your MyCustomConverterAdapter
ensures that structured output requests from CrewAI tasks are correctly handled by your integrated external agent, improving the reliability and usability of your custom agent within the CrewAI framework.
Out of the Box Adapters
We provide out of the box adapters for the following frameworks:
- LangGraph
- OpenAI Agents