Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 37 ยท Applications and Limits

Prompting Patterns

Practical prompting for large language models: clear instructions, few-shot examples, chain-of-thought, self-consistency, structured JSON output with validation and retries.

A prompt is the only steering wheel most developers have. The same model can fail or succeed on a task depending on how the request is written. Good prompting is not magic words: it is clear specification, useful examples, and a way to check the output. This lesson collects the patterns that work across models, and shows the code around the model that makes them reliable.

No real LLM runs inside this page. Where a model call is needed, the examples use a small rule-based stand-in function named fake_llm that returns scripted answers, so that you can run and test the surrounding code (templates, parsing, validation, voting). Replace the stand-in with a call to any real model API in your own project.

Anatomy of a good prompt

  1. Role and goal: who the model is acting as and what success looks like.
  2. Context: the facts it needs. The model knows nothing about your private data unless you include it (RAG).
  3. Task: a specific instruction, using verbs ("classify", "list", "rewrite in two sentences").
  4. Constraints: length, tone, forbidden content, allowed labels.
  5. Output format: exactly what the reply should look like, ideally machine-parseable.
  6. Examples: two to five input-output pairs.

Put long documents first and instructions last (or repeat the key instruction), and delimit sections with clear markers such as triple quotes or XML-like tags so the model can tell instructions from data.

Python A prompt template builder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Few-shot examples

Zero-shot gives only an instruction. Few-shot adds examples, which teaches format and edge cases better than prose. Choose examples that are diverse, correct, and cover the confusing cases; keep their order and label balance from biasing the answer (models lean toward the last label shown and toward the majority label). Dynamic few-shot selects the examples most similar to the current input by embedding search.

Chain of thought and reasoning

For multi-step problems, asking the model to think step by step (or giving worked examples with reasoning) improves accuracy, because the intermediate tokens are computation the model can use. Modern reasoning models do this internally, and for them very long hand-written reasoning prompts add little. Two cautions: written reasoning can be plausible but wrong, and it costs tokens and latency.

Self-consistency samples several reasoning paths at temperature above 0 and takes a majority vote on the final answers. Errors tend to be scattered while correct answers agree, so the vote is more accurate than a single sample. We can show the effect by simulation with the stand-in model.

Python Self-consistency by majority vote

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Accuracy rises with the number of votes, as long as wrong answers are scattered. If the model makes the same systematic mistake, voting cannot fix it. The cost is k times as many generations.

Structured output

Programs need parseable replies. Ask for JSON with named fields, give the schema, and validate in code. Do not trust the format: models occasionally add prose, trailing commas or wrong types. Many APIs offer constrained decoding or "JSON mode" that guarantees syntax, but you still need to check that the values make sense.

Python Parse, validate, retry

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The loop shows the engineering pattern: parse, validate against a closed set, feed the error message back, and cap the retries.

More patterns

  • Decompose: split a big task into steps (extract, then classify, then summarize), each with its own prompt. Easier to test and debug.
  • Self-critique and revise: ask the model to review its draft against a checklist, then rewrite.
  • Role and audience: "Explain to a new engineer" changes depth and vocabulary.
  • Negative examples and refusal rules: state what to do when the information is missing ("answer 'unknown'") to reduce invention.
  • Delimiters against injection: wrap untrusted text in tags and tell the model to treat it as data. This helps but does not fully prevent prompt injection, where text inside a document says "ignore previous instructions" (safety).
  • Prompt versioning: keep prompts in files under version control and run them against a fixed test set after every change (harness).

Common mistakes

  1. Vague goals ("make it better") instead of criteria.
  2. Contradictory instructions buried in long prompts.
  3. Examples that all share one label, teaching a bias.
  4. Evaluating a prompt on three hand-picked inputs.
  5. Putting secrets in a prompt: assume a user can extract the system prompt.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does self-consistency do?

    Independent errors tend to scatter while correct answers agree, so the plurality answer is more accurate.
  2. 2. Why validate model JSON in code even in JSON mode?

    Schema and value checks catch wrong labels, missing fields and out-of-range numbers.
  3. 3. Which example set is the best for few-shot classification?

    Diversity and balance teach the boundary and avoid a label bias.
  4. 4. What is prompt injection?

    Documents, web pages or user input can contain instructions the model might follow.

Summary

  • A strong prompt states role, context, task, constraints, format and examples.
  • Few-shot examples teach format; chain of thought and self-consistency help on multi-step problems.
  • Treat model output as untrusted: parse, validate, retry with the error message.
  • Version prompts and evaluate them on a test set, not on a few examples.

Next: Retrieval-Augmented Generation