AutoGen Tutorial (Python): building prompt templates for advanced developers
This tutorial shows you how to build reusable prompt templates in AutoGen for Python agents, then wire them into a multi-agent workflow that stays consistent across tasks. You need this when your agents start drifting, repeating instructions, or producing inconsistent outputs because the prompt logic is scattered across your codebase.
What You'll Need
- •Python 3.10 or newer
- •
pyautogeninstalled - •An OpenAI API key set in your environment
- •Basic familiarity with
AssistantAgent,UserProxyAgent, and AutoGen conversations - •A terminal and a Python file to run the examples
Step-by-Step
- •Start by defining prompt templates as plain Python strings. Keep the system instruction, task instruction, and output contract separate so you can reuse them across agents and tasks.
from textwrap import dedent
SYSTEM_TEMPLATE = dedent("""
You are a senior Python engineer working on agentic workflows.
Follow instructions exactly and return concise, production-ready answers.
""").strip()
TASK_TEMPLATE = dedent("""
You will help the user with: {task}
Constraints:
- Use only the provided context.
- Ask clarifying questions only if required.
- Return the final answer in Markdown.
""").strip()
OUTPUT_TEMPLATE = dedent("""
Output format:
1. Summary
2. Implementation details
3. Risks or edge cases
""").strip()
- •Next, render those templates with runtime values before passing them into AutoGen. This keeps your prompts deterministic and makes it easy to test different task variants without rewriting agent setup code.
def build_prompt(task: str) -> str:
return "\n\n".join([
SYSTEM_TEMPLATE,
TASK_TEMPLATE.format(task=task),
OUTPUT_TEMPLATE,
])
prompt = build_prompt("design a retry strategy for an API client")
print(prompt)
- •Now create an assistant agent using the rendered prompt as its system message. Use
UserProxyAgentto trigger the conversation and keep execution disabled unless you explicitly need code execution.
import os
import autogen
config_list = [
{
"model": "gpt-4o-mini",
"api_key": os.environ["OPENAI_API_KEY"],
}
]
assistant = autogen.AssistantAgent(
name="prompt_engineer",
system_message=build_prompt("review a prompt template for consistency"),
llm_config={"config_list": config_list},
)
user_proxy = autogen.UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER",
code_execution_config=False,
)
- •If you want advanced developer behavior, split responsibilities across multiple prompt templates instead of one giant blob. For example, one template can enforce style, another can define review criteria, and another can define the final response shape.
STYLE_TEMPLATE = dedent("""
Style rules:
- Be direct.
- Prefer bullets over paragraphs.
- Call out trade-offs explicitly.
""").strip()
REVIEW_TEMPLATE = dedent("""
Review criteria:
- Is the prompt reusable?
- Are variables clearly named?
- Does it constrain output enough for production use?
""").strip()
def build_review_prompt(subject: str) -> str:
return "\n\n".join([
SYSTEM_TEMPLATE,
STYLE_TEMPLATE,
REVIEW_TEMPLATE,
f"Subject: {subject}",
])
- •Finally, run a conversation using your templated prompt. This example asks the assistant to review a prompt design, which is a good pattern for validating template quality before you use it in production workflows.
result = user_proxy.initiate_chat(
assistant,
message=build_review_prompt(
"a claims triage agent that must classify documents and escalate uncertain cases"
),
)
print(result.summary if hasattr(result, "summary") else "Chat completed.")
Testing It
Run the script once with a valid OPENAI_API_KEY in your environment and confirm that AutoGen creates both agents without errors. Then change the {task} input and verify that the generated system message changes while the rest of the structure stays stable.
You should also inspect the rendered prompt before sending it to the model. If your output becomes inconsistent, usually one of three things is wrong: a missing variable substitution, overly broad instructions, or conflicting constraints between templates.
For production-style testing, add assertions around build_prompt() so every template includes required sections like role, task, and output format. That catches regressions before they reach an agent conversation.
Next Steps
- •Add Jinja2 templates if you need conditionals or loops in prompts
- •Store templates in versioned files so product and engineering can review changes separately
- •Build a small eval harness that compares outputs from different prompt versions
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