CrewAI Tutorial (Python): adding human-in-the-loop for beginners
This tutorial shows you how to add a human approval step to a CrewAI workflow in Python. You need this when an agent is about to do something risky, like send an email, approve a refund, or write to a production system, and you want a person to review the output first.
What You'll Need
- •Python 3.10 or newer
- •
crewaiinstalled - •
python-dotenvinstalled - •An LLM API key for CrewAI, such as:
- •
OPENAI_API_KEY - •or another provider supported by your CrewAI setup
- •
- •A terminal and a code editor
- •Basic familiarity with:
- •
Agent - •
Task - •
Crew
- •
Step-by-Step
- •Start by installing the packages and setting your API key. CrewAI will use your model provider through environment variables, so keep secrets out of your code.
pip install crewai python-dotenv
export OPENAI_API_KEY="your-api-key-here"
- •Create a small CrewAI setup with one agent and one task. The agent will draft content, but we will not let it act automatically until a human reviews the result.
from crewai import Agent, Task, Crew, Process
writer = Agent(
role="Email Writer",
goal="Draft a concise customer support email",
backstory="You write clear support responses for banking customers.",
verbose=True,
)
task = Task(
description=(
"Draft an email apologizing for a delayed card replacement "
"and explain that the new card will arrive in 3 business days."
),
expected_output="A professional customer support email draft.",
agent=writer,
)
crew = Crew(
agents=[writer],
tasks=[task],
process=Process.sequential,
verbose=True,
)
- •Run the crew and capture the output as a draft. At this point, the agent has produced text, but nothing has been approved yet.
result = crew.kickoff()
draft_email = str(result)
print("\n--- DRAFT EMAIL ---\n")
print(draft_email)
- •Add a human-in-the-loop approval gate before any downstream action. The simplest pattern is to pause execution, show the draft to a person, and require an explicit yes/no decision.
def get_human_approval(text: str) -> bool:
print("\n--- REVIEW REQUIRED ---\n")
print(text)
decision = input("\nApprove this output? Type 'yes' to continue: ").strip().lower()
return decision == "yes"
if get_human_approval(draft_email):
print("\nApproved. Sending email now...")
else:
print("\nRejected. No action taken.")
- •Put the approval gate in front of any real side effect. In production, this is where you would call your email service, ticketing system, database update, or payment API only after approval passes.
def send_email(to_address: str, body: str) -> None:
print(f"\nSending email to {to_address}...")
print(body)
customer_email = "customer@example.com"
if get_human_approval(draft_email):
send_email(customer_email, draft_email)
else:
print("Email was not sent.")
- •If you want a cleaner structure, wrap the whole flow in a function. This keeps your approval logic reusable across multiple crews and tasks.
from dotenv import load_dotenv
load_dotenv()
def run_with_human_review():
result = crew.kickoff()
draft = str(result)
if get_human_approval(draft):
send_email("customer@example.com", draft)
return "Sent"
return "Blocked"
status = run_with_human_review()
print(f"\nFinal status: {status}")
Testing It
Run the script from your terminal and wait for the draft output. When prompted for approval, type yes and confirm that the mock send_email() function runs.
Then run it again and type anything other than yes. The script should stop before sending anything.
If you are wiring this into a real system later, test both branches first with dummy data only. You want to verify that no external side effect happens unless approval is explicit.
Next Steps
- •Add structured review metadata such as reviewer name, timestamp, and reason for rejection.
- •Replace the console prompt with a web UI or Slack approval flow.
- •Learn how to split risky work into separate tasks so only specific steps require human review.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit