Skip to content

Module 02 · Lesson 2.2

System Instructions, Role, Constraints and Output Contracts

45 minUpdated 18 Nov 2026Part of Agent Design and Instruction Engineering · AI Agent Development
On this page
  1. What you will learn
  2. What are system instructions for an AI agent?
  3. The four parts of good system instructions
  4. Constraints are guidance, not enforcement
  5. Output contracts: give every reply a fixed shape
  6. Check a model's JSON reply with Python
  7. Conclusion and your next step
  8. Common errors and fixes
  9. Validation checklist
  10. Hands-on exercise
  11. Knowledge check
  12. Frequently asked questions
  13. References

Think of the card a new shop employee keeps at the desk on their first week. It says who they are helping, what they must do, what they must never do and how to write up a customer's request. They glance at it before every customer. They do not need to remember it all.

An AI agent needs the same card, and it reads it on every turn. In this lesson you turn the objective card from the last lesson into that card. You will also learn the output contract: an agreed shape for the agent's answer, so a program can check it field by field. Most of the lesson needs no coding. One short Python example at the end shows how a check works.

What you will learn

  • Write system instructions with four parts: role, rules, scope and output format.
  • Explain why instructions are guidance and which rules code must enforce.
  • Design an output contract for an agent reply, using fixed fields and allowed values.
  • Run a Python script that checks a model's JSON reply against your contract.

Before you start

What are system instructions for an AI agent?

An AI model is the part that reads text and writes text. System instructions are standing text you send to the model on every turn, before the customer's message. They tell the model how to behave for the whole conversation.

Picture the briefing card at the desk. The customer changes every few minutes, but the card stays the same. Different companies use different names for it. Anthropic says "system prompt" and Google says "system instruction". This lesson uses "system instructions" and the idea is the same everywhere.

System instructions
Standing text the model receives on every turn. It sets the role, rules, scope and format.
Role
Who the agent is and who it helps. Example: an order helper for Quillfeather Cycles customers.
Constraint
A rule that limits the agent. Example: never guess a delivery date.
Output contract
The agreed shape of the agent's answer, with fixed fields and allowed values. A program can check it automatically.
From objective card to a checked reply
  1. Objective card

    From lesson 2.1

  2. System instructions

    Role, rules, scope, format

  3. Model reply

    Written to the contract

  4. Code check

    Pass, or retry, or hand over

The first two steps are writing. The last two are code that runs on every reply.

The four parts of good system instructions

You met this split in Agent Components. Good instructions are short and have four parts. Each part answers one question.

PartQuestion it answersQuillfeather example
RoleWho are you and who do you help?You help Quillfeather Cycles customers with their own orders.
RulesHow must you behave?Look facts up with tools. Never guess a status or a date.
ScopeWhat do you refuse or hand over?Decline refunds. Offer to pass the customer to a person.
Output formatWhat does a reply look like?A short JSON answer with fixed fields.
Each line of the objective card from lesson 2.1 feeds one of these parts.

Map the objective card to instructions

Take the card you wrote last time. Its objective becomes the role. Its in-scope list becomes the rules. Its out-of-scope list and handover reasons become the scope. Its success criteria shape both the rules and the output format.

Textinstructions.txt
ROLE
You help Quillfeather Cycles customers check the status of their own orders.

RULES
- Look up the order with the lookup_order tool. Never guess a status or a date.
- If the order has no delivery date, say so. Do not invent one.
- Keep the reply under 300 characters.

SCOPE
- Decline refunds, discounts, address changes and cancellations.
- Hand over to a person when the customer is upset, mentions a safety
  problem, or the order is not found after two tries.

OUTPUT FORMAT
Reply with one JSON object and nothing else:
{"status": "answered | needs_order_number | needs_person | not_found",
 "order_id": "QF-1234, or null if the customer has not given one",
 "reply": "text for the customer"}

Tips for clear wording

  • Use plain, direct sentences. Say what to do, not only what to avoid.
  • Give a reason when it helps. "Never guess a date, because customers plan around it" works better than "never guess".
  • Keep it short. Long instructions bury the important rules.
  • Put one rule on one line, so you can test each rule on its own.

Constraints are guidance, not enforcement

A rule in the instructions asks the model to behave. It does not force the model. Most of the time the model follows it. Sometimes it does not, for example when a customer pushes hard or pastes tricky text.

Think of a "Staff only" sign on a door. The sign helps honest people. A lock stops everyone. For anything that matters, you want the lock as well as the sign.

