Prompt Engineering

2025-05-18

With large language models becoming part of the developer toolkit, knowing how to write effective prompts is a practical skill. Prompt engineering is about structuring instructions to get useful and consistent outputs from an LLM. The difference between a vague prompt and a well-crafted one can be the difference between useless output and a genuinely helpful response.

Be Specific

The most common mistake is being too vague. "Write me a function" gives the model very little to work with. "Write a TypeScript function that takes an array of numbers and returns the median value, handling edge cases like empty arrays" gives a clear specification.

Specificity includes the language, the framework, the expected input and output, error handling requirements, and any constraints. The more context we provide, the closer the output will be to what we actually need.

Provide Examples

Few-shot prompting, where we include examples of the desired input and output, is one of the most effective techniques. If we want the model to format data in a certain way, showing one or two examples is often more effective than describing the format in words.

Convert these to the format "LAST, First":

Input: John Smith
Output: SMITH, John

Input: Alice Johnson
Output: JOHNSON, Alice

Input: David Lee
Output:

The model picks up on the pattern and applies it consistently.

System and Role Instructions

When using chat-based models, the system message sets the behaviour for the entire conversation. Defining the role, tone, and constraints upfront keeps the outputs consistent.

For example, a system message like "You are a senior backend developer. Respond with concise, production-ready code. Use TypeScript. Do not include explanations unless asked." shapes every response that follows.

Chain of Thought

For complex tasks, asking the model to think step by step produces better results than asking for the answer directly. This is because the model generates reasoning tokens before arriving at the conclusion, which reduces errors.

Phrases like "Think through this step by step" or "Explain your reasoning before giving the final answer" trigger this behaviour.

Iterative Refinement

Rarely does the first prompt produce exactly what we need. Prompt engineering is an iterative process. We start with a prompt, evaluate the output, and adjust. If the output is too verbose, we add "Be concise." If it misses edge cases, we specify them. If the format is wrong, we show an example of the correct format.

Saving effective prompts as templates for recurring tasks saves time. A well-tested prompt for code review, documentation generation, or test writing can be reused across projects.

Limitations

Prompts cannot fix fundamental model limitations. LLMs can produce confident but incorrect answers. They struggle with precise arithmetic, very long reasoning chains, and tasks that require up-to-date information beyond their training data. Treating the output as a starting point that needs review, rather than a finished product, is the right mindset.