25 — Applications

Prompt Engineering#

System · Few-Shot · CoTHeuristic 1 min read
◆ The PatternThe craft of asking questions that get the best answers

Prompt engineering is the art of structuring inputs to maximize output quality. The key tools: system prompts (set behavior), few-shot examples (show by example), and chain-of-thought (encourage step-by-step reasoning).

Zero-shot: Direct instruction → answer
Works for simple tasks. "Translate to French: Hello world"
Few-shot: Example₁, Example₂, ..., Query → answer
Provide 2-5 examples of input→output. Model infers the pattern.
Chain-of-Thought: "Think step by step" → reasoning → answer
Dramatically improves math, logic, and multi-step reasoning. Model "shows its work."

Advanced techniques: self-consistency (sample N times, majority vote), tree-of-thought (explore multiple reasoning paths), structured output (ask for JSON with a schema), persona prompting (act as expert in X).

Few-shot chain-of-thought prompting raised PaLM 540B’s GSM8K accuracy from about 18% to about 57% (Wei et al. 2022); simply adding “Let’s think step by step” also helps (Kojima et al. 2022).
Interactive — prompt structure diagram

Python — structured prompting#

Pattern bridge: Crafting inputs to steer outputs is the art of framing. In markets, the framing effect shows how presentation shapes decisions.
Worked example

If a single answer is right 60% of the time and samples were independent, a majority vote over 5 samples would be right 68.3% of the time and over 15 samples 78.7%. That is the idea behind self-consistency. In practice samples share their mistakes, so the real gain is smaller — but the direction holds.

Where it misleads
  • Results are sensitive to small formatting changes — spacing, separators, option order — sometimes by many points (Sclar et al. 2024).
  • A prompt tuned on one model version often does not transfer to the next.
  • Prompt tweaks evaluated on a handful of examples are a garden of forking paths; test on a held-out set.
In code
from math import comb
def majority(p, n):                               # independent samples, binary right/wrong
    return sum(comb(n, k) * p**k * (1 - p)**(n - k) for k in range(n // 2 + 1, n + 1))
votes = {n: round(majority(0.6, n), 3) for n in (1, 5, 15)}
Python, in your browser — edit the code and run it again

Assumes import numpy as np. Configurations named after real models use their published shapes; other numbers are made up or simulated, as the comments say.

The same idea with a library
from openai import OpenAI
client = OpenAI()

# Chain-of-thought with structured output
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a math tutor. "
         "Always work through problems step by step. "
         "Return JSON with 'steps' (array) and 'answer' (number)."},
        {"role": "user", "content": "If a train travels 120km in "
         "1.5 hours, what is its speed in m/s?"}
    ],
    response_format={"type": "json_object"},
    temperature=0.1
)
Sources
  • J. Wei et al., “Chain-of-Thought Prompting Elicits Reasoning in Large Language Models”, NeurIPS, 2022
  • X. Wang et al., “Self-Consistency Improves Chain of Thought Reasoning in Language Models”, ICLR, 2023
  • M. Sclar, Y. Choi, Y. Tsvetkov & A. Suhr, “Quantifying Language Models’ Sensitivity to Spurious Features in Prompt Design”, ICLR, 2024

Last reviewed October 2026 · How this site checks its content · Report a mistake

Linked from 1
← Previous
Batching & Throughput
Open in the full reader, with the topic sidebar