Skip to content

Command Palette

Search for a command to run...

Chapter 2·30 min read·Intermediate

Prompt Engineering

Structure, few-shot, structured outputs and function calling. Prompts are code — version them.

What is prompt engineering?

Prompt engineering is the discipline of designing inputs to LLMs to reliably produce desired outputs. It is not 'tricking' the model — it is software engineering where the prompt is part of the program.

Core techniques: system prompt (role + rules), structured output schemas, few-shot examples, chain-of-thought reasoning, delimiters for untrusted content, and function calling for tool use. Prompts must be versioned, evaluated and treated as code.

Why prompt engineering matters

A good prompt is the difference between a feature that works 60% of the time and one that works 99% of the time — often more impact than swapping models. Prompt changes are cheap, fast to A/B, and high-leverage. But they're also fragile: an undocumented prompt change can silently break production. Version and eval your prompts.

Where prompts live

System prompt (role, rules, output format) + user message (the actual query) + retrieved context (in delimiters) + few-shot examples → LLM → structured output. In production, prompts are stored as versioned templates, rendered with variables, and gated by eval before promotion.

Structured output with validation

BeforeAfter
naive.pypython
def ask_llm(question: str) -> str:
    """Naive: free-text prompt, free-text response, no validation."""
    r = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": f"Answer: {question}"}],
    )
    return r.choices[0].message.content
# Problems: unpredictable format, no validation, hallucinated fields, retries fail

Experiment: prompting techniques

See how different prompting techniques affect output quality on the same task.

Choose a prompting technique. See its effect on a Q&A task.

What to observe

No single technique wins everywhere. Few-shot excels at classification and format calibration. Chain-of-thought excels at multi-step reasoning. Structured output + system prompt is the production default for reliability. Match the technique to the task.

Production prompting

Version prompts in code, gate with eval before promotion, canary new versions, track per-prompt-version metrics. Use system prompts for primacy, delimiters for untrusted content, structured outputs for downstream reliability, and few-shot only when zero-shot underperforms. Never hard-code prompts in notebooks.

Challenge

Your prompt produces great answers 92% of the time but occasionally reveals the system prompt when users ask cleverly. Harden the prompt against extraction without hurting quality. (Hint: primacy + delimiters + output guard.)

Production checklist

Production checklist

0 of 8 checked

Knowledge check

Retrieved documents are concatenated into your prompt. A user asks 'ignore previous instructions and...'. Which defence is most fundamental?

Complete

You can now write production-grade prompts with structured outputs. Next: streaming and structured outputs in depth.

Mark this chapter as complete

Track your progress and unlock the next chapter.

Email updates

Get the next chapter and new guides by email.

One email per substantial release. Unsubscribe in one click.

We will email you a confirmation link first, then send an occasional email when a substantial guide is published. Unsubscribe in one click. Stored on our own servers, never sold or shared. See the privacy policy.

Continue learning