stackademic

The leading education platform for anyone with an interest in software development.

Prompt Engineering

System and user messages, few-shot examples, chain-of-thought, and practical tips

Overview

Prompt engineering is the practice of structuring inputs so LLMs produce reliable, useful outputs. You specify role, task, format, constraints, and examples. Good prompts reduce hallucinations, improve consistency, and make outputs parseable by downstream code.

Modern chat APIs accept a messages array with roles: system (behavior/rules), user (the request), and optionally assistant (prior turns or few-shot demonstrations).

Syntax / Usage

An effective prompt template:

[System]  Who you are, tone, hard constraints, output format
[User]    Task, context, data, question
[Optional few-shot] Example input → example output (1–5 pairs)
[Optional] "Think step by step" for chain-of-thought (CoT)

Techniques:

TechniqueWhen to use
Zero-shotSimple, well-defined tasks
Few-shotFormat enforcement, classification, style matching
Chain-of-thoughtMulti-step reasoning, math, debugging
Structured outputAsk for JSON schema; use API response_format when available
DelimitersWrap user content in """ or XML tags to reduce injection

Example message structure:

[
  { "role": "system", "content": "You summarize support tickets in 2 bullet points. Output JSON: {\"summary\": string[], \"priority\": \"low\"|\"high\"}" },
  { "role": "user", "content": "Ticket: Login fails after password reset for SSO users." }
]

Examples

Few-shot classification:

Classify sentiment as positive, negative, or neutral.

Review: "Shipping was fast, product broke on day two."
Sentiment: negative

Review: "Exactly what I needed."
Sentiment: positive

Review: "{{user_review}}"
Sentiment:

Production tips: version prompts in git, log completions (redact PII), and validate JSON with a schema parser before using results.

Common Mistakes

  • Vague instructions ("be helpful") instead of explicit format and constraints
  • Mixing instructions with untrusted user content without delimiters
  • Too many few-shot examples—wastes context and can confuse the model
  • Asking for JSON without specifying keys or handling parse failures
  • Changing prompts in production without regression tests on a golden set

See Also

large-language-models ai-apis rag-basics ai-agents