Learn how to enable your CrewAI Agents to write and execute code, and explore advanced features for enhanced functionality.
allow_code_execution
parameter to True
when creating the agent.
Here’s an example:
from crewai import Agent
coding_agent = Agent(
role="Senior Python Developer",
goal="Craft well-designed and thought-out code",
backstory="You are a senior Python developer with extensive experience in software architecture and best practices.",
allow_code_execution=True
)
allow_code_execution
parameter defaults to False
.max_retry_limit
parameter, which defaults to 2, controls the maximum number of retries for a task.
crewai_tools
package. If not installed, the agent will log an info message:
“Coding tools not available. Install crewai_tools.”
Task Analysis
Code Formulation
Code Execution
CodeInterpreterTool
).Result Interpretation
from crewai import Agent, Task, Crew
# Create an agent with code execution enabled
coding_agent = Agent(
role="Python Data Analyst",
goal="Analyze data and provide insights using Python",
backstory="You are an experienced data analyst with strong Python skills.",
allow_code_execution=True
)
# Create a task that requires code execution
data_analysis_task = Task(
description="Analyze the given dataset and calculate the average age of participants.",
agent=coding_agent
)
# Create a crew and add the task
analysis_crew = Crew(
agents=[coding_agent],
tasks=[data_analysis_task]
)
# Execute the crew
result = analysis_crew.kickoff()
print(result)
coding_agent
can write and execute Python code to perform data analysis tasks.Was this page helpful?