Skip to content

The Specialists: Mastering Agents

Agents are the brains of your operation. In CrewAI, an Agent is an autonomous unit that can perform tasks, make decisions, and communicate with other agents. In this chapter, we will dive deep into how to craft effective agents.

The quality of an agent’s output is directly tied to how well you define its attributes. There are three critical pillars:

  • Role: This is the agent’s job title. It tells the LLM what perspective to take (e.g., “Financial Analyst” vs. “Creative Writer”).
  • Goal: This defines success for the agent. It directs the LLM’s focus (e.g., “Minimize tax liability” vs. “Write a viral poem”).
  • Backstory: This is often the most overlooked but powerful attribute. It provides context, personality, and constraints. A backstory creates the “flavor” of the response.

Here is an example of Python definition (if you weren’t using YAML):

from crewai import Agent
analyst = Agent(
role='Senior QA Engineer',
goal='Find all bugs in the code',
backstory=(
"You have 20 years of experience in software testing. "
"You are extremely detail-oriented and pessimistic about "
"code quality. You never assume code works."
),
verbose=True # Enables logging of the agent's thought process
)

Not all agents need the most expensive model. You might want a “Manager” agent using GPT-4o for complex reasoning, but a “Summarizer” agent using GPT-4o-mini to save costs. CrewAI allows you to assign specific LLMs to specific agents.

CrewAI integrates with LiteLLM, supporting over 100+ providers.

By default, CrewAI looks for the OPENAI_API_KEY environment variable and uses GPT-4.

To run agents locally (for privacy or cost), you can use Ollama. You must change the OPENAI_API_BASE and model name.

from crewai import Agent, LLM
# Configure the LLM
local_llm = LLM(
model="ollama/llama3.1",
base_url="http://localhost:11434"
)
local_agent = Agent(
role='Local Assistant',
goal='Answer quickly',
backstory='You are running locally.',
llm=local_llm
)

Beyond the persona, there are technical settings to fine-tune behavior:

  • allow_delegation: (Default: False) If set to True, the agent can hand off work to others. Note: This generally requires a more powerful model.
  • max_iter: Limits the number of “thought loops” an agent can perform before forcing an answer, preventing infinite loops.
  • verbose: Essential for debugging. It shows you the “Thought”, “Action”, and “Observation” loop of the agent.