Advanced Prompt Engineering
Structured prompting, few-shot design, and reliable JSON output
Overview
Advanced prompting moves beyond single instructions toward controllable, testable behavior. Key levers are role and system messages, few-shot exemplars, explicit output schemas, and reasoning scaffolds like chain-of-thought or step decomposition. The goal is to reduce variance so the same prompt yields reliable results across inputs, which matters far more in production than clever one-off wording.
Syntax / Usage
Separate a stable system message (rules, format, persona) from the dynamic user message (the task). For machine-readable output, request structured JSON rather than parsing free text. Modern APIs support schema-constrained responses.
from openai import OpenAI
client = OpenAI()
SYSTEM = (
"You are a support classifier. Return JSON only. "
"Never invent categories outside the allowed set."
)
resp = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": SYSTEM},
{"role": "user", "content": "My invoice charged me twice this month."},
],
response_format={
"type": "json_schema",
"json_schema": {
"name": "ticket",
"schema": {
"type": "object",
"properties": {
"category": {"enum": ["billing", "bug", "account", "other"]},
"urgency": {"enum": ["low", "medium", "high"]},
},
"required": ["category", "urgency"],
"additionalProperties": False,
},
},
},
)
print(resp.choices[0].message.content) # valid JSON matching schema
Examples
Few-shot exemplars anchor tone and edge-case handling better than adjectives. Keep examples short and representative:
FEWSHOT = [
{"role": "user", "content": "reset link expired"},
{"role": "assistant", "content": '{"category":"account","urgency":"medium"}'},
{"role": "user", "content": "site is completely down"},
{"role": "assistant", "content": '{"category":"bug","urgency":"high"}'},
]
messages = [{"role": "system", "content": SYSTEM}, *FEWSHOT,
{"role": "user", "content": "how do I export my data?"}]
For hard reasoning tasks, ask the model to think stepwise but return only the final answer, keeping the chain out of user-facing output:
prompt = (
"Solve step by step internally, then output ONLY the final number.\n"
"A cart has 3 items at $12 and 2 at $7.50. Total?"
)
Common Mistakes
- Cramming rules, data, and format into one giant user message
- Relying on prose parsing instead of JSON schema or function calling
- Over-long few-shot sets that waste tokens and bias toward examples
- Vague adjectives ("be concise") without concrete constraints or examples
- Never running a prompt against a fixed eval set before shipping changes
See Also
prompt-engineering large-language-models ai-evaluation