Prompt and context engineering
Before building a RAG system, which is expensive and complex, you need to know how to get the most out of a prompt and decide which information is worth sending to the model.
Reading time: 2 min
Prompt engineering
Prompt engineering is the craft of writing instructions so the model carries out the task reliably: defining the role, the goal, the constraints, the output format and, when needed, adding examples. A good prompt is more like a well-written brief for a new colleague than a magic formula.
The practices that pay off most are unsophisticated: be specific, explain the reason behind each rule, clearly separate instructions from data (for example, with tags or delimiters) and ask for structured output that your code can validate. Above all, treat prompts as code: versioned and tested against real cases before changing them.
Context engineering
Context engineering is deciding what goes into the context window on each call, and in what order: which instructions, which documents, which part of the history and which application data. If prompt engineering is how you ask for the task, context engineering is the information the model uses to solve it.
Context is a scarce resource: every token costs money and time, and irrelevant information distracts the model. The usual techniques are selecting only what is relevant, summarising long histories, trimming noise (email footers, legal notices) and building the context dynamically for each case. For example, the general policy for a new customer; the recent history for a regular customer with an open issue.
Fine-tuning vs prompt engineering vs RAG
These are three ways to adapt a model to your case. Prompt engineering changes the instructions. RAG gives it external information at query time. Fine-tuning retrains it on your own examples so that its baseline behaviour changes.
The sensible order is this: prompt engineering first, because it is cheap and immediate; then RAG, when the problem is that the model doesn't have the information; and fine-tuning only when you need a very specific style or format consistently and have exhausted the other options. Fine-tuning is not a good way to teach the model data that changes: that is what RAG is for.
Test yourself on this module
Copy this prompt and paste it into your AI (ChatGPT, Claude, Gemini…). It will give you a 20-question multiple-choice test on the module's concepts and then suggest a hands-on exercise.
Act as the examiner for Dani Pérez's "AI Engineering Guide". Examine me on the module "Prompt and context engineering" (https://daniperez.pro/en/resources/ai-engineering-guide/prompt-and-context-engineering). Concepts covered by the exam: - Prompt engineering - Context engineering - Fine-tuning vs prompt engineering vs RAG Exam: 1. 20 multiple-choice questions, each with 4 options (a, b, c, d) and a single correct answer. 2. Ask about understanding and judgement (what each thing is for and when NOT to use it), not about memorising definitions. 3. Spread the position of the correct answer evenly across a, b, c and d. 4. Ask me the questions in 4 rounds of 5. Don't give any example answer (like "1a 2b 3c 4d 5a"): I already know to answer with the letters. Don't tell me whether I got them right until I have answered all 20. 5. At the end, mark them all: for each question, my answer, the correct one and a short explanation. Give me my score out of 20 and tell me which concepts I should review. Hands-on exercise (after marking): 6. Ask me what application I have or want to build, and which language and framework I work with. If I don't have one, use this: a customer support application for an online shop, with tickets, customers, orders and a knowledge base (FAQ and return policies). In that case, focus the exercise on: drafting a reply to a ticket, building the context according to the type of customer (new, regular or angry). 7. Suggest an exercise that applies this module's concepts to that application: goal, requirements, criteria to consider it done and common mistakes to avoid. 8. Don't solve it for me. When I bring you my solution, review it against those criteria.