25 — Applications

Prompt Engineering#

System · Few-Shot · CoT✓ Mathematical
◆ 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).

Chain-of-thought prompting improved GSM8K (math) accuracy from 17.7% to 58.1% on PaLM 540B — for free, just by adding "Let's think step by step."
Interactive — prompt structure diagram

Python — structured prompting#

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
)
Pattern bridge: Crafting inputs to steer outputs is the art of framing. In markets, the framing effect shows how presentation shapes decisions.
← Previous
Batching & Throughput
Open in the full reader, with the topic sidebar →