Prompt Engineering — talk to AI like a pro — 4. Role, constraints & format

17 min read min de lecture
Chapter 04

Role, constraints & format

Chapter 4 of 10 · 40%

Chapter objectives

  • Assign a role to frame the register
  • Impose useful constraints and dial creativity through phrasing
  • Force a usable format (including machine-readable) and handle long contexts

Assigning a role: a powerful shortcut

"You are a lawyer specializing in employment law": in one sentence, this instruction steers the vocabulary, the level of detail, the caution of the wording and even the structure of the answers. The role is a framing shortcut: instead of listing twenty micro-instructions about register, you activate at once a whole field of knowledge and conventions associated with that profile.

Why it works: the model learned from texts written by all kinds of profiles. By assigning it a role, you shift its likely answers toward the zone of texts that profile would have written. "You are a merciless proofreader" produces franker critiques; "you are a science communicator for the general public" produces simpler explanations — on the same question, with the same model.

The role gains power when you add an audience: "You are a cybersecurity expert speaking to non-technical executives" frames both ends of the communication. Sofia uses "You are a B2B communications manager writing for rushed restaurant owners" — the register is locked on both sides.

What a role does not do

Beware of the urban legend: assigning an expert role does not make the model more competent. "You are the world's best tax specialist" creates no new tax knowledge — it steers the style, tone and vocabulary, not the truthfulness. A model that gets a tax rule wrong will get it just as wrong with a fancy title, but with more confidence in the wording. That's precisely the trap: the role increases apparent confidence, not reliability.

The role therefore serves to frame the register, never to guarantee accuracy. For high-stakes domains (legal, medical, financial), combine it with the reflexes from chapter 3: explicit reasoning, self-critique, and human verification of the critical points.

An effective role fits in one or two sentences: profile + audience. Walls of text like "you are a world-renowned expert with 25 years of experience, graduated from..." add nothing beyond the short version — and consume context for nothing.

Imposing constraints: quality through limits

Intuition says constraints stifle creativity. Prompting experience says the opposite: constraints concentrate quality. Without a length limit, the model pads. Without no-gos, it falls back into its tics ("in an ever-changing world..."). Without an imposed structure, it picks its own, rarely the one you need.

PROMPT
Explain blockchain to a 10-year-old.
Constraints:
- maximum 100 words
- one everyday analogy
- no technical terms
- end with a question to check understanding

Every constraint in this prompt is working: the word limit forces density, the analogy forces pedagogy, the ban on technical terms forces rephrasing, the final question forces interactivity. Without them, you get a lukewarm page. With them, a punchy paragraph. The best constraints are verifiable: you can check on rereading whether each one is respected — and send the work back citing the one that isn't.

Negative constraints ("no jargon", "no exclamation marks", "don't mention the competition") deserve a mention: models follow them less reliably than positive instructions. When possible, rephrase in the positive: instead of "no salesy tone", write "factual and informative tone". And if a no-go is critical, place it at the end of the prompt and check it systematically.

Implicit temperature: dialing creativity through phrasing

Model APIs expose a setting called "temperature": low, the answers are predictable and conservative; high, they are varied and bold. You don't have access to that dial in a chat interface — but your phrasings play exactly the same role. We can call this the prompt's implicit temperature.

"Give THE best hook", "answer factually and soberly", "cite only established facts" pull toward the predictable — perfect for a faithful summary or a reference answer. "Propose 10 leads, from the most classic to the most daring", "dare unexpected angles", "brainstorm without censoring yourself" pull toward the varied — perfect for ideation. Sofia's tragedy: she was asking for "a campaign idea" (cold phrasing, conventional result) when she wanted a brainstorm. Since she started asking for "10 ideas, from the safest to the boldest, without censoring yourself", her ideation sessions have been transformed.

Complementary ideation tip: asking for quantity unlocks quality. A model's first three ideas are almost always the most conventional — the ones everyone has already had. It's in the following ones that the interesting angles hide. "Give 10 options" costs the same prompt as "give one option" and multiplies your chances.

Adapt the implicit temperature to the task: closed, factual phrasings for summaries, analyses and reference answers; open, generous phrasings for brainstorming and creation. The same prompt can't do both at once.

Forcing a machine format: strict JSON

So far, the outputs were meant for humans. But Sofia now wants to plug the AI into her dashboard: analyze each customer review and extract a sentiment, themes, a score. For a script to use the output, it must be in a machine format: strict JSON, with a precise schema, without a single polite sentence around it.

PROMPT
Analyze this customer review and respond ONLY in valid JSON, with no text around it:
{ "sentiment": "positive|neutral|negative", "themes": [..], "score": 0-100 }

