Skip to content

Generative AI

Prompt Templating

O(n) time to scan and fill a template of length n.

The idea, in plain English

A prompt template is a fill-in-the-blank form for talking to an AI. It works like a mail-merge letter with placeholders such as '{name}' and '{date}'. You write the wording once. Then each time, you plug in different variables, such as a topic, a tone, or a user's question, instead of retyping the instructions from scratch.

How it works

  1. 1Write the template text once, with placeholders marked like {topic} or {question}.
  2. 2Collect the real values you want to fill in. Store them as a simple key-to-value mapping, called a dictionary or object.
  3. 3Scan the template for every {placeholder}, and replace each one with its matching value from the mapping.

When you'd use it

Use this any time you send an AI the same kind of instruction repeatedly with different details, such as a support bot, a summarizer, or a code reviewer. Templating keeps the wording consistent and makes the variable parts easy to swap.

Common beginner mistakes

  • Don't forget to fill in a placeholder. If you do, the literal text "{topic}" gets sent to the model instead of a real value, which confuses it.
  • Avoid building prompts by joining raw strings together instead of using a template. This makes it hard to see or reuse the fixed wording once you have many slightly different prompts.

Try it — edit and run

Click the code to edit · press ⌘/Ctrl+↵ to run

Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.

Expected output — hit Run to try it
Template: You are a helpful assistant. Answer the user's question about {topic} in a {tone} tone. User: {question}
Filled (cooking): You are a helpful assistant. Answer the user's question about cooking in a friendly tone. User: How do I boil an egg?
Filled (finance): You are a helpful assistant. Answer the user's question about finance in a formal tone. User: What is compound interest?

Not sure this is the right topic? See the learning paths → or where this leads →