Tutorial: Building a Research Agent with CrewAI
Let’s build a practical agent that researches a topic and produces a comprehensive report.
Step 1: Install Dependencies
pip install crewai crewai-tools langchain-anthropic
Step 2: Define Your Agents
from crewai import Agent, Task, Crew, Process
from crewai_tools import SerperDevTool, WebsiteSearchTool
search_tool = SerperDevTool()
web_tool = WebsiteSearchTool()
researcher = Agent(
role="Senior Research Analyst",
goal="Find comprehensive, accurate information about {topic}",
backstory="You're an expert researcher who finds the most "
"relevant and current information on any topic.",
tools=[search_tool, web_tool],
verbose=True
)
writer = Agent(
role="Content Writer",
goal="Write a clear, engaging report based on research",
backstory="You're a skilled writer who transforms complex "
"research into readable, actionable content.",
verbose=True
)
Step 3: Define Tasks
research_task = Task(
description="Research {topic}. Find at least 5 reliable "
"sources. Identify key trends, data points, "
"and expert opinions.",
expected_output="A comprehensive research brief with "
"key findings, data points, and source URLs.",
agent=researcher
)
writing_task = Task(
description="Write a 1500-word report on {topic} based "
"on the research provided. Include sections: "
"Overview, Key Findings, Analysis, and "
"Recommendations.",
expected_output="A polished, well-structured report "
"ready for publication.",
agent=writer
)
Step 4: Assemble and Run the Crew
crew = Crew(
agents=[researcher, writer],
tasks=[research_task, writing_task],
process=Process.sequential,
verbose=True
)
result = crew.kickoff(
inputs={"topic": "AI adoption in Indian healthcare 2026"}
)
print(result)
When you run this, the researcher agent will search the web, read articles, and compile findings. Then the writer agent will use those findings to produce a polished report. All automated.
Tutorial: Building a LangChain Agent
For more control, here’s how to build an agent with LangChain directly.
Setup
from langchain_anthropic import ChatAnthropic
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain.tools import tool
from langchain_core.prompts import ChatPromptTemplate
llm = ChatAnthropic(model="claude-sonnet-4-20250514")
@tool
def calculate(expression: str) -> str:
"""Evaluate a mathematical expression."""
return str(eval(expression))
@tool
def search_database(query: str) -> str:
"""Search the product database."""
# Your database query logic here
return f"Results for: {query}"
tools = [calculate, search_database]
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful business analyst."),
("human", "{input}"),
("placeholder", "{agent_scratchpad}")
])
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)
result = executor.invoke({
"input": "Calculate our profit margin if revenue is "
"5000000 and costs are 3750000, then search "
"for similar companies in our database."
})
The agent will automatically decide when to use the calculator vs. the database, chain the results together, and provide a coherent answer.
Best Practices for Agent Development
1. Start Simple
Build agents with 1-2 tools first. Add complexity only when the simple version works reliably.
2. Define Clear Boundaries
Tell agents what they should NOT do, not just what they should do. Constraints prevent expensive mistakes.
3. Add Human-in-the-Loop
For any agent that takes irreversible actions (sending emails, making purchases, modifying data), add a confirmation step.
4. Log Everything
Agent debugging is hard without logs. Record every decision, tool call, and intermediate result.
5. Handle Failures Gracefully
Tools fail. APIs go down. Agents need fallback strategies for when things go wrong.
People Also Ask
Do I need to know Python to build AI agents?
For LangChain and CrewAI, yes. However, no-code platforms like n8n and Make.com offer visual agent builders that require no coding. The tradeoff is less flexibility.
How much does it cost to run AI agents?
Costs depend on the LLM used and the number of tool calls. A typical research agent using Claude Sonnet makes 5-15 API calls per task, costing $0.05-$0.30. Using cheaper models or open-source alternatives reduces costs further.
Can AI agents replace employees?
Agents excel at well-defined, repetitive tasks. They’re poor at tasks requiring judgment, empathy, or creative problem-solving. The best approach is using agents to handle the routine work so humans can focus on the high-value tasks.
Start Building Your First Agent
The best way to learn is to build. Pick a repetitive task in your workflow, design an agent for it, and iterate. Within a few hours, you’ll have an autonomous system handling work that used to take you hours.
Want to skip months of trial and error? We’ve distilled thousands of hours of prompt engineering into ready-to-use prompt packs that deliver results on day one. Our packs at wowhow.cloud include battle-tested prompts for marketing, coding, business, writing, and more — each one refined until it consistently produces professional-grade output.
Blog reader exclusive: Use code BLOGREADER20 for 20% off your entire cart. No minimum, no catch.
Browse Prompt Packs →
Comments · 0
No comments yet. Be the first to share your thoughts.