Agents
What are CrewAI Agents and how to use them.
What is an agent?
An agent is an autonomous unit programmed to:
- Perform tasks
- Make decisions
- Communicate with other agents
Think of an agent as a member of a team, with specific skills and a particular job to do. Agents can have different roles like Researcher
, Writer
, or Customer Support
, each contributing to the overall goal of the crew.
Agent attributes
Attribute | Parameter | Description |
---|---|---|
Role | role | Defines the agent’s function within the crew. It determines the kind of tasks the agent is best suited for. |
Goal | goal | The individual objective that the agent aims to achieve. It guides the agent’s decision-making process. |
Backstory | backstory | Provides context to the agent’s role and goal, enriching the interaction and collaboration dynamics. |
LLM (optional) | llm | Represents the language model that will run the agent. It dynamically fetches the model name from the OPENAI_MODEL_NAME environment variable, defaulting to “gpt-4” if not specified. |
Tools (optional) | tools | Set of capabilities or functions that the agent can use to perform tasks. Expected to be instances of custom classes compatible with the agent’s execution environment. Tools are initialized with a default value of an empty list. |
Function Calling LLM (optional) | function_calling_llm | Specifies the language model that will handle the tool calling for this agent, overriding the crew function calling LLM if passed. Default is None . |
Max Iter (optional) | max_iter | Max Iter is the maximum number of iterations the agent can perform before being forced to give its best answer. Default is 25 . |
Max RPM (optional) | max_rpm | Max RPM is the maximum number of requests per minute the agent can perform to avoid rate limits. It’s optional and can be left unspecified, with a default value of None . |
Max Execution Time (optional) | max_execution_time | Max Execution Time is the maximum execution time for an agent to execute a task. It’s optional and can be left unspecified, with a default value of None , meaning no max execution time. |
Verbose (optional) | verbose | Setting this to True configures the internal logger to provide detailed execution logs, aiding in debugging and monitoring. Default is False . |
Allow Delegation (optional) | allow_delegation | Agents can delegate tasks or questions to one another, ensuring that each task is handled by the most suitable agent. Default is False . |
Step Callback (optional) | step_callback | A function that is called after each step of the agent. This can be used to log the agent’s actions or to perform other operations. It will overwrite the crew step_callback . |
Cache (optional) | cache | Indicates if the agent should use a cache for tool usage. Default is True . |
System Template (optional) | system_template | Specifies the system format for the agent. Default is None . |
Prompt Template (optional) | prompt_template | Specifies the prompt format for the agent. Default is None . |
Response Template (optional) | response_template | Specifies the response format for the agent. Default is None . |
Allow Code Execution (optional) | allow_code_execution | Enable code execution for the agent. Default is False . |
Max Retry Limit (optional) | max_retry_limit | Maximum number of retries for an agent to execute a task when an error occurs. Default is 2 . |
Use System Prompt (optional) | use_system_prompt | Adds the ability to not use system prompt (to support o1 models). Default is True . |
Respect Context Window (optional) | respect_context_window | Summary strategy to avoid overflowing the context window. Default is True . |
Code Execution Mode (optional) | code_execution_mode | Determines the mode for code execution: ‘safe’ (using Docker) or ‘unsafe’ (direct execution on the host machine). Default is safe . |
Creating an agent
Agent interaction: Agents can interact with each other using CrewAI’s built-in delegation and communication mechanisms. This allows for dynamic task management and problem-solving within the crew.
To create an agent, you would typically initialize an instance of the Agent
class with the desired properties. Here’s a conceptual example including all attributes:
Setting prompt templates
Prompt templates are used to format the prompt for the agent. You can use to update the system, regular and response templates for the agent. Here’s an example of how to set prompt templates:
Bring your third-party agents
Extend your third-party agents like LlamaIndex, Langchain, Autogen or fully custom agents using the the CrewAI’s BaseAgent
class.
BaseAgent includes attributes and methods required to integrate with your crews to run and delegate tasks to other agents within your own crew.
CrewAI is a universal multi-agent framework that allows for all agents to work together to automate tasks and solve problems.
Conclusion
Agents are the building blocks of the CrewAI framework. By understanding how to define and interact with agents,
you can create sophisticated AI systems that leverage the power of collaborative intelligence. The code_execution_mode
attribute provides flexibility in how agents execute code, allowing for both secure and direct execution options.
Was this page helpful?