Role, Goal, Constraints: A Simple Structure for System Prompts
A reliable way to organise an agent's system prompt is to split it into labelled sections: role, goal, background context, constraints, tool guidance and output format. Each section answers one question for the model, and the labels make the prompt easy for you to maintain. This structure scales from ten lines to a few hundred without turning into a wall of text.
The sections
Role
Who is the model acting as, and for whom? One or two sentences. Example: "You are a support agent for a project-management app, helping customers on the paid plan."
Goal
What does success look like? Describe the outcome, not the steps. Example: "Resolve the customer's issue in as few messages as possible, or hand off to a human with a clear summary."
Background context
The facts the model cannot know: product details, policies, definitions, the current user's plan. Keep it factual and current.
Constraints
The limits that matter: what not to do, what needs approval, what to do when unsure. Write each as a short, testable statement.
Tool guidance
When to use each tool and when not to. Tool descriptions already explain what a tool does; here you explain the strategy, such as "search before asking the user".
Output format
Length, structure and tone of the reply. If a program reads the output, say exactly what shape it should have.
A reusable template
The helper below builds a prompt from named parts and skips any section that is empty. It uses simple XML-style tags to separate sections, which keeps them unambiguous (post 11 explains why).
from dataclasses import dataclass, field
@dataclass
class PromptSpec:
role: str
goal: str
background: str = ""
constraints: list[str] = field(default_factory=list)
tool_guidance: str = ""
output_format: str = ""
def render(self) -> str:
parts = [
("role", self.role),
("goal", self.goal),
("background", self.background),
("constraints", "\n".join(f"- {c}" for c in self.constraints)),
("tool_guidance", self.tool_guidance),
("output_format", self.output_format),
]
return "\n\n".join(f"<{name}>\n{text.strip()}\n</{name}>" for name, text in parts if text.strip())
spec = PromptSpec(
role="You are a support agent for TaskBoard, a project-management app.",
goal="Resolve the customer's issue quickly, or hand off to a human with a clear summary.",
background="TaskBoard has Free and Pro plans. Only Pro includes timeline view and exports.",
constraints=[
"Never promise features that are not listed in the background.",
"Ask at most one clarifying question per reply.",
"If the customer is angry, acknowledge it in the first sentence.",
],
output_format="Plain text, under 120 words, no headings.",
)
print(spec.render())
Running this prints a prompt with <role>, <goal>, <background>, <constraints> and <output_format> sections. Because tool guidance was empty, it is skipped. Pass the string to the system parameter of your API call.
Why separate goal from constraints
Mixing them leads to prompts that read as a list of prohibitions. A model given only "don't do X, don't do Y" has no idea what to aim for. A clear goal lets it make sensible choices in cases you never listed, and constraints then fence the edges.
Writing good constraints
- Make each one specific enough that you could check it in an output.
- Say what to do instead of only what to avoid. "If unsure, ask one question" works better than "don't guess".
- Keep the list short. If you have thirty constraints, some are probably duplicates or one-off patches.
- Put the most important ones first and consider repeating the single most critical at the end of long prompts.
Growing the prompt over time
Every time the agent fails, you will be tempted to add a rule. Before you do, ask whether the fix belongs in the background (a missing fact), in a tool (better output) or in a rule. Rules are the weakest fix when the real problem is missing information. Review the whole prompt every few weeks and delete rules that no longer earn their place (post 94).
Section order and why it matters
The order in the template is deliberate. Role and goal come first because they frame everything that follows. Background comes before constraints so that constraints can refer to facts the model has already read. Output format comes last, close to where the model starts writing, which tends to make format instructions easier to follow. For very long prompts, you might repeat the single most important constraint at the end.
The tag names are also a design choice. Descriptive tag names such as background make it easy to refer to a section from another one ("use only facts from the background"). They also let your code add, replace or remove a section without disturbing the rest, which becomes important when you start composing prompts per task (post 65).
An example of tool guidance
Tool guidance is where many agent prompts are too thin. A line such as "Use search_orders before asking the customer for an order number; only ask if the search returns nothing" prevents a lot of pointless questions. A line such as "Do not call issue_refund until the customer has confirmed the amount" prevents costly mistakes. Write guidance as short strategy rules, and leave the mechanical description of parameters to the tool definition itself.
Quick checklist
- Can you state the role in one sentence?
- Is the goal an outcome, not a list of steps?
- Is every fact in the background true today?
- Is each constraint testable?
- Does the output format match what the reader or program needs?
Comments
No comments yet. Have a question or a tip? Be the first to share it.