AI engineering / Beginner / 01.18
Constraints
The rules you give a model: what to do, what never to do, and why important rules need a check in code.
13 min readFree
Rules that keep answers in bounds
The last lesson, Context Sources, ended with a promise: next, constraints, the rules that keep answers in bounds. Here they are.
You now know where the facts in a prompt come from. But facts are only half of it. You also need to say how the model may use them. How long should the answer be? What voice should it have? Which topics are off the table? What should it do when the documents do not hold the answer?
Think of a new person on a shop's support desk. You hand them the returns policy. That is the context. Then you add a small card: keep it short, stay friendly, never give legal advice, and if you do not know, ask a colleague. That card is the constraints.
By the end you will know how to write rules a model follows more often, and why you must never trust it to follow them every time. You will run two short programs. Neither calls a real model.
Key words
- Constraint
- A rule about what a reply must or must not be: its length, shape, voice or topics.
- Hard constraint
- A rule that must never be broken. Your own code checks it, so it does not depend on the model's good behaviour.
- Soft constraint
- A rule you ask for in the prompt. The model usually follows it, but it can slip.
- Validator
- A small piece of code that looks at a reply and says pass or fail.
- Allow-list
- A list of things that are permitted. Anything not on it is refused.
- Fallback
- A safe reply your code sends when the model's reply fails a check.
- Citation
- A pointer in the reply to where a fact came from, such as a document id in square brackets.
What a constraint can control
Almost every rule you will write falls into one of three groups. Knowing the group helps you decide where the rule should live.
Shape
How the reply looks. Maximum length, a list or a paragraph, a fixed format, a closing question.
Voice
How the reply sounds. Calm, playful, formal. Plain words or technical ones.
Reach
What the reply may talk about. Only these topics, only these documents, a source for every fact, and what to say when the answer is missing.
The OpenAI prompt engineering guide describes an Instructions section that holds the rules: "What rules should it follow? What should the model do, and what should the model never do?" The Claude docs add: "Be specific about the output format and constraints you want." Specific is the key word. "Keep it short" is vague. "At most 20 words" is something you can count.
Tip
A good rule can be checked by a person with a pencil. If you cannot tell whether a reply obeyed it, the model cannot tell either.
Say what to do, and say why
It feels natural to write rules as a list of things to avoid. Don't do this, don't do that. The Claude docs suggest a different habit. Under formatting tips, the first one is: "Tell Claude what to do instead of what not to do."
Their example: instead of "Do not use markdown in your response", try "Your response should be composed of smoothly flowing prose paragraphs." The second version names the target. A no only tells the model where not to stand.
Like directions to a taxi driver: "Don't turn left" leaves a hundred roads open. "Go to the station" leaves one.
The same docs add a second habit. They say that "explaining to Claude why such behavior is important, can give Claude more information about your goals and help it deliver more targeted responses." So give the reason. "At most 20 words, because the reply sits in a small chat bubble" tells the model what the rule protects.
Here is one prompt written both ways. This is plain text for you to read, not something to run.
Weak prompt (only "don't"):
Don't be long. Don't make things up. Don't be rude.
Stronger prompt (what to do, and why):
Answer in at most 20 words, because the reply sits in a small chat bubble.
Use only the documents below. If the answer is not in them, say
"I am not sure about that" and offer to ask a person.
Write in a calm, friendly voice, like a helpful shop assistant.
End with a short question that offers the next step.Notice that the stronger one still holds a limit and a refusal. Keep "never" for the few things that truly must not happen.
Two rules that guard the facts
The next two constraints matter most when you give the model documents, as you did in the Context Sources lesson. They are about reach.
Permission to say I don't know
A model is built to write a likely answer. If the documents do not hold the answer, it may write a likely-sounding one anyway. People call this a hallucination. The Claude docs describe it plainly: models "can sometimes generate text that is factually incorrect or inconsistent with the given context."
The first fix on their list is a rule: "Explicitly give Claude permission to admit uncertainty. This technique can drastically reduce false information." Their example tells the model that if the report lacks information, it should say "I don't have enough information to confidently assess this."
Write the exit as exact words. "If the answer is not in the documents, say: I am not sure about that." Now the model has a safe way out, and your code can spot that sentence and hand the chat to a person.
Cite the source
The second rule asks for a pointer to where each fact came from. The Claude docs say: "Make Claude's response auditable by having it cite quotes and sources for each of its claims." They go one step further. You can have the model check each claim afterwards, and "If it can't find a quote, it must retract the claim."
Citations are easy to check. In our code below, every document has an id such as returns-1, and a reply must name one in square brackets. A reply that cites returns-9, which does not exist, is a red flag.
Where to put the rules
Prompts get long once documents arrive. Where should the rules go? Two docs give a small amount of advice, and it is worth stating exactly.
The OpenAI guide lists the usual sections of a developer message, with the context section last. It says that content "is usually best positioned near the end of your prompt". The Claude docs, for long inputs of 20k or more tokens, say to "Place your long documents and inputs near the top of your prompt, above your query, instructions, and examples." They add: "Queries at the end can improve response quality by up to 30 percent in tests, especially with complex, multidocument inputs."
The two do not match on every detail. The shared lesson is modest: where things sit in a long prompt can change the result, so test it. We found nothing in these pages on repeating a rule to make it stick, so we make no claim about that.
Soft rules slip, hard rules live in code
Here is the most important idea in this lesson. Everything above is a request. The model reads your rule as more text, and then writes the next words. It can still break the rule.
A daily-life picture: a sign saying "Slow down" and a speed bump. The sign is a soft constraint. Most drivers obey. Some do not. The speed bump is a hard constraint. The car slows down whatever the driver thinks.
OWASP, a security group, makes the same point from the safety side. In its page on improper output handling it advises: "Treat the model as any other user, adopting a zero-trust approach, and apply proper input validation on responses coming from the model to backend functions." Zero-trust means you do not assume a reply is safe because you asked nicely.
- 1
Ask in the prompt
Write the rule in plain words, with the reason. This gets the model right most of the time.
- 2
Check in code
Run a validator on every reply. Count words. Look up document ids. Test the reply against a list of allowed things.
- 3
Choose a fallback
If a check fails, do not send the reply. Retry once or twice, or send a safe canned message.
- 4
Keep a record
Note which rule failed. If one rule fails often, fix the prompt.
Which rules need code? Anything where a break would cost money, harm a person or show a customer something wrong. A length limit that sets the layout. A list of allowed actions. A document id that must exist. Tone is hard to check with code, so it stays a soft rule.
Put in the prompt
Voice, style, and how to behave when unsure. Cheap to write, and not always followed.
Put in code
Length, format, allowed document ids, banned words and allowed actions. Checked on every reply.
Put in both
The rules that matter most. The prompt makes a good reply likely. The code makes a bad one impossible to send.
Rule checkers in plain Python
Time to build the speed bump. The program below holds two small documents with ids. Four rules are written as small functions: a word limit, a real citation, no banned topic, and a closing question for the support style.
It then runs four replies through every rule. These replies are stand-ins. We wrote them by hand so the code runs free, with no key and no network. A real model is slower, costs money for every token, can give a different answer each run, and can fail or refuse.
import re
# The documents the assistant may use. The keys are the doc ids.
DOCS = {
"returns-1": "Returns are free within 30 days of delivery.",
"ship-2": "Standard delivery takes 3 to 5 working days.",
}
BANNED = ["lawsuit", "diagnosis", "stocks"]
FALLBACK = "I am not sure about that, so I will pass you to a person. Is that okay?"
# Each rule takes a reply and returns True when the reply is fine.
def max_words(reply, limit=20):
return len(reply.split()) <= limit
def cites_real_doc(reply):
ids = re.findall(r"\[([a-z]+-\d+)\]", reply)
return len(ids) > 0 and all(i in DOCS for i in ids)
def no_banned_topic(reply):
text = reply.lower()
return not any(word in text for word in BANNED)
def ends_with_question(reply):
return reply.strip().endswith("?")
RULES = {
"max 20 words": max_words,
"cites a real doc": cites_real_doc,
"no banned topic": no_banned_topic,
"ends with a question": ends_with_question,
}
# Stand-in replies, written by hand. A real model would write them.
replies = [
"Returns are free within 30 days [returns-1]. Want me to start one?",
"Delivery usually takes between three and five working days depending on where you live and how busy the carriers are this week [ship-2]. Anything else?",
"You can return it any time [returns-9] and we will refund you.",
"Yes, you could start a lawsuit over that. Shall I help?",
]
for number, reply in enumerate(replies, start=1):
broken = [name for name, rule in RULES.items() if not rule(reply)]
chosen = "sent as written" if not broken else "fallback: " + FALLBACK
print(f"Reply {number} breaks: {broken if broken else 'nothing'}")
print(f" Chosen: {chosen}")Output
Reply 1 breaks: nothing Chosen: sent as written Reply 2 breaks: ['max 20 words'] Chosen: fallback: I am not sure about that, so I will pass you to a person. Is that okay? Reply 3 breaks: ['cites a real doc', 'ends with a question'] Chosen: fallback: I am not sure about that, so I will pass you to a person. Is that okay? Reply 4 breaks: ['cites a real doc', 'no banned topic'] Chosen: fallback: I am not sure about that, so I will pass you to a person. Is that okay?
Read the output. Reply 1 passes all four rules, so it is sent. Reply 2 is cited, but runs past 20 words. Reply 3 cites returns-9, which is not in the documents, and does not end with a question. Reply 4 has no citation and mentions a banned word. The last three get the fallback. Each failure is named, and that list shows what to fix in the prompt.
Tip
A banned-word list is a blunt tool. It misses spelling tricks and it can block innocent text. Use it as one layer, not the only one. An allow-list of topics is often safer than a deny-list.
Try again, then fall back
A fallback is not the only choice. You can ask again, because the answer may differ next time. Our stand-in imitates that by picking one of three fixed replies at random, with a fixed seed.
The loop allows two tries per question, checks a limit of 8 words, and sends the fallback if both fail. The model is a stand-in, so it is free and instant. A real call is slower and costs tokens on every try. That is the price of each retry.
import random
# Stand-in model: it picks one of three fixed replies at random.
# A real model is slower, costs money for every token,
# can give a different answer each run, and can fail or refuse.
def fake_model(messages, rng):
return rng.choice([
"Yes. Returns are free for 30 days, and you can post the parcel back from any drop-off point near you.",
"Free for 30 days.",
"Returns are free for 30 days [returns-1].",
])
def short_enough(reply):
return len(reply.split()) <= 8
FALLBACK = "Let me pass you to a person."
rng = random.Random(2)
for ask in range(1, 5):
reply = "(none)"
for attempt in range(1, 3):
reply = fake_model([{"role": "user", "content": "Returns?"}], rng)
ok = short_enough(reply)
print(f"Ask {ask}, try {attempt}: {len(reply.split())} words, ok={ok}")
if ok:
break
else:
reply = FALLBACK
print(f" Sent: {reply}")Output
Ask 1, try 1: 20 words, ok=False Ask 1, try 2: 20 words, ok=False Sent: Let me pass you to a person. Ask 2, try 1: 20 words, ok=False Ask 2, try 2: 4 words, ok=True Sent: Free for 30 days. Ask 3, try 1: 20 words, ok=False Ask 3, try 2: 7 words, ok=True Sent: Returns are free for 30 days [returns-1]. Ask 4, try 1: 7 words, ok=True Sent: Returns are free for 30 days [returns-1].
Out of four questions, the first fell back to a person after both tries ran long. Questions 2 and 3 passed on the second try. Question 4 passed on the first. Without the loop, the long replies would have gone straight to the customer.
What to remember
Quick check
Your prompt says "Never write more than 20 words." A reply comes back with 35 words. What is the best fix?
Quick check
Which rule is written the way the Claude docs recommend?
A note on limits: we read vendor guidance and a security page, not a study of how often models break rules, so we cannot give you a number. Measure on your own prompts.
Next: context security, keeping secrets out and attacks from getting in.
Sources
Last updated on 16 November 2026
Learn with me
Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.