CrewAI Tutorial (Python): building prompt templates for beginners

By Cyprian AaronsUpdated 2026-04-21
crewaibuilding-prompt-templates-for-beginnerspython

This tutorial shows you how to build reusable prompt templates in CrewAI with Python, then wire them into agents and tasks so your prompts stay consistent and easy to maintain. You need this when your team keeps rewriting the same instructions for every task, or when you want cleaner prompts for beginner-friendly assistants that produce more predictable output.

What You'll Need

  • Python 3.10+
  • crewai
  • crewai-tools if you want extra tools later
  • An OpenAI API key set as OPENAI_API_KEY
  • Basic familiarity with Agent, Task, and Crew
  • A terminal and a code editor

Install the package:

pip install crewai

Set your API key:

export OPENAI_API_KEY="your-key-here"

Step-by-Step

  1. Start by creating a small project file and defining your template strings.
    Keep the template simple at first: one variable for the user topic, one for the output format, and one for tone.
from textwrap import dedent

TASK_TEMPLATE = dedent("""
You are a helpful assistant for beginners.
Explain the topic: {topic}

Rules:
- Use simple language
- Avoid jargon unless you define it
- Give one practical example
- Format the answer as: {format_style}
""").strip()

AGENT_BACKSTORY = dedent("""
You are an expert teacher who explains technical concepts to beginners.
Your job is to make complex ideas easy to follow.
""").strip()
  1. Create an agent that uses the backstory template and a task that uses the prompt template.
    CrewAI does not require a separate templating engine here; plain Python string formatting is enough and easier to debug.
from crewai import Agent, Task

topic = "prompt templates in CrewAI"
format_style = "short bullet points"

agent = Agent(
    role="Beginner Tutor",
    goal="Teach technical topics clearly",
    backstory=AGENT_BACKSTORY,
    verbose=True,
)

task_description = TASK_TEMPLATE.format(
    topic=topic,
    format_style=format_style,
)

task = Task(
    description=task_description,
    expected_output="A beginner-friendly explanation with one example",
    agent=agent,
)
  1. Add a crew and run it.
    This is the point where your template becomes operational: the formatted string gets passed into the task, then the agent produces output based on that prompt.
from crewai import Crew, Process

crew = Crew(
    agents=[agent],
    tasks=[task],
    process=Process.sequential,
    verbose=True,
)

result = crew.kickoff()
print(result)
  1. Make your template reusable by wrapping it in a function.
    This is the pattern you want in production: keep prompt text in one place, pass variables in from code, and avoid hardcoding task descriptions everywhere.
from textwrap import dedent

def build_beginner_prompt(topic: str, format_style: str = "plain English"):
    return dedent(f"""
    You are a helpful assistant for beginners.
    Explain the topic: {topic}

    Rules:
    - Use simple language
    - Avoid jargon unless you define it
    - Give one practical example
    - Format the answer as: {format_style}
    """).strip()
  1. Use that function inside a second task so you can compare outputs across topics.
    This is useful when you want multiple beginner prompts with consistent structure but different content.
second_task = Task(
    description=build_beginner_prompt(
        topic="how CrewAI tasks work",
        format_style="numbered steps",
    ),
    expected_output="A clear beginner explanation with steps",
    agent=agent,
)

crew = Crew(
    agents=[agent],
    tasks=[task, second_task],
    process=Process.sequential,
    verbose=True,
)

result = crew.kickoff()
print(result)

Testing It

Run the script and check that both tasks complete without errors. If your API key is set correctly, you should see verbose logs from CrewAI followed by generated text for each task.

Test with different values for topic and format_style. If the output changes cleanly each time without breaking structure, your template is working.

Also inspect whether the response matches your rules: simple language, one example, and the requested format. If it drifts, tighten the wording in your template before adding more complexity.

Next Steps

  • Add variables for audience level, tone, and output length
  • Move templates into separate .py or .yaml files for larger projects
  • Learn how to combine prompt templates with tools and memory in CrewAI

Keep learning

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

Related Guides