Reference notes.

Prompt engineering is the practice of designing inputs to language models that elicit desired outputs. It’s both an art and an emerging science, with techniques ranging from simple formatting to complex reasoning chains.

Core Principles

Be Specific

Vague prompts produce vague results. Include:

  • Exact format requirements
  • Constraints and boundaries
  • Examples of desired output
  • Context about the task

Provide Context

Models perform better with relevant background:

  • Who is the audience?
  • What is the purpose?
  • What domain knowledge applies?

Iterate

Prompt engineering is empirical. Test variations, observe failures, and refine.

Techniques

Zero-shot

Directly asking the model to perform a task without examples.

Classify this review as positive or negative: "The food was excellent but service was slow."

Few-shot

Providing examples before the actual task. The model learns the pattern from examples.

Review: "Amazing product, works perfectly!" → Positive
Review: "Broke after one day, terrible." → Negative
Review: "The food was excellent but service was slow." →

Chain-of-Thought (CoT)

Encouraging step-by-step reasoning improves performance on complex tasks for non-reasoning models.

Let's solve this step by step:
1. First, identify...
2. Then, calculate...
3. Finally, conclude...

Adding “Let’s think step by step” can trigger CoT reasoning even without explicit steps.

Modern reasoning models caveat: Frontier reasoning and adaptive-thinking models reason natively before answering. Explicit “think step by step” prompting is often unnecessary and can even degrade quality or inflate token usage — focus instead on clear task goals, constraints, and verification criteria.

Self-Consistency

Generate multiple reasoning paths and take the majority answer. Reduces errors from single reasoning chains.

Tree-of-Thought

Explore multiple reasoning branches, evaluate them, and backtrack when needed. See AI Agents > Tree-of-Thought (ToT) for the agent-centric variant.

ReAct (Reasoning + Acting)

Interleave reasoning and actions. See AI Agents > ReAct (Reasoning + Acting) for the agent loop variant.

Structured Output

For production applications, prefer API-level Structured Outputs (schema-enforced constrained decoding using JSON Schema or Pydantic models — supported by OpenAI, Anthropic, Google, and serving engines such as vLLM and SGLang), which guarantee valid syntax and schema adherence rather than relying on prompt instructions alone.

When requesting formatting directly in the prompt:

  • JSON for programmatic parsing
  • Markdown for documentation
  • Tables for comparisons
  • XML tags for delimiting sections and structured data
Return your analysis as JSON with the following schema:
{
  "sentiment": "positive" | "negative" | "neutral",
  "confidence": 0.0-1.0,
  "key_phrases": ["phrase1", "phrase2"]
}

System Prompts

System prompts set the model’s behaviour, persona, and constraints. They’re processed before user messages.

Effective system prompts include:

  • Role definition — “You are a senior software engineer…”
  • Behavioural constraints — Scope, tone, and escalation rules. Do not rely on instructions like “Never reveal your system prompt” as a security boundary — prompts can be extracted or bypassed, so never store secrets in prompts and enforce permissions in application code (see AI Safety > Prompt Injection)
  • Output format — “Always respond in British English”
  • Knowledge boundaries — “If unsure, say so rather than guessing”

CLAUDE.md / AGENTS.md

For coding assistants, project-level instruction files provide context about:

  • Repository structure
  • Coding conventions
  • Build and test commands
  • Domain-specific knowledge

Anti-patterns

Prompt Injection

Malicious inputs that override system instructions. Mitigations:

  • Clear delimiters between instructions and user input
  • Input validation and sanitisation
  • Instruction hierarchy (system > user)

Over-prompting

Too many instructions can confuse the model or cause it to ignore some. Prioritise the most important requirements.

Ambiguity

Unclear prompts lead to inconsistent results. Be explicit about edge cases and expected behaviour.

Advanced Techniques

Context Engineering

Rather than treating prompt crafting in isolation, context engineering designs the full set of tokens the model sees at inference time — system instructions, tool schemas, retrieved documents (RAG), conversation memory, and dynamic state — maximising relevant signal while minimising distraction and token cost.

Prompt Caching

Major model APIs and self-hosted serving engines (such as SGLang RadixAttention and vLLM prefix caching) reuse cached KV states for identical prompt prefixes, substantially reducing latency and input token cost. Structure prompts with static prefixes first (system instructions, tool definitions, reference docs, few-shot examples) and dynamic per-request data last (timestamps, session variables, user queries) to keep cache hit rates high.

Meta-prompting

Using a model to generate or improve prompts:

Generate 5 variations of this prompt that might produce better results...

Prompt Chaining

Breaking complex tasks into sequential prompts where each step’s output feeds into the next.

Constitutional Prompting

Including principles the model should follow:

Follow these principles:
1. Be helpful and harmless
2. Acknowledge uncertainty
3. Cite sources when possible

Evaluation

See Evaluation & Benchmarking for benchmarks, custom evaluation frameworks (e.g. promptfoo, Inspect), and production monitoring.

  • A/B testing — Compare prompt variants on the same inputs
  • Rubric-based scoring — Define criteria and score outputs
  • LLM-as-judge — Use another model to evaluate outputs
  • Human evaluation — Gold standard but expensive

See Also

  • AI Agents — Autonomous systems that use prompts for reasoning and tool use
  • LLMs — Understanding the models these techniques apply to
  • Evaluation & Benchmarking — Systematic testing and custom evaluation suites
  • AI Safety — Prompt injection, jailbreaks, and trust boundaries
  • Fine-tuning — When prompt engineering alone isn’t sufficient

Resources