Advanced Prompt Engineering Techniques for Developers
A prompt is an interface contract with a model. Clear instructions, examples and a defined output format turn unpredictable answers into dependable ones.
A reliable prompt structure
Separate the parts of a prompt so the model cannot confuse instructions with data.
- Role: who the model is acting as
- Task: one specific job
- Context: only the facts needed, inside delimiters
- Constraints: length, tone, what to avoid
- Output format: schema or template
SYSTEM = "You are a support-ticket classifier. Return JSON only."
USER = '''Classify the ticket between <ticket> tags.
Categories: billing, bug, feature_request, other.
<ticket>
{ticket_text}
</ticket>
Return: {"category": "...", "confidence": 0.0-1.0, "reason": "one sentence"}'''Few-shot examples
Two or three diverse examples teach format and edge cases better than a long description. Include one tricky case.
examples = [
("I was charged twice this month", '{"category":"billing","confidence":0.97,"reason":"Duplicate charge."}'),
("App crashes when I upload a CSV", '{"category":"bug","confidence":0.95,"reason":"Crash on upload."}'),
("Could you add dark mode?", '{"category":"feature_request","confidence":0.93,"reason":"Asks for a new feature."}'),
]
shots = "\n".join(f"Ticket: {t}\nAnswer: {a}" for t, a in examples)Reasoning prompts
For maths, logic and multi-step tasks, ask the model to work through steps before the final answer, then request the answer on its own line so you can parse it. Some current models reason internally, so test whether explicit step-by-step instructions still help on your task.
prompt = '''Solve the problem. Think step by step, then give the result
on the last line as "ANSWER: <number>".
A model has 91% precision and 80% recall. What is its F1 score?'''Validate model output in code
Never trust formatted output blindly. Parse it, validate it and retry or fall back on failure.
import json
def parse(raw):
try:
data = json.loads(raw)
assert data["category"] in {"billing", "bug", "feature_request", "other"}
assert 0 <= float(data["confidence"]) <= 1
return data
except (json.JSONDecodeError, KeyError, AssertionError, ValueError):
return None
print(parse('{"category":"bug","confidence":0.9,"reason":"x"}'))
print(parse("Sure! Here you go")) # None -> retryTest prompts like code
Keep 20 to 50 representative inputs with expected outputs. Run every prompt change against them and track accuracy. Version prompts in Git, and include adversarial inputs such as prompt-injection text inside user content. Treat anything inside the delimiters as data, never as instructions.
Hands-on practice
Run the examples in the downloadable Jupyter notebook. The extra checks below focus on mistakes that appear in real projects, not just syntax.
Validate model JSON instead of trusting it
import json
raw='{"priority":"high","score":0.87}'
obj=json.loads(raw)
assert obj['priority'] in {'low','medium','high'}
assert 0<=obj['score']<=1
print(obj){'priority': 'high', 'score': 0.87}Treat model output as untrusted input and validate it like an API response.
Create a tiny regression test set
tests=[('2+2','4'),('capital of France','Paris')]
for prompt,expected in tests:
print(prompt,'-> expected:',expected)2+2 -> expected: 4 capital of France -> expected: Paris
Prompt changes should be tested against stable examples before deployment.
Self-check
- Can you explain why the first practice result is trustworthy rather than just plausible?
- What would you monitor or test before using this Prompt Engineering workflow in production?
- Which assumption in the example would break first on a larger or messier dataset?
Frequently asked questions
What is few-shot prompting?
Including a few input and output examples in the prompt so the model copies the pattern.
Does chain-of-thought always help?
It helps on multi-step reasoning for many models, but not on simple lookups, and some models already reason internally. Test on your own data.
How do I stop prompt injection?
Separate instructions from untrusted data, validate outputs, limit tool permissions and never let model output run as code without checks.
Primary references
Use these official sources when exact syntax, defaults or definitions matter beyond the examples here.




















































































































































