Skip to main content
Xenors Xenors
Home › Data Science › Advanced Prompt Engineering Techniques for Developers

Module 10: Generative AI, LLMs & Prompt EngineeringBy Ashok Kumar Yadav4 min read

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.

Prompt template with role, task, context, examples and output format sections
Quick answer: Write prompts with a clear role, a specific task, relevant context, a few examples and an exact output format. Test every prompt on a fixed set of inputs and change one thing at a time.

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
Structured prompt with delimiters
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.

Build a few-shot block
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.

Step-by-step with a parsable answer line
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.

Validate JSON responses
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 -> retry

Test 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

Practice example
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

Practice example
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.

Written by Ashok Kumar Yadav
Ashok Kumar Yadav writes Xenors tutorials on applied AI, programming and data workflows. Every lesson is built around code you can run and check yourself.
GitHub · LinkedIn

Continue exploring