AgentLu
← Learn

Prompt Engineering

12 min read

A prompt is the text you give a language model before it answers: who it should be, what you want done, and what the answer should look like. Prompt engineering is treating that text as something you build and test rather than something you type and hope for.

You do not need to write code to do this well. Almost everything here is a sentence or two of plain English plus a way to check whether the sentence worked - and if you can write a clear instruction for a classmate, you already have the skill.

It is worth the effort because it is the cheapest change available. A better prompt costs nothing, takes a minute, and is often the difference between an answer you can use and one you throw away. It is also the first thing to try before reaching for a bigger model, a tool, or a rewrite.

Start by saying what the answer should be

The most common problem is not a model that cannot do the task - it is an instruction that never said what the task was, or what the answer should look like. Compare these two.

A task with no shape

Weak
Tell me about this review.javascript
Improved
Classify this review as positive or negative.javascript

The weak version asks for a topic, so a paragraph of opinion is a perfectly good answer. The improved version asks for a decision and names the only two answers allowed, which makes the reply short, checkable and useful.

One label instead of two

Weak
Is this review positive?javascript
Improved
Classify this review as positive or negative.javascript

A yes/no question forces the model to decide whether 'not exactly positive' counts as no. Naming both labels removes the guesswork and gives you a result you can count across many reviews.

No answer format

Weak
Say something helpful about what I should wear.javascript
Improved
Recommend one specific item of clothing and explain why it fits the weather, in one sentence.javascript

Asking for something specific and a reason gives the model a target. Without it you get a friendly paragraph that mentions three options and commits to none.

Show the pattern when the format matters

Sometimes the instruction is clear and the model still drifts - it answers with a sentence when you wanted a single word, or invents its own category names. Two labelled examples fix this faster than any amount of extra describing.

Describing the format

Weak
Classify the review. Only answer with the label.javascript
Improved
Here are two examples.
Input: I loved it! Output: positive.
Input: I hated it. Output: negative.
Now classify this review.javascript

The weak version tells the model what to do; the improved version shows it. Examples communicate the exact shape of an input and an output in a way a sentence cannot, and they are the quickest fix when the model keeps adding commentary of its own.

Ask for the working when the answer has steps

Models are much better at arithmetic, planning and logic when they write the reasoning down before committing to an answer. Each step gives the next one something to build on.

Answer only

Weak
A shop has 5 apples and sells 2. How many are left?javascript
Improved
A shop has 5 apples and sells 2. How many are left? Think step by step, then give the answer.javascript

The added sentence asks for the working before the conclusion. When the answer is wrong you can also see which step went wrong, which is far more useful than a bare wrong number.

One big request

Weak
Plan a birthday party with a budget of 50 dollars.javascript
Improved
Break the problem into steps: choose a venue, choose food, choose decorations, then total the cost. Solve one step at a time, then give the final plan.javascript

Splitting a vague request into named sub-problems turns it into a series of small tasks. The model stops trying to hold the whole thing in one thought, and the constraint - the budget - is much less likely to be forgotten.

One good answer is not evidence

The trap with prompting is that a single success feels like proof. You try a prompt, the model classifies one review correctly, and you move on - but a model that agrees with you once may have simply guessed well. The only way to know whether an instruction works is to run it on several inputs you already know the answers to, and to look for the pattern in what it gets wrong.

Pick a handful of examples that pull in different directions: an obviously positive one, an obviously negative one, one that mixes praise and criticism, and one that is sarcastic. If a prompt only works on the easy cases, that is information - it tells you which part of the instruction is too vague.

When you are building an agent, that comparison is a loop you can write in a few lines. Here is the shape of it:

const cases = [
  { input: "I loved every minute of it.", expected: "positive" },
  { input: "The acting was wooden and dull.", expected: "negative" },
  { input: "Beautiful shots, but I was bored.", expected: "negative" },
];

for (const testCase of cases) {
  const answer = await llm({
    system: "Classify this review as positive or negative.",
    user: testCase.input,
  });

  const label = answer.content.trim().toLowerCase();
  log(testCase.expected === label ? "ok" : "miss", testCase.input, "→", label);
}javascript

Running one prompt over several known inputs. The labels you expect sit next to the inputs, so a disagreement is easy to spot.

Where prompting stops working

Prompting is powerful, but it is not a substitute for facts or for checking your own work. Three limits are worth recognising early, because they are the reason real agents end up with code around their prompts.

The model does not know things it was never told. Ask it for today's weather or your school's late policy and it will produce something that sounds right, because a confident sentence is what it was trained to write. When an answer has to be true, you have to supply the facts: look them up with a tool, or retrieve the relevant document and instruct the model to answer only from what it was given.

A prompt cannot validate its own output. If a field must be an ISO date or a positive number, a polite request is not a guarantee. The fix is to check the value in code and repair or reject it, which is why agents that fill in forms usually extract first and validate afterwards.

Instructions get diluted. A long prompt with ten rules competes for the model's attention, and the rule you care about most may be the one that loses. When a prompt starts to feel like a legal document, that is usually a sign that part of the job belongs in code - a tool call, a lookup, or a check - rather than in more sentences.

Here is the same idea in code: the prompt does the part it is good at, and the code handles the part it cannot.

const forecast = await callTool("getWeather", { city: "Chicago" });

const answer = await llm({
  system: "Recommend one item of clothing, using only the weather data you are given.",
  user: JSON.stringify(forecast),
});

if (!forecast || typeof forecast.tempC !== "number") {
  return { reply: "I could not read the forecast, so I will not guess." };
}

return { reply: answer.content };javascript

The prompt answers the question; the code supplies the facts and refuses an answer that does not survive checking.

A small project, end to end

The smallest useful prompt project is a classifier: one prompt, one input, one label. Building it end to end shows where each idea above actually lands.

1. Write the instruction in a file of its own

Keeping the prompt in its own file is what makes it editable: you can change the wording and re-run without touching anything else. The task, the labels and the output shape all live here.

# Review classifier

Classify the review the user sends as positive or negative.

Answer with one word: positive or negative.markdown

2. Read it and send it to the model

The agent is short because the prompt is doing the work. It reads the file, sends the user's message along with those instructions, and returns the answer. Notice the fallback: if the file is empty, the agent sends something sensible instead of an empty instruction.

export default async function run({ input, llm, files }) {
  const file = files.find((entry) => entry.path === "instructions.md");
  const instructions = String(file?.content ?? "").trim();

  const answer = await llm({
    system: instructions || "You are a helpful assistant.",
    user: input,
  });

  return { reply: answer.content };
}javascript

3. Run it on inputs you know the answer to

Send an obviously positive review and you should get positive; send an obviously negative one and you should get negative. Those two runs tell you almost nothing on their own - they are the easy cases. The interesting run is the mixed one: "Beautiful shots, but I was bored" is a negative review with a compliment in it, and it is exactly the input a vague prompt gets wrong.

4. Change one thing and run it again

This is the loop that makes the project teach you something. If the mixed review comes back positive, the instruction is probably letting the compliment outweigh the complaint, so say what wins: "Decide based on the reviewer's overall opinion, not on any single compliment." Then re-run every input, not just the one that failed - a fix for one case that breaks another is not a fix.

Mistakes worth catching early

What goes wrongWhat it looks likeWhat to do instead
Testing one inputThe prompt looks perfect because the one review you tried came back right.Run four or five inputs you already know the answers to, including an awkward one.
Describing the format instead of showing itThe model replies with a sentence when you wanted a single label.Give two labelled examples: an input line and an output line.
Asking for something vague"Tell me about this" produces a friendly paragraph that answers nothing.Name the decision, the labels, and the shape of the answer.
Naming only one option"Is this positive?" gives a yes/no answer you cannot count.Name every label the answer is allowed to use.
Adding rules instead of codeThe prompt grows to ten rules and the important one starts getting ignored.Move the check, the lookup or the calculation into code.
Trusting a fact the model cannot knowA confident answer about something it was never told.Supply the data with a tool or a document, and say to answer only from that.

Debugging a prompt

  • Read the actual reply before reading anything else. Most problems are visible in the first line of the output - a paragraph where a label belongs, or a confident claim about something you never supplied.
  • Change one sentence at a time. If you rewrite three things and it improves, you have no idea which one mattered.
  • Test the boundary cases: the mixed review, the sarcastic one, the one with no clear opinion. Prompts fail at the edges long before they fail in the middle.
  • Check whether the model is inventing facts. If the answer contains a detail you never gave it, that is a grounding problem, and no amount of rephrasing will fix it.
  • When a rule keeps being ignored, make it shorter and more absolute: "Never state the final answer" beats "Try to avoid giving the answer directly".