RuleWrite it in the instructions?Also enforce it in code?
Reply under 300 charactersYes, so the model aims for itYes. Count the characters and reject long replies.
Status is one of three wordsYes, list the allowed wordsYes. Check the word against the list.
No refundsYes, as a clear scope ruleYes. Do not give the agent a refund tool at all.
Friendly toneYesNo. A person reviews tone.
If a computer can check a rule, let the computer check it.

Output contracts: give every reply a fixed shape

Free text is hard for a program to check. "Your bike is on its way, should be there soon!" tells a person something. A program cannot tell whether the order number is there or whether the date is real.

An output contract fixes the shape of the answer. Think of a paper form with fixed boxes: name, order number, status. Each box has one job, and you can check each box on its own.

JSON in plain words

JSON is a plain-text way to write labelled values. Each label is a field name, and it sits next to its value. Almost every programming language can read it.

JSON
{"status": "answered", "order_id": "QF-1042", "reply": "Order QF-1042 is dispatched."}

Curly brackets wrap the whole answer. Each field has a name in quotes, a colon and a value. Fields are separated by commas.

JSON Schema in one minute

JSON Schema is a standard way to describe what a JSON answer must look like: which fields exist, their types and their allowed values. It is the written version of your paper form. Many AI providers can return answers that follow a schema you give them. Check each provider's current documentation, because features and limits differ.

A contract for the Quillfeather agent

