AutoGen Tutorial (TypeScript): building prompt templates for beginners

By Cyprian AaronsUpdated 2026-04-21
autogenbuilding-prompt-templates-for-beginnerstypescript

This tutorial shows you how to build reusable prompt templates in AutoGen with TypeScript, then wire them into an agent so you can generate consistent beginner-friendly responses. You need this when your prompts start getting copied around the codebase, drift in quality, and become hard to maintain across multiple agents.

What You'll Need

  • Node.js 18+ installed
  • A TypeScript project with ts-node or a build step via tsc
  • autogen package installed
  • An OpenAI API key exported as OPENAI_API_KEY
  • Basic familiarity with AutoGen agents and messages
  • A terminal that can run TypeScript files directly

Install the package:

npm install autogen
npm install -D typescript ts-node @types/node

Set your API key:

export OPENAI_API_KEY="your-api-key"

Step-by-Step

  1. Start by defining a prompt template as a plain function. Keep it deterministic: same inputs should produce the same prompt structure every time.
type BeginnerPromptInput = {
  topic: string;
  audience: string;
  tone?: string;
};

export function buildBeginnerPrompt(input: BeginnerPromptInput): string {
  const tone = input.tone ?? "clear and friendly";

  return [
    `You are writing for ${input.audience}.`,
    `Explain ${input.topic} in a ${tone} way.`,
    "Use short paragraphs.",
    "Avoid jargon unless you define it.",
    "Give one concrete example.",
    "End with a simple next step."
  ].join("\n");
}
  1. Create a small wrapper that turns the template into an AutoGen-compatible user message. This keeps your prompt logic separate from the agent setup, which is where most teams make a mess.
import { AssistantAgent } from "autogen";
import { buildBeginnerPrompt } from "./promptTemplate";

const client = {
  apiKey: process.env.OPENAI_API_KEY!,
};

const agent = new AssistantAgent({
  name: "teacher",
  modelClient: client as any,
});

const prompt = buildBeginnerPrompt({
  topic: "TypeScript generics",
  audience: "beginner developers",
});

console.log(prompt);
  1. Add structured variables so the template can handle different beginner scenarios. This is where templates become useful in real systems: onboarding, support replies, internal docs, and training assistants all need predictable formatting.
type PromptContext = {
  topic: string;
  audience: string;
  goal: string;
  constraints?: string[];
};

export function buildStructuredPrompt(ctx: PromptContext): string {
  const constraints = ctx.constraints?.length
    ? ctx.constraints.map((item) => `- ${item}`).join("\n")
    : "- No extra constraints";

  return `
Task:
Explain ${ctx.topic} to ${ctx.audience}.

Goal:
${ctx.goal}

Rules:
${constraints}

Output format:
1. Plain-English explanation
2. One example
3. One warning about common mistakes
4. One next action
`.trim();
}
  1. Send the generated template into an AutoGen conversation. The important part here is that the agent never sees raw ad hoc text from scattered call sites; it sees one controlled prompt shape.
import { AssistantAgent } from "autogen";
import { buildStructuredPrompt } from "./promptTemplate";

async function main() {
  const agent = new AssistantAgent({
    name: "tutor",
    modelClient: {
      apiKey: process.env.OPENAI_API_KEY!,
    } as any,
  });

  const prompt = buildStructuredPrompt({
    topic: "array methods in TypeScript",
    audience: "new developers",
    goal: "teach them how map and filter work",
    constraints: ["Use no more than 150 words", "Include one code example"],
  });

  const result = await agent.run([{ role: "user", content: prompt }]);
  console.log(result.messages.at(-1)?.content);
}

main();
  1. Make the template reusable by separating content rules from runtime values. If you need multiple beginner templates, keep one shared formatter and pass different instruction blocks into it.
type TemplateBlock = {
  title: string;
  instructions: string[];
};

function renderTemplate(topic: string, block: TemplateBlock): string {
  return [
    `Topic: ${topic}`,
    `Section: ${block.title}`,
    ...block.instructions.map((line) => `- ${line}`),
  ].join("\n");
}

const introBlock = {
  title: "Beginner Explanation",
  instructions: [
    "Define the concept in one sentence",
    "Use everyday language",
    "Avoid advanced terminology"
  ],
};

console.log(renderTemplate("prompts", introBlock));

Testing It

Run the file with ts-node or compile it with tsc first if your project uses ESM settings. Confirm that each call returns a stable prompt string before you ever hit the API.

Then test with at least three inputs:

  • A simple topic like prompts
  • A technical topic like dependency injection
  • A constrained output like “under 100 words”

If the response quality changes wildly between runs, your template is probably too vague or mixing instruction styles. Tighten the wording, keep one instruction per line, and move variable data into explicit fields instead of embedding it inline.

Next Steps

  • Add Zod validation for template inputs so bad prompts fail before they reach the model.
  • Build a library of templates for support answers, onboarding flows, and policy explanations.
  • Learn how to combine prompt templates with AutoGen multi-agent workflows for review and critique loops

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