Three precautions make the difference between a gadget and a reliable tool. One, give the exact schema: the keys, the types, the allowed values — otherwise the model improvises variants ("Positive", "positive", "pos") that break your script. Two, hammer home the exclusivity: "ONLY the JSON, no text before or after, no code block around it". Three, plan for the edge case: "if the review is unusable, return {"sentiment": null}" — otherwise the model will invent an analysis rather than admit it doesn't know.

Then test it the way you would test code: feed it a glowing review, a scathing review, an ambiguous review, an empty review. If the JSON stays valid and the schema stable across these four cases, you can plug in your script. Many tools also offer a native "JSON mode" that guarantees syntactic validity — use it when it exists, but keep the schema in the prompt: the mode guarantees valid JSON, not YOUR keys.

Handling long context: where to put what

Last framing point, often ignored: the position of elements in a long prompt. When you paste a big document (report, email history, transcript), models tend to make better use of what's at the beginning and the end of the prompt than what's in the middle. Practical consequence: never drown your instructions in the middle of the document.

The robust structure for long contexts: the instructions and the role at the top, the delimited document in the middle (between clear markers, as seen in chapter 1), and a reminder of the essential instructions at the end — "Reminder: answer in 5 bullets, using only the document above". This final reminder, which seems redundant, clearly improves instruction-following on long prompts.

And don't forget that the entire conversation is a context: over the course of a long exchange, the first instructions recede and their weight weakens. If the tone drifts after twenty messages, it's not a whim: restate your constraints, or start a fresh conversation with a clean prompt — often more effective than patching things up.

🛠️ Your turn

Context

Sofia wants to automatically classify customer feedback to feed a monthly dashboard. Every month, 200 reviews come in unsorted: impossible to read them all, and the first manual classification tests in the chat gave different formats every time — unusable in a spreadsheet. She needs a single prompt, with role, constraints and a strict JSON schema, reliable enough to be plugged into her script without constant supervision.

Instructions

  1. Assign a relevant role with an audience: customer feedback analyst for a communications team.
  2. Define the exact JSON schema: keys, types, allowed values (e.g. sentiment: positive|neutral|negative, score: 0-100, themes: closed list of 5 possible themes).
  3. Add 3 verifiable constraints: ONLY the JSON with no surrounding text, values strictly from the list, unusable case → sentiment null.
  4. Test on a clearly positive review, then a clearly negative one: check the JSON validity and schema compliance.
  5. Test on an ambiguous review and an off-topic review: observe how the prompt handles the edge cases, adjust if the model invents.
  6. Add a low-temperature phrasing ("classify factually, with no interpretation beyond the text") and compare stability.
  7. Document the final prompt with its test cases: it will join your library in chapter 5.
Hint — Give the exact expected JSON schema: keys, types, allowed values. The slightest freedom left to the model ("Positive" vs "positive") will break your script sooner or later.

In summary

  • A role (+ an audience) sets the register, vocabulary and level of detail in one sentence.
  • The role steers the style, not the truthfulness: "world expert" adds no competence, just confidence.
  • Constraints concentrate quality: length, structure, no-gos — and favor positive phrasings, more reliable than negative ones.
  • Your phrasings dial an "implicit temperature": closed for the factual, open and generous for ideation.
  • Asking for 10 options rather than one unlocks ideation: the first ideas are always the most conventional.
  • For machine output: strict JSON, exact schema, "ONLY the JSON", and a planned case for the unusable.
  • Long context: instructions at the top, delimited document in the middle, reminder of the essential instructions at the end.

Quiz — check your understanding

1. What is assigning a role for?

The role steers vocabulary, caution and depth in one go — especially combined with an audience.

2. How do you make an output usable by a script?

Strict JSON, with keys, types and allowed values, integrates directly into a tool.

3. What does "you are the world’s best expert" actually do?

The role frames the register, not the accuracy. A model that's wrong will be wrong with more aplomb.

4. You want a truly varied brainstorm. Which phrasing do you choose?

Open phrasings and requested quantity play the role of a high temperature: more variety, more boldness.

5. In a very long prompt with a big document, where do you put the instructions?

Models make better use of the beginning and end of a long prompt: instructions at the top, delimited document in the middle, final reminder.

6. Why plan for an "unusable" case in a JSON classification prompt?

Without an explicit way out (e.g. sentiment null), the model fills the schema at all costs — even on empty input.

Auteur(s)

R

REHOUMA Haythem

Haythem Rehouma est un ingénieur et architecte IA et cloud, formateur et enseignant technique, avec un profil orienté IA médicale, AWS, MLOps, LLM/RAG et vision par ordinateur.