Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:CrewAIInc CrewAI Agent Constructor

From Leeroopedia
Knowledge Sources
Domains Multi_Agent_Systems, NLP
Last Updated 2026-02-11 00:00 GMT

Overview

Concrete class for defining AI agents with role-based identity, language model binding, and tool capabilities provided by the CrewAI framework.

Description

The Agent class extends BaseAgent and represents a fully configured AI agent. It accepts a role, goal, and backstory that form the agent's persona, plus optional parameters for the language model, tools, delegation permissions, memory, code execution, reasoning mode, guardrails, and MCP server connections. On instantiation, Pydantic validation ensures all fields are consistent.

Usage

Import and instantiate Agent when defining any participant in a crew. Pass the required role, goal, and backstory strings, and optionally configure the LLM, tools, and behavioral parameters. The resulting Agent instance is then passed to a Crew.

Code Reference

Source Location

  • Repository: crewAI
  • File: lib/crewai/src/crewai/agent/core.py
  • Lines: L128-267

Signature

class Agent(BaseAgent):
    """Represents an agent in a system."""

    llm: str | InstanceOf[BaseLLM] | Any = Field(default=None)
    function_calling_llm: str | InstanceOf[BaseLLM] | Any | None = Field(default=None)
    max_execution_time: int | None = Field(default=None)
    step_callback: Any | None = Field(default=None)
    use_system_prompt: bool | None = Field(default=True)
    system_template: str | None = Field(default=None)
    prompt_template: str | None = Field(default=None)
    response_template: str | None = Field(default=None)
    allow_code_execution: bool | None = Field(default=False)
    respect_context_window: bool = Field(default=True)
    max_retry_limit: int = Field(default=2)
    inject_date: bool = Field(default=False)
    code_execution_mode: Literal["safe", "unsafe"] = Field(default="safe")
    reasoning: bool = Field(default=False)
    max_reasoning_attempts: int | None = Field(default=None)
    embedder: EmbedderConfig | None = Field(default=None)
    guardrail: GuardrailType | None = Field(default=None)
    guardrail_max_retries: int = Field(default=3)
    a2a: list[A2AConfig] | A2AConfig | None = Field(default=None)

Inherited from BaseAgent:

    role: str  # Required
    goal: str  # Required
    backstory: str  # Required
    tools: list[BaseTool] | None = None
    max_iter: int = 15
    verbose: bool = False
    allow_delegation: bool = False
    knowledge_sources: list[BaseKnowledgeSource] | None = None

Import

from crewai import Agent

I/O Contract

Inputs

Name Type Required Description
role str Yes Agent's role description (e.g., "Senior Researcher")
goal str Yes Agent's objective
backstory str Yes Background context shaping agent behavior
llm BaseLLM | Any No Language model identifier or instance (default: env-configured)
tools None No Tools available to the agent
max_iter int No Maximum reasoning iterations (default: 15)
verbose bool No Enable verbose logging (default: False)
allow_delegation bool No Allow delegating tasks to other agents (default: False)

Outputs

Name Type Description
Agent instance Agent Configured agent ready for crew assignment

Usage Examples

Basic Agent

from crewai import Agent

researcher = Agent(
    role="Senior Research Analyst",
    goal="Find and synthesize the latest AI research papers",
    backstory="You are an experienced research analyst with expertise in AI.",
    llm="openai/gpt-4o",
    verbose=True,
)

Agent with Tools

from crewai import Agent
from crewai_tools import SerperDevTool, ScrapeWebsiteTool

researcher = Agent(
    role="Web Researcher",
    goal="Find comprehensive information about the given topic",
    backstory="You are a skilled web researcher.",
    llm="openai/gpt-4o",
    tools=[SerperDevTool(), ScrapeWebsiteTool()],
    max_iter=25,
    allow_delegation=False,
)

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment