Part 6 · Using Language Models in Practice

Chapter 27Prompt Engineering Fundamentals

If you learn one practical skill from this book, make it this one. Prompt engineering — the craft of writing the input that gets a model to do what you want — is the cheapest, fastest, and highest-leverage skill in all of applied AI. A better prompt costs nothing, takes effect instantly, and frequently outperforms expensive fine-tuning. This chapter teaches the fundamentals with plenty of before-and-after examples, and it assumes nothing. Master these basics and you will solve the large majority of tasks without ever touching training, exactly as Chapter 20 promised.

Why Prompting Is the Highest-Leverage Skill

Recall in-context learning from Chapter 20: you teach the model directly in the prompt, with no training at all. That makes prompting extraordinarily efficient. Improving a prompt is free and immediate — no data to prepare, no compute to spend, no waiting. And because modern models are so capable, a well-crafted prompt often matches or beats what you could achieve by fine-tuning. The overwhelming majority of the improvements you will ever make to a model's behavior come from writing better prompts. It is the first thing to reach for and the thing you will use most.

The Golden Rule: Be Clear and Specific

Everything in prompting flows from one principle: the model is not a mind reader. It responds to exactly what you write, so vague input produces vague output. The single biggest improvement most beginners can make is simply to be more specific about what they want.

python
# Vague prompt -- the model has to guess what you want:
"Write about dogs."

# Clear, specific prompt -- the model knows exactly what to produce:
"Write a friendly 3-sentence introduction to caring for a new puppy, "
"aimed at first-time owners, focusing on feeding, vet visits, and training."

The second prompt leaves almost nothing to chance: it specifies the topic, the length, the audience, the tone, and the points to cover. The model can now do exactly what you intended instead of guessing. Specificity is not about longer prompts for their own sake; it is about removing ambiguity.

The Anatomy of a Good Prompt

Strong prompts tend to contain a few recognizable parts. Not every prompt needs all of them, but knowing the toolkit helps you write deliberately rather than by trial and error.

  • A role or persona — who the model should act as ("You are an experienced pediatric nurse").
  • A clear task — exactly what you want done, stated plainly.
  • Context or input — the background or material the task operates on.
  • Constraints — the format, length, tone, and any do's and don'ts.
  • Examples — optional demonstrations of what you want, which we explore fully in the next chapter.
Figure 27.1 — The anatomy of a well-built prompt: role, task, context, constraints, and optional examples, assembled deliberately.
Figure 27.1 — The anatomy of a well-built prompt: role, task, context, constraints, and optional examples, assembled deliberately.

Give the Model a Role

Telling the model who to be shapes its tone, vocabulary, and assumed expertise, often dramatically. "Explain inflation" and "You are an economics teacher explaining inflation to a curious ten-year-old" produce very different answers — the second simpler, warmer, and better targeted. A role is a compact way to steer many qualities of the response at once.

Specify the Output Format

Models follow formatting instructions remarkably well, so tell the model exactly how you want the response shaped: as a bulleted list, a single paragraph, a table, a specific number of items, or a particular structure. If you need three options, ask for exactly three. If you need a short answer, say so. Leaving format unspecified means the model picks one for you, and it may not be the one you wanted.

python
"Suggest names for a coffee shop. Respond as a numbered list of exactly 5 names, "
"each followed by a short phrase explaining the vibe it evokes. No preamble."

Provide Context and Constraints

Give the model the background it needs, and be explicit about boundaries. State not only what to do but, when it matters, what not to do — "answer only from the text below," "do not invent statistics," "keep it under 100 words." Constraints turn a loose request into a precise one. The grounded RAG prompts from Chapter 36 are a perfect example: the instruction to answer only from the provided context is a constraint that prevents the model from wandering.

Show, Don't Just Tell

Often the fastest way to convey what you want is to show an example rather than describe it. A single well-chosen example can communicate a format or style more clearly than a paragraph of instructions. This is the bridge to few-shot prompting, the centerpiece of the next chapter — but it is worth knowing now that when words fail to pin down what you mean, an example usually succeeds.

Iterate: Prompting Is Experimental

You will rarely write the perfect prompt on the first try, and that is completely normal. Prompting is experimental, much like debugging code: you write a prompt, look at where the output falls short, adjust, and try again. Treat each disappointing response as information about what to clarify next, not as a failure. The people who are best at prompting are simply the ones most willing to iterate.

Reusable Prompt Templates

Once a prompt works well for a recurring task, capture it as a template with placeholders you fill in each time. This makes good prompting repeatable and keeps your code clean.

python
def summarize_prompt(text, audience):
    return (
        f"You are a skilled science writer. Summarize the text below for {audience}. "
        "Use plain language, keep it to 3 sentences, and avoid jargon.\n\n"
        f"Text: {text}"
    )

prompt = summarize_prompt(article, audience="high school students")

Common Prompting Mistakes

Most weak prompts share a few avoidable flaws, all of which you now know how to fix.

  • Vagueness — leaving the task, format, or audience unspecified, forcing the model to guess.
  • Conflicting instructions — asking for opposite things, like "be thorough but very brief," without resolving the tension.
  • Too much irrelevant context — burying the actual request in a wall of unnecessary text, which both wastes tokens and risks the 'lost in the middle' effect from Chapter 12.
  • Assuming hidden knowledge — expecting the model to know specifics it was never given, instead of providing them.
  • Burying the key instruction — hiding the most important request in the middle of a long prompt rather than placing it prominently at the start or end.

Summary

Prompt engineering is the highest-leverage skill in applied AI: free, instant, and often a match for fine-tuning. Its golden rule is to be clear and specific, since the model responds to exactly what you write. Good prompts are built deliberately from a role, a clear task, context, constraints, and optional examples; specifying the output format and stating what not to do are especially powerful. When words fall short, show an example. Prompting is experimental, so iterate like a debugger, capture what works as reusable templates, and avoid the common traps of vagueness, conflicting instructions, irrelevant context, assumed knowledge, and buried instructions.

These fundamentals handle most tasks. Chapter 28 adds the advanced techniques — few-shot examples, chain-of-thought reasoning, and self-consistency — that push quality higher on the genuinely hard problems.

Practice

Exercises

  1. 1Take the vague prompt 'tell me about marketing' and rewrite it into a clear, specific prompt that names a role, a task, an audience, a format, and a length. Run both and compare the results.
  2. 2Write the same request twice, once with a role assigned to the model and once without. Describe how the role changed the tone and content of the response.
  3. 3Take any request and add an explicit output-format constraint (for example, 'exactly five bullet points, each under ten words'). Show how the response changes.
  4. 4Write a reusable prompt template, as a function with placeholders, for a task you do often. Use it on two different inputs to confirm it works.
  5. 5Find or write a deliberately bad prompt that commits at least two of the common mistakes from this chapter. Diagnose the mistakes and rewrite it well.
  6. 6Take a task where your first prompt gives a mediocre answer, then iterate at least three times, recording what you changed each round and how the output improved.
View detailed solutions for all chapters →