Ankit KanojiyaBeginner
Evergreen guide by Ankit Kanojiya · Beginner · AI / ML
Prompt Engineering Fundamentals for Developers
Core techniques for writing prompts that produce reliable, parseable output. Covers structure, examples, output formatting, and common failure modes.
- Prompt Engineering
- LLM
- Few-Shot
- Structured Output
- Best Practices
Prompt engineering is the practical skill of shaping an LLM's behavior through its input. For application developers the goal is rarely clever phrasing; it is producing output that is correct, consistent, and easy to consume in code. This article covers the techniques that move the needle and the traps that waste time.
Structure a prompt in clear sections
A reliable prompt usually has three parts: a role or instruction, the context or data, and the requested output format. Separating them helps the model and makes the prompt easier to maintain.
You are a support-ticket classifier.
Ticket:
"My invoice shows two charges for the same order."
Classify the ticket into one of: billing, shipping, technical, other.
Respond with only the category word.
Keeping instructions and data visually distinct reduces the chance the model treats part of the data as a command.
Be specific about the output
Vague requests produce vague, hard-to-parse answers. State the exact shape you want.
- Name the format: JSON, a single word, a markdown table.
- List allowed values for categories so the model cannot invent new ones.
- Say what to do when unsure, such as returning the string unknown rather than guessing.
When you need structured data, ask for JSON and describe the schema in the prompt:
Return a JSON object with keys:
"sentiment": one of "positive", "neutral", "negative"
"confidence": a number from 0 to 1
No prose, no code fences.
Many providers also offer a structured-output or JSON mode that guarantees valid JSON. Prefer that over parsing free text when it is available, and still validate against a schema in code.
Show examples (few-shot prompting)
When a task is hard to describe but easy to demonstrate, include a few worked examples. This is called few-shot prompting, as opposed to zero-shot where you only give instructions.
Convert dates to ISO format.
Input: March 3, 2024 -> 2024-03-03
Input: 7/4/21 -> 2021-07-04
Input: next Tuesday -> unknown
Input: Jan 1 2030 ->
Two or three good examples usually outperform a longer paragraph of instructions. Make sure examples cover edge cases, including how to handle inputs that should be rejected.
Give the model room to reason
For multi-step reasoning, math, or logic, asking the model to work through the problem before answering improves accuracy. A simple instruction like think step by step can help, but if you need a clean final value, separate the reasoning from the answer so you can discard the former.
First, work out the calculation in a "reasoning" field.
Then put only the final number in an "answer" field.
This keeps the useful output parseable while still benefiting from intermediate steps.
Common failure modes
- Overstuffing: cramming many unrelated tasks into one prompt lowers quality on each. Split them.
- Conflicting instructions: telling the model to be thorough and to answer in one word forces a trade-off it may resolve unpredictably.
- Leading questions: phrasing that assumes a conclusion biases the answer.
- Prompt injection: when a prompt includes untrusted text (a web page, a user message, a document), that text may contain instructions like ignore previous directions. Treat external content as data, isolate it clearly, and never let it control privileged actions without validation.
Iterate with a small test set
Treat prompts like code. Keep a handful of representative inputs with expected outputs, and re-run them whenever you change the prompt or the model version. A prompt that works on one example often breaks on the next, and a tiny regression set catches that early.
Keep it as simple as it can be
Start with the shortest prompt that could work and add detail only when a real failure shows up. Every extra instruction is another thing the model can misread and another token you pay for. Clear structure, explicit output rules, a few targeted examples, and a repeatable test set will handle the large majority of application needs before any advanced technique is required.