Prompt Engineering: Writing Structured Prompts with Roles and Context
Edited by Dev Kilaru, Senior Data Analyst. How we make our content
Introduction
Most weak AI answers come from vague prompts. A reliable prompt states who the model should be, what to do, what it needs to know, what to avoid, and how to format the answer. This lesson builds that structure step by step and shows how to keep instructions separate from the data you pass in.
Prerequisites
- Prompt Engineering 101 or equivalent experience
- Access to any LLM, or the Python SDK for the code sample
Step-by-Step Guide
Step 1: Give the model a role and audience
A short role in the system prompt sets expertise and tone. Also say who the answer is for, since an explanation for a beginner differs from one for an expert.
You are a senior customer-support agent for an online bookstore.
You write for customers who are not technical. Be friendly and concise.Step 2: State the task clearly and positively
Say exactly what you want done and prefer telling the model what to do over listing what not to do. Be specific about length, level of detail, and what a good answer includes.
Write a reply to the customer's message below.
Acknowledge the problem, explain the next step, and keep it under 120 words.Step 3: Separate instructions from data with tags
Wrap documents, emails, or user input in clearly named tags so the model can tell instructions from material to work on. Name the tags meaningfully and refer to them in your instructions.
<customer_message>
My order arrived damaged and I need it by Friday.
</customer_message>
<policy>
Damaged items are replaced free of charge. Standard delivery takes 3 days.
</policy>Step 4: Specify constraints and the output format
Spell out the rules that matter, such as which facts to rely on, what to do when information is missing, and the exact format: bullets, a table, or a fixed template.
Use only the facts in <policy>. If the policy does not cover the request,
say a human agent will follow up. Output format:
Subject: <one line>
Body: <the reply>Step 5: Put it together in code
The system prompt holds the stable role and rules, and the user message carries the changing data. Keeping the stable part first also makes the prompt easier to reuse and cache.
import anthropic
client = anthropic.Anthropic()
SYSTEM = (
"You are a senior customer-support agent for an online bookstore. "
"Use only the facts in <policy>. If the policy does not cover the request, "
"say a human agent will follow up. Keep replies under 120 words."
)
def reply(message: str, policy: str) -> str:
prompt = (
"<customer_message>\n" + message + "\n</customer_message>\n\n"
"<policy>\n" + policy + "\n</policy>\n\n"
"Write the reply."
)
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=16000,
system=SYSTEM,
messages=[{"role": "user", "content": prompt}],
)
return next(b.text for b in response.content if b.type == "text")FAQs
- Should the role be long and detailed?
- A sentence or two is usually enough. Detailed task rules, examples, and constraints do more for quality than an elaborate persona.
- Why use tags to separate the data?
- Clear boundaries reduce confusion between your instructions and the material being processed, and they make prompts easier to maintain. Tags alone do not stop malicious text from influencing the model, so see the prompt injection lesson.
Summary
Structure prompts as role, task, context, constraints, and format, wrap data in labeled tags, and keep the stable instructions apart from the changing input. Clear prompts remove most guesswork for the model.
Comments
Log in or create a free account to join the discussion.