LearnTopic guide

Prompt Engineering

Prompt engineering is the practice of writing and testing the instructions, examples, and context that make a model behave reliably in production.

Topic overview

A prompt is the full input that steers a model: instructions, context, examples, and the user request. Small changes in wording, order, or format can change quality a lot, so treating prompts as engineered, tested artifacts, not one-off text, is what separates a demo from a production system.

Core concepts

Clear instructions: state the task, the audience, the constraints, and the output format directly. Specific instructions beat vague ones, and telling the model what to do works better than only telling it what to avoid.

System, user, and assistant roles: the system prompt sets durable behaviour and rules, the user message carries the request, and earlier assistant turns provide history. Keep stable rules in the system prompt.

Few-shot examples: a few input and output examples show the exact format and style you want. Pick examples that cover edge cases, and keep them consistent.

Structured output: ask for JSON that matches a schema, or use the provider's structured-output or tool-calling features, then validate the result in code. Never trust free text to parse itself.

Reasoning prompts: for multi-step problems, asking the model to work through the steps, or using a reasoning-capable model, often improves accuracy. It costs more tokens and latency, so use it where it pays off.

Context management: put the most relevant information in the prompt, separate it clearly with delimiters, and trim the rest. Long, noisy context lowers quality and raises cost.

Testing and versioning: keep prompts in version control, run them against a fixed set of test cases, and compare changes with evals before shipping. This is how you avoid silent regressions.

Prompt injection: untrusted text, such as a web page or an uploaded document, can contain instructions that hijack the model. Separate untrusted content from instructions, limit the tools and permissions the model has, and validate outputs.

Why it matters

Prompts are the cheapest lever for improving a system, and the easiest to break. Teams that test prompts, version them, and guard against injection ship more reliable products and debug faster than teams that edit prompt text by hand in production.

Interview relevance

Expect questions on how you would design a prompt for a given task, how you would get consistent JSON, how you would measure whether a new prompt is better, what few-shot examples do, when to prefer fine-tuning or retrieval over more prompting, and how you would defend against prompt injection. Strong answers mention evals and iteration, not just clever wording.