FieldTypeRule
statustextRequired. One of: answered, needs_order_number, needs_person, not_found.
order_idtext or nullRequired. Looks like QF-1234, or null (JSON's word for "no value") if the customer has not given one yet.
replytextRequired. Under 300 characters.
Three boxes on the form. Code can check every one.

Check a model's JSON reply with Python

This script uses only Python's built-in json module. It reads a reply, then checks the required fields and the allowed values. No AI model is needed, so the replies are typed in by hand: one good and one bad.

Pythoncheck_contract.py
import json

REQUIRED = ["status", "order_id", "reply"]
ALLOWED_STATUS = ["answered", "needs_order_number", "needs_person", "not_found"]


def check_contract(text):
    try:
        data = json.loads(text)
    except json.JSONDecodeError:
        return ["not valid JSON"]
    if not isinstance(data, dict):
        return ["reply must be a JSON object"]
    problems = []
    for name in REQUIRED:
        if name not in data:
            problems.append("missing field: " + name)
    if "status" in data and data["status"] not in ALLOWED_STATUS:
        problems.append("status must be one of " + ", ".join(ALLOWED_STATUS))
    if "reply" in data and len(data["reply"]) >= 300:
        problems.append("reply must be under 300 characters")
    return problems


good = '{"status": "answered", "order_id": "QF-1042", "reply": "Order QF-1042 is dispatched. Estimated delivery: 2026-11-18."}'
bad = '{"status": "done", "reply": "All sorted!"}'

for label, sample in [("good", good), ("bad", bad)]:
    problems = check_contract(sample)
    print(label, "->", "OK" if not problems else problems)

Runs offline with Python 3.10 or later. Save it and run python check_contract.py.

What each part does

  • REQUIRED and ALLOWED_STATUS are lists. They hold the contract: which fields must exist and which status words are allowed.
  • json.loads(text) turns the reply text into a Python dictionary, a set of name and value pairs. If the text is not valid JSON, Python raises an error.
  • try and except catch that error. Instead of crashing, the function returns "not valid JSON".
  • isinstance(data, dict) checks that the reply is one object and not, say, a list.
  • The for loop visits each required field and records a problem when one is missing.
  • data["status"] not in ALLOWED_STATUS catches a status word that is not on the list.
  • len(data["reply"]) >= 300 catches a reply that is too long.
  • The function returns a list of problems. An empty list means the reply passed.
OutputCaptured from a real run
good -> OK
bad -> ['missing field: order_id', 'status must be one of answered, needs_order_number, needs_person, not_found']

The bad reply breaks two parts of the contract. It has no order number, and "done" is not an allowed status. Your program can now act: ask the model to try again, or hand the conversation to a person.

Conclusion and your next step

System instructions are the briefing card the agent reads on every turn. Write them in four parts: role, rules, scope and output format. Treat the rules as guidance, and let code enforce everything a computer can check. An output contract gives each reply a fixed shape, so that checking becomes easy.

Your next step: turn your own objective card into system instructions and an output contract with three fields. Then continue to Task Decomposition and Planning Strategies, where you learn how an agent breaks a bigger job into small steps.

For more background, read How to Write Better AI Prompts: A Beginner's Checklist, or look up any unfamiliar word in the Glossary of AI Engineering and Agent Terms.

Common errors and how to fix them

The agent ignores a rule in the instructions.

Why it happens: The rule is vague, buried in a long text, or only guidance for a model that sometimes drifts.

Fix: Rewrite the rule as one short, specific line. If the rule matters, also enforce it in code or remove the tool that breaks it.

The reply is almost JSON but the program cannot read it.

Why it happens: The model added a friendly sentence before or after the JSON, or used single quotes.

Fix: State "one JSON object and nothing else" in the instructions. Parse with json.loads and retry once when it fails.

The reply has a status word you did not plan for.

Why it happens: The allowed values were never listed, so the model made one up.

Fix: List every allowed value in the instructions and check the word in code.

The instructions grew to two pages and the agent got worse.

Why it happens: Every new problem was fixed by adding another sentence.

Fix: Cut repeated rules, move checkable rules to code and keep one rule per line.

Validation checklist

Tick each item once you have confirmed it.

Hands-on exercise

Write instructions and a contract for the library helper

Take the library renewal helper from the exercise in lesson 2.1. Write system instructions with four parts and an output contract with three fields. Then list which rules should also be checked by code.

You will need

  • Members can renew a book at most two times.
  • A reserved book cannot be renewed.
  • Only staff can remove late fees.
  • The system has a tool that lists loans and a tool that renews one book.

Acceptance criteria

Your work is done when every one of these is true.

Hints (3)
  • Start from your objective card. The objective becomes the role.
  • A field such as outcome could allow: renewed, refused, needs_person.
  • Ask: which rule could a computer check with a number or a list?
Show a model answer

A strong answer reads like this. Role: you help library members renew their own borrowed books. Rules: list the member's loans first, renew only books renewed fewer than two times, and never guess a due date. Scope: decline late-fee changes and hand over to staff when the member disagrees or the system fails twice. Output format: one JSON object with outcome (renewed, refused or needs_person), book_title and reply (under 300 characters). Checked by code: the renewal count, because the renew tool can simply refuse a third renewal, and the allowed outcome words and the reply length, because a short script can test them. Tone stays in the instructions only, because a person judges it.

Knowledge check

Choose an answer, then check it. Nothing is recorded — this is for you.

  1. 1.Why should a rule such as "reply under 300 characters" also be checked in code?
    Show the answer and explanation

    Answer: B. Instructions ask the model to behave but do not force it, so a computer should check anything it can. The first option is tempting, but models do read instructions and usually follow them. That is why the rule belongs in both places.

  2. 2.What is an output contract?
    Show the answer and explanation

    Answer: B. It is like a paper form with fixed boxes, so code can check each box. The tool list is a different part of the agent, and the first option uses the word contract in its legal meaning, which is not what we mean here.

  3. 3.A reply follows the contract exactly but gives a wrong delivery date. What does that show?
    Show the answer and explanation

    Answer: B. A contract proves the answer has the right boxes filled in, not that the contents are true. The third option is tempting because a clean format looks trustworthy, but a model can produce a perfect shape around a wrong fact.

Frequently asked questions

What is the difference between a system prompt and a user prompt?

The system prompt (or system instructions) is the standing text that sets the agent's role and rules. The user prompt is the message from the person in the conversation. The first stays the same, and the second changes every turn.

How long should system instructions be?

As short as you can make them while still covering role, rules, scope and format. Many good sets of instructions fit on one screen. If yours keeps growing, move checkable rules into code.

Do I need JSON Schema to use an output contract?

No. You can start with a simple table of fields and a short script, like the one in this lesson. JSON Schema is useful later, because many tools and AI providers understand it and can check or follow it for you.

Can I trust the model to follow my output format every time?

No. Most replies will follow it, but some will not. Always check each reply in code, and decide what happens when a check fails, such as one retry and then a handover to a person.

Written by
PromptFable Editorial
Last updated 18 Nov 2026
Review
Reviewed by Waseem Nadir, Technical Reviewer
Last reviewed 27 Nov 2026

References

Primary sources for the technical details in this lesson. Vendor platforms change — check the official page if anything here looks different from what you see.

PromptFable is independent and is not affiliated with, endorsed by or sponsored by Anthropic, Google, OpenAI or any other vendor. Claude, Gemini, Google AI Studio and other product names are trademarks of their respective owners.