Lab 14

Lab 14: Prompt Patterns

How the structure of a prompt changes model behavior — zero-shot, few-shot, chain-of-thought, and structured output side by side.

What you'll build

A small script that compares four common prompt patterns against the same question.

By the end of this lab you will have one Python file that builds four different prompt shapes: zero-shot, few-shot, chain-of-thought, and structured output prompting. You can run it in mock mode with no API key, or point it at a real chat completions endpoint by setting TOY_MODEL_API_KEY. In mock mode, the responses are static demonstrations so you can focus on the prompt structure itself.

The point is not that one pattern always wins. It is that prompt structure changes behavior. Same model, same question, different scaffolding.

Setup

cd ai_ecosystem_labs
python3 14-prompt-patterns/prompt_patterns.py "What is the capital of Germany?"

No dependencies needed. Python 3.9+ is enough for the built-in type hints used in the script.

Time guide. Setup: ~2 min. Working through it: 15–25 min if you mainly want to compare prompt shapes side by side.

Run it

Install nothing, then compare the outputs.

This lab uses only the Python standard library. In mock mode, it prints each prompt structure and a static sample response so you can compare message count and format. The interesting output differences only become observable once you export TOY_MODEL_API_KEY and let a real model respond.

cd ai_ecosystem_labs
python3 14-prompt-patterns/prompt_patterns.py "What is the capital of Germany?"

Walk through it

Four patterns, one question.

Zero-shot

This is the baseline: a system prompt plus the user question. It is cheap and often good enough for straightforward tasks. The tradeoff is that the model has to infer the pattern from almost nothing.

Few-shot

Here the script includes example question/answer pairs before the real question. That costs more tokens, but it gives the model a concrete pattern to imitate.

Chain-of-thought

This version asks the model to think step by step before answering. For easy geography questions that may feel like overkill, but the pattern becomes more useful as tasks get more ambiguous or multi-step.

Structured output

The final pattern asks for valid JSON matching a small schema. Prompting alone can be a little fragile, but it is the simplest way to feel the difference between free-form text and shape-constrained output.

The code

prompt_patterns.py

Try this

Three things to try before moving on.

  1. Run the script and compare the prompt shapes first. Start in mock mode and pay attention to the prompt structure, not the sample wording. Then try a real model if you have a key handy, because that is when output differences between patterns become meaningfully observable.
  2. Modify the few-shot examples to answer a different kind of question. Swap the geography examples for something else, like historical dates or unit conversions, and see how much behavior you can steer just by changing the examples.
  3. Extend the structured schema to include population. Add the field to the schema in build_structured_prompt() and see what happens. A real model may guess, hedge, or produce inconsistent formatting if it is not confident.

Concepts behind this

Read prompt engineering for the deeper concept page behind this lab.

Then connect it to evals thinking, because prompts become much more useful when you can compare them systematically instead of relying on vibe checks.