Prompt Engineering
Prompt engineering shapes LLM behavior through instructions, examples, and structure. Good prompts reduce hallucinations, enforce output format, and improve task accuracy without retraining the model.
Search across all documentation pages
Prompt engineering shapes LLM behavior through instructions, examples, and structure. Good prompts reduce hallucinations, enforce output format, and improve task accuracy without retraining the model.
Quick-reference recipe card - copy-paste ready.
SYSTEM = """You are a Python code reviewer.
- Flag bugs, security issues, and style problems.
- Respond in bullet points.
- If code is fine, say "LGTM"."""
messages = [
{"role": "system", "content": SYSTEM},
{"role": "user", "content": f"Review this code:\n```python\n{code}\n```"},
]When to reach for this:
"""prompt_engineering.py - system, few-shot, chain-of-thought, and structured prompts."""
from __future__ import annotations
from openai import OpenAI
client = OpenAI()
# Chain-of-thought for reasoning
cot_messages = [
{"role": "system", "content": "Solve math problems step by step. Show your work, then give the final answer on the last line as ANSWER: <number>."},
{"role": "user", "content": "A store has 24 apples. They sell 3/8 of them. How many remain?"},
]
cot = client.chat.completions.create(model="gpt-4o-mini", messages=cot_messages, temperature=0)
print(cot.choices[0].message.content)
# Few-shot classification
few_shot = [
{"role": "system", "content": "Classify support tickets as billing, technical, or account. Reply with one word only."},
{"role": "user", "content": "I was charged twice this month"},
{"role": "assistant", "content": "billing"},
{"role": "user", "content": "The API returns 500 errors on POST /users"},
{"role": "assistant", "content": "technical"},
{"role": "user", "content": "I need to change my email address"},
]
result = client.chat.completions.create(model="gpt-4o-mini", messages=few_shot, temperature=0)
print("classification:", result.choices[0].message.content)
# Grounded QA - only use provided context
context = "Our API rate limit is 1000 requests/minute for Pro plans and 100/minute for Free plans."
grounded = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "Answer ONLY using the provided context. If the answer is not in the context, say 'I don't have that information.'"},
{"role": "user", "content": f"Context:\n{context}\n\nQuestion: What is the Pro plan rate limit?"},
],
temperature=0,
)
print(grounded.choices[0].message.content)What this demonstrates:
temperature=0 for deterministic classification and extraction.| Pattern | Technique | Best For |
|---|---|---|
| Zero-shot | Instructions only | Simple, well-defined tasks |
| Few-shot | 2-5 examples | Classification, extraction |
| Chain-of-thought | "Think step by step" | Math, logic, multi-step reasoning |
| Grounded | "Use only this context" | RAG, factual QA |
| Role-based | "You are a senior X" | Tone and expertise level |
# Template prompts with variables
def build_review_prompt(code: str, focus: str) -> list[dict]:
return [
{"role": "system", "content": f"You are a code reviewer focusing on {focus}."},
{"role": "user", "content": f"```python\n{code}\n```"},
]
# Store prompts in version-controlled files
from pathlib import Path
SYSTEM_PROMPT = Path("prompts/reviewer.txt").read_text()response_format.---, XML tags, markdown sections).| Alternative | Use When | Don't Use When |
|---|---|---|
| Prompt engineering | Quick iteration, no training data | Need consistent behavior at scale |
| Fine-tuning | Thousands of labeled examples | Few examples (use few-shot) |
| RAG | Answers need current/private data | Model already knows the information |
| Structured output / tools | Need guaranteed schema | Free-form text is acceptable |
temperature=0 for reproducible reasoning.<user_input>...</user_input>.prompts/v2/reviewer.txt).<context>{retrieved_docs}</context>
<question>{user_query}</question>Stack versions: This page was written for Python 3.14.0 (stable 3.14, maintenance 3.13), FastAPI 0.115+, Django 5.2, Flask 3.1, Pydantic 2, PyTorch 2.6+, pandas 2.2+, Polars 1.x, ruff 0.9+, and uv 0.6+.
Reviewed by Chris St. John·Last updated Jul 16, 2026