# Meta Prompting

Course: [Prompt Engineering](https://www.devtutorials.ai/learn/prompt-engineering) · Section: Advanced Reasoning
Source: https://www.devtutorials.ai/learn/prompt-engineering/advanced-reasoning/meta-prompting

> Use prompts to work on prompts: generate a prompt from a goal, critique it against a checklist, extract the rewrite in code, compare versions, and let tests decide which wins.

You have learned many rules for good prompts: the task, the audience, the format, the context, examples, and delimiters. Checking all of them by hand every time is slow. What if the model could help?

**Meta prompting** means using a prompt to work on **another prompt**. You can ask a model to write a first draft of a prompt, to review a prompt against a checklist, or to rewrite a weak prompt into a stronger one. You stay in charge: you test every suggestion before you keep it.

**The main idea:** a model can draft, review, and improve prompts, but your tests decide which version wins.

## A Real-World Example

Think about writing an important email.

- You write a first draft.
- You ask a colleague: "Is this clear? What's missing?"
- They say: "You never said what the deadline is, and your request is hidden at the end."
- You fix it and send it.

The colleague doesn't write your email for you from the start. They review it and make suggestions, and you decide. Meta prompting gives you a colleague like this for your prompts.

## What You Will Learn

In this lesson, you will learn:

1. What meta prompting is.
2. How to ask a model to write a prompt from a goal.
3. How to ask for a review against a checklist.
4. How to get an improved prompt that your code can read.
5. How to compare versions and see what changed.
6. Why your tests, and not the model, decide which prompt is better.

Use the **Chat** tab in the Prompt sandbox to ask for a review or a rewrite. Then paste the old and new prompts into the **Compare** or **Test** tab to see if the new one is really better.

## What Is Meta Prompting?

There are three common ways to use it:

| Use | What you give the model | What you get back |
|---|---|---|
| **Generate** | A goal, a reader, and limits | A first draft of a prompt |
| **Critique** | A prompt and a checklist | A list of problems with the prompt |
| **Improve** | A prompt and its problems | A rewritten prompt |

All three still need testing. A rewritten prompt that **sounds** better is not always **better**.

## Generate a Prompt From a Goal

Describe what you want the prompt to do, and ask the model to write it. Tell the model which parts to include, from the Prompt Structure lesson.

```js runnable title="A meta prompt that writes a prompt"
function writePromptFor(goal, reader, limits) {
  return [
    "Write a prompt for a language model.",
    "Purpose: " + goal,
    "The output will be read by: " + reader,
    "Limits: " + limits.join("; "),
    "Include: an instruction, the output format, and a {{input}} slot for the data.",
    "Reply with only the prompt, between <prompt> and </prompt> tags.",
  ].join("\n");
}

console.log(writePromptFor(
  "Summarise customer support tickets",
  "a support team lead",
  ["at most 2 sentences", "mention the product", "no customer names"]
));
```

Expected output:

```text
Write a prompt for a language model.
Purpose: Summarise customer support tickets
The output will be read by: a support team lead
Limits: at most 2 sentences; mention the product; no customer names
Include: an instruction, the output format, and a {{input}} slot for the data.
Reply with only the prompt, between <prompt> and </prompt> tags.
```

When you ask for tags around the prompt, your code can pull the prompt out cleanly, without the extra chat text the model adds.

## Read the New Prompt in Code

```js runnable title="Pull the prompt out of the reply"
function extractPrompt(reply) {
  const m = reply.match(/<prompt>\s*([\s\S]*?)\s*<\/prompt>/i);
  return m ? m[1] : null;
}

const reply = "Sure! Here is a prompt you can use:\n<prompt>\nSummarise the ticket for a team lead in at most 2 sentences. Name the product. Do not include customer names.\nTicket: {{input}}\n</prompt>\nLet me know if you want changes.";
const prompt = extractPrompt(reply);

console.log(prompt);
console.log("Has an input slot:", prompt.includes("{{input}}"));
```

Expected output:

```text
Summarise the ticket for a team lead in at most 2 sentences. Name the product. Do not include customer names.
Ticket: {{input}}
Has an input slot: true
```

The reply here is a sample written for this example. The code ignores the friendly lines before and after the tags.

## Critique Against a Checklist

A review is most useful when it follows a **checklist**. Without a checklist, the model gives general advice like "be more specific". With one, it tells you exactly which rule is broken.

You can also run a simple checklist with code first. Code is not as smart as a model, but it is instant, free, and always gives the same result.

```js runnable title="Check a prompt against a checklist in code"
const checklist = [
  { rule: "has an action word", test: (p) => /\b(write|summari[sz]e|classify|list|explain|extract|translate)\b/i.test(p) },
  { rule: "names the reader", test: (p) => /\b(for|reader|audience)\b/i.test(p) },
  { rule: "sets a length", test: (p) => /\b(\d+|one|two|three)\s+(words?|sentences?|lines?|bullets?)\b/i.test(p) },
  { rule: "sets a format", test: (p) => /\b(json|list|table|one word|sentence)/i.test(p) },
  { rule: "marks the data", test: (p) => /<\w+>|{{\w+}}|"""/.test(p) },
];

function review(p) {
  return checklist.filter((c) => !c.test(p)).map((c) => "missing: " + c.rule);
}

console.log(JSON.stringify(review("Summarise this ticket.")));
console.log(JSON.stringify(review("Summarise the ticket for a team lead in 2 sentences.\n<ticket>{{input}}</ticket>")));
```

Expected output:

```text
["missing: names the reader","missing: sets a length","missing: sets a format","missing: marks the data"]
[]
```

Now ask the real model for a review and a rewrite. The system prompt makes it act as a reviewer, and the user message gives it the checklist.

<PromptTry tab="chat" system="You are an expert prompt engineer. You review prompts and rewrite them to be clear and testable." prompt="Review the prompt below.\nPrompt to review: Summarise this ticket.\n\nCheck it for: action word, reader, length, output format, and marked data.\nFirst list each problem in one line. Then write an improved prompt between <prompt> and </prompt> tags, with a {{input}} slot for the ticket." />

**Easy Way to Remember:** a meta prompt works best when you **give a checklist and get back a prompt inside tags**. The checklist keeps the review focused. The tags let your code use the result.

## See What Changed

When the model rewrites a prompt, look at what it actually changed. A quick word comparison shows which words were added and which were removed.

```js runnable title="Compare two prompt versions"
function changes(oldP, newP) {
  const a = new Set(oldP.toLowerCase().match(/[a-z0-9{}]+/g));
  const b = new Set(newP.toLowerCase().match(/[a-z0-9{}]+/g));
  return {
    added: [...b].filter((w) => !a.has(w)),
    removed: [...a].filter((w) => !b.has(w)),
  };
}

const v1 = "Summarise this ticket.";
const v2 = "Summarise the ticket for a team lead in 2 sentences.";
console.log(JSON.stringify(changes(v1, v2)));
```

Expected output:

```text
{"added":["the","for","a","team","lead","in","2","sentences"],"removed":["this"]}
```

The new version added a reader ("team lead") and a length ("2 sentences"). Now you know what to test: does the reply follow the length rule, and is it right for a team lead?

## Improve, Then Answer

There is a lighter kind of meta prompting that works in any chat. Ask the model to **rewrite your prompt first, and then answer the improved version**, all in one reply. This is a quick fix for a weak first prompt when you don't have time to test different versions.

Try it. The original request is unclear. The model first makes it specific, and then answers it.

Look at Step 1 in the reply. It shows what the model thinks you meant. If that is wrong, you have caught the misunderstanding before reading the whole answer.

Note that articles and research papers use the term "meta prompting" in slightly different ways. In this course, it means using a prompt to create or improve a prompt.

## Your Tests Decide

A model will happily rewrite a good prompt into a worse one, and say that it is better. So **never keep a rewrite without testing it**. Run both versions on the same test cases, and keep the new one only if it gets a higher score.

```js runnable title="Keep a new version only if it scores higher"
const history = [{ version: "v1", score: 4 }];

function propose(version, score) {
  const best = history.reduce((a, b) => (b.score > a.score ? b : a));
  if (score > best.score) {
    history.push({ version, score });
    return version + " kept (" + score + " > " + best.score + ")";
  }
  return version + " rejected (" + score + " <= " + best.score + ")";
}

console.log(propose("v2", 7));
console.log(propose("v3", 6));
console.log(propose("v4", 9));
console.log("Best so far:", history[history.length - 1].version);
```

Expected output:

```text
v2 kept (7 > 4)
v3 rejected (6 <= 7)
v4 kept (9 > 7)
Best so far: v4
```

The scores here are sample test results. v3 sounded fine, but it scored lower, so it was dropped. This simple rule protects you from "improvements" that actually make things worse. The next lessons, especially Prompt Evaluation, show how to get these scores with the Test tab.

Small models are not good at reviewing prompts. They may miss real problems or invent problems that aren't there. Treat their review as ideas to test, not as the final answer. For important prompts, use a bigger model for the review, or review the prompt yourself.

## Common mistakes

| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
| Trusting the rewrite | "The model is an expert, so its version is better." | Test both versions on the same cases. | A worse prompt replaces a good one. |
| No checklist | "Just ask it to improve the prompt." | Give it the rules to check. | You get general advice like "be more specific". |
| No tags around the new prompt | "I'll copy the prompt out by hand." | Ask for the prompt between tags. | Extra chat text ends up inside your prompt. |
| Not looking at the changes | "It's the same prompt, just better." | Compare the words that were added and removed. | A rule you needed was quietly deleted. |
| Replacing old versions | "The new one replaces the old one." | Keep a history of versions with their scores. | You can't go back when the new one fails. |
| Rewriting again and again | "One more round will make it perfect." | Stop when the scores stop getting better. | You spend time without any real gain. |

## Exercises

In each exercise, the last line should give back a value. Press **Check** to compare your value with the expected answer.

## What to Remember

- **Meta prompting** uses a prompt to **write, review, or improve** another prompt.
- To get a draft prompt, give the model a **goal, a reader, and limits**.
- To get a specific review, give the model a **checklist**.
- Ask for the result **between tags**, and pull it out with code.
- **Compare versions** to see exactly what changed.
- **Your tests decide.** Keep a new version only if it gets a higher score.
- For a quick fix in a chat, ask the model to **improve your request first, and then answer it**.

Next: **Generate Knowledge Prompting**, where you ask the model to list what it knows before it answers.
