Coding Agents
Learn how to enable your CrewAI Agents to write and execute code, and explore advanced features for enhanced functionality.
Introduction
CrewAI Agents now have the powerful ability to write and execute code, significantly enhancing their problem-solving capabilities. This feature is particularly useful for tasks that require computational or programmatic solutions.
Enabling Code Execution
To enable code execution for an agent, set the allow_code_execution
parameter to True
when creating the agent.
Here’s an example:
Note that allow_code_execution
parameter defaults to False
.
Important Considerations
-
Model Selection: It is strongly recommended to use more capable models like Claude 3.5 Sonnet and GPT-4 when enabling code execution. These models have a better understanding of programming concepts and are more likely to generate correct and efficient code.
-
Error Handling: The code execution feature includes error handling. If executed code raises an exception, the agent will receive the error message and can attempt to correct the code or provide alternative solutions. The
max_retry_limit
parameter, which defaults to 2, controls the maximum number of retries for a task. -
Dependencies: To use the code execution feature, you need to install the
crewai_tools
package. If not installed, the agent will log an info message: “Coding tools not available. Install crewai_tools.”
Code Execution Process
When an agent with code execution enabled encounters a task requiring programming:
Task Analysis
The agent analyzes the task and determines that code execution is necessary.
Code Formulation
It formulates the Python code needed to solve the problem.
Code Execution
The code is sent to the internal code execution tool (CodeInterpreterTool
).
Result Interpretation
The agent interprets the result and incorporates it into its response or uses it for further problem-solving.
Example Usage
Here’s a detailed example of creating an agent with code execution capabilities and using it in a task:
In this example, the coding_agent
can write and execute Python code to perform data analysis tasks.
Was this page helpful?