Playbook: How to Create an AI Agent

A practical, no-nonsense guide to building AI agents using CrewAI, from basic architecture to production-ready workflows with tools, memory, and quality control.

Building AI Agents: The No-Nonsense Guide

Forget the hype. As developers, we are moving from Traditional Software Development (deterministic inputs, if/else transformations, exact outputs) to AI Software Development (fuzzy inputs, probabilistic reasoning, fuzzy outputs).

In this paradigm, you stop writing the edge cases. Instead, you put the options on the table and let the system reason through them.

To do this effectively, you need Agents. An Agent is simply a loop around an LLM prompt that iterates until it is self-satisfied with the result.

Here is the architecture of an Agentic workflow, specifically using the CrewAI framework (though the logic applies everywhere).


🏆 Gold Standard Example

The Difference Between Generic and Great

Generic (Don't do this)

role: "Writer"
goal: "Write good content"
backstory: "You are a writer who creates content for websites."

Gold Standard (Do this)

role: "B2B Technology Content Strategist"
goal: "Create compelling, technically accurate content that explains complex topics in accessible language while driving reader engagement and supporting business objectives"
backstory: "You have spent a decade creating content for leading technology companies, specializing in translating technical concepts for business audiences. You excel at research, interviewing subject matter experts, and structuring information for maximum clarity and impact. You believe that the best B2B content educates first and sells second, building trust through genuine expertise rather than marketing hype."

Rules:

  • Specific Role: Not just "Writer" but "B2B Technology Content Strategist"
  • Measurable Goal: Clear success criteria (technical accuracy + accessibility + engagement)
  • Rich Backstory: Provides context, expertise, and philosophy that guides decision-making

Task Design: Aligned Description and Expected Output

Misaligned (Don't do this)

analysis_task:
  description: "Analyze customer feedback to find areas of improvement."
  expected_output: "A marketing plan for the next quarter."

Gold Standard (Do this)

analysis_task:
  description: "Analyze customer feedback to identify the top 3 areas for product improvement."
  expected_output: "A report listing the 3 priority improvement areas with supporting customer quotes and data points."

Rules:

  • Aligned Goals: Description and expected output must match
  • Specific Scope: "Top 3 areas" not vague "areas of improvement"
  • Clear Deliverable: Exact format specified (report with quotes and data)

1. The Agent (The Brain)

Don't build one "Super Agent." Build a team of specialized hires. Think like a manager: Who do I need to hire to get this job done?

Agents perform significantly better when Role Playing. You must define:

ParameterDescriptionExample
RoleThe job title"Senior Copywriter"
GoalWhat they are trying to achieve"Uncover cutting-edge developments"
BackstoryContext to steer personality and focus"You are a veteran analyst..."

The Golden Rule: Keep agents granular. One agent, one specific focus. Too much context leads to hallucinations.

from crewai import Agent

# The "Professional"
researcher = Agent(
    role='Senior Research Analyst',
    goal='Uncover cutting-edge developments in AI Agents',
    backstory="""You are a veteran analyst at a top tech firm. 
    You dig deep into news and documentation to find truth, 
    ignoring marketing fluff.""",
    verbose=True,
    allow_delegation=False,
)

2. The Task (The Assignment)

An agent without a task is just a chatbot. A task needs a clear Description and a concise Expected Output.

Key Relationship: One agent can handle multiple tasks, but each task is assigned to exactly one agent. Think of it like a team member who can work on several projects.

In CrewAI, tasks also handle the Hyperparameters of execution (Async, Callbacks, Output formats).

from crewai import Task

# The Assignment
research_task = Task(
    description="Analyze the top 3 frameworks for AI Agents in 2024.",
    expected_output="A bulleted list of pros and cons for each framework.",
    agent=researcher,
    output_file="research_report.md"
)

3. The Tools (The Hands)

An LLM is a brain in a jar. Tools give it hands to interact with the external world (APIs, DBs, Scrapers).

Key Tool Concepts:

ConceptPurposeImpact
VersatilityConvert fuzzy LLM inputs to typed outputs (JSON/String)Ensures reliability
Fault ToleranceHandle tool crashes gracefullyPrevents app crashes
CachingReuse identical queriesCritical for scale

Assignment Strategy:

Agent Level: The agent carries these tools for any task. Task Level: Specific tools for a specific task (Overrides agent tools).

from crewai_tools import SerperDevTool

search_tool = SerperDevTool()

# Assigning at the Task Level (Recommended for focus)
research_task.tools = [search_tool]

The "No Bullshit" Example: Structured Content Crew

Here is a complete workflow. We have a Researcher and a Writer. Note the use of output_pydantic. This forces the fuzzy AI to return a structured object we can use programmatically.

# Task 2: Write (Waits for research, outputs Pydantic object)
write_task = Task(
    description='Write a blog post about {topic} using the research context.',
    expected_output='A structured blog post object.',
    agent=writer,
    context=[research_task],
    output_pydantic=BlogPost
)

# 4. The Crew (The Team Manager)
crew = Crew(
    agents=[researcher, writer],
    tasks=[research_task, write_task],
    process=Process.sequential,
    verbose=True
)

# 5. Execution
result = crew.kickoff(inputs={'topic': 'AI Agent Frameworks'})

# Accessing the structured output
print(f"Title: {result.pydantic.title}")
print(f"Keywords: {result.pydantic.seo_keywords}")

Bells and Whistles (Advanced Features)

Once you have the basics, you add these layers to make the system production-ready.

1. Memory (The "Brain" Upgrade)

By setting memory=True in your Crew config, you enable three types of persistence:

Memory TypePurposeUse Case
Short-termContext retention during current runAgents share findings
Long-termLocal DB storage across executionsSelf-improvement
Entity MemoryRemember people, orgs, productsRelationship tracking

When to use: Complex, multi-step workflows
When to skip: Simple, one-off tasks where you want a fresh state every time

2. Guardrails & Quality Control

In AI Engineering, you cannot control the transformation, but you can control the quality.

QA Agents are essential for production systems. They dramatically improve accuracy and reliability by catching errors before they reach users.

The Pattern:

  • Worker Agent: Writes the code/text
  • QA Agent: Reviews output against specific criteria. If it fails, sends it back for revision

Why This Matters:

  • LLMs are probabilistic - they will occasionally produce incorrect or incomplete outputs
  • A QA agent acts as a safety net, ensuring quality standards are met
  • This pattern reduces hallucinations and improves consistency by 40-60% in production systems

3. Async & Callbacks

Don't block the main thread if you don't have to.

async_execution=True: Run tasks in parallel (e.g., Research Agent and Outline Agent working simultaneously). callback=my_func: Trigger a function immediately after a task finishes (e.g., Save to DB, Send Slack notification).

4. Observability

If your agent loops forever, you need to know why. Enable tracing to see the chain of thought:

Set tracing=True in code.
Or run crewai traces enable in your CLI.

5. Custom Tools

Don't just use search tools. Build tools that connect to your business logic:

  • LoadCustomerDataTool: CRM integration
  • CheckBugReportsTool: Jira integration
  • TapPreviousConversationsTool: RAG

Final Thought

The goal is Agentic Automation. You aren't writing the code to do the work; you are designing the team that does the work for you.