Agents

An Agent is an autonomous unit in CrewAI: it performs tasks, uses tools, may delegate when allowed, and can keep memory. Think of a specialist on a team — a researcher gathers, a writer drafts.

CLI projects can use agents/<name>.jsonc. This chapter uses Python so it matches the official attribute table.


The required trio

ParameterRole
roleFunction and expertise on the team
goalThe personal objective that guides decisions
backstoryPersonality and context; it shapes tone and trade-offs

Common optionals: tools, llm, verbose, allow_delegation, max_iter (default 20), memory, knowledge_sources.

from crewai import Agent
from crewai_tools import SerperDevTool

researcher = Agent(
    role="Research Analyst",
    goal="Find and summarize information about specific topics",
    backstory="You are an experienced researcher with attention to detail.",
    llm="openai/gpt-4o-mini",
    tools=[SerperDevTool()],
    verbose=True,
    allow_delegation=False,
    max_iter=20,
)

llm may be a provider/model-id string, an LLM(...) instance, or omitted (falls back to OPENAI_MODEL_NAME / the default model). Tool-calling can use a separate function_calling_llm (a cheaper model that only picks tools).


JSONC (CLI projects)

agents/researcher.jsonc:

{
  "role": "{topic} Senior Data Researcher",
  "goal": "Uncover cutting-edge developments in {topic}",
  "backstory": "You find the most relevant information and present it clearly.",
  "llm": "openai/gpt-4o-mini",
  "tools": ["SerperDevTool"],
  "settings": {
    "verbose": true,
    "allow_delegation": false,
    "max_iter": 20
  }
}

"agents": ["researcher"] in crew.jsonc loads that file. {topic} comes from crew-level inputs.


Other switches

ParameterMeaning
allow_code_executionRun code; code_execution_mode is "safe" (Docker) or "unsafe"
respect_context_windowSummarize when over the window (default True)
reasoningPlan before executing; optional max_reasoning_attempts
inject_dateInject today’s date; date_format defaults to "%Y-%m-%d"
multimodalNon-text inputs
max_rpm / max_execution_timeRate limit and timeout

Write specific roles: weak role="assistant"; strong role="Market analyst for executives; cite only verifiable sources". Make goal a completable outcome, not “do your best”.


Without a Crew: kickoff()

from pydantic import BaseModel
from typing import List
from crewai import Agent

class Findings(BaseModel):
    main_points: List[str]
    risks: List[str]

agent = Agent(
    role="Briefing writer",
    goal="Return structured findings",
    backstory="You never invent citations.",
)
result = agent.kickoff(
    "Summarize CrewAI Flows vs Crews in 2026 terms.",
    response_format=Findings,
)
print(result.raw)
print(result.pydantic.main_points)

messages can be a string or a list of {role, content} dicts. The return type is LiteAgentOutput (raw, pydantic, usage_metrics, …). Full pipelines still belong on Task + Crew.


vs LangChain agents

LangChain’s create_agent is a single-loop harness (model + tools + middleware). CrewAI’s Agent is a role card; real multi-role work happens on a Crew. Use LangGraph when you need graph checkpoints; use CrewAI when you need role-play teams. See LangChain.


Next steps

评论