AutoGen Tutorial (Python): building prompt templates for beginners
This tutorial shows you how to build reusable prompt templates in AutoGen with Python, then wire them into agents so your prompts stay consistent, parameterized, and easy to maintain. You need this when your team keeps rewriting the same instructions by hand and you want a clean way to generate beginner-friendly prompts without copy-paste drift.
What You'll Need
- •Python 3.10+
- •
autogen-agentchatinstalled - •An OpenAI API key
- •A
.envfile or exported environment variable forOPENAI_API_KEY - •Basic familiarity with AutoGen agents and model clients
- •A terminal and a code editor
Step-by-Step
- •Start by installing the packages and setting your API key. If you are using a virtual environment, activate it first so your dependencies stay isolated.
pip install autogen-agentchat python-dotenv
export OPENAI_API_KEY="your-api-key-here"
- •Create a simple prompt template function instead of hardcoding long strings inside your agent setup. This makes it easy to swap variables like topic, audience, and tone without rewriting the whole prompt.
from textwrap import dedent
def beginner_prompt_template(topic: str, audience: str = "beginners") -> str:
return dedent(f"""
You are a helpful tutor.
Explain {topic} for {audience}.
Rules:
- Use simple language.
- Give one concrete example.
- Keep the answer under 120 words.
- Avoid jargon unless you define it.
""").strip()
print(beginner_prompt_template("AutoGen prompt templates"))
- •Load your API key and create a model client that AutoGen can use. Then pass the generated template into an assistant agent so the prompt is built dynamically at runtime.
import os
from dotenv import load_dotenv
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient
load_dotenv()
model_client = OpenAIChatCompletionClient(
model="gpt-4o-mini",
api_key=os.environ["OPENAI_API_KEY"],
)
system_prompt = beginner_prompt_template("AutoGen prompt templates")
agent = AssistantAgent(
name="tutor",
model_client=model_client,
system_message=system_prompt,
)
- •Build a small helper that injects runtime values into the template before each run. This is the part that makes prompt templates useful in production, because your app can generate different prompts for different users or workflows.
def build_prompt(topic: str, level: str, output_format: str) -> str:
return dedent(f"""
You are helping a {level} developer learn {topic}.
Requirements:
- Use plain English.
- Format the answer as {output_format}.
- Include exactly one example.
- End with one practical tip.
""").strip()
prompt = build_prompt(
topic="AutoGen prompt templates",
level="beginner",
output_format="a short checklist",
)
print(prompt)
- •Run the agent with a user message that matches the template’s intent. Keep the user input narrow; let the template control tone, structure, and constraints.
import asyncio
from autogen_agentchat.messages import TextMessage
async def main():
result = await agent.on_messages(
[TextMessage(content="Teach me how prompt templates help in AutoGen.", source="user")],
cancellation_token=None,
)
print(result.chat_message.content)
asyncio.run(main())
Testing It
Run the script and confirm two things: first, the printed template should include your injected values like topic and audience; second, the agent response should follow your constraints such as simple language and short length. If you get an authentication error, check that OPENAI_API_KEY is set in the same shell session where you run Python. If the output ignores your formatting rules, tighten the system message and make it more specific about structure and length.
A good test is to change one variable at a time, such as switching topic from "AutoGen prompt templates" to "tool calling", then verify that only that part changes in the generated prompt. That tells you your template is actually parameterized instead of being a static string.
Next Steps
- •Add Jinja-style templating for more complex prompts with optional sections
- •Store templates in separate files so product teams can edit them without touching code
- •Combine templates with tool use and conversation memory for multi-step agent workflows
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