Anatomy of a prompt
A prompt is a list of role-tagged messages: system for stable rules, user for data and the question, assistant for prior turns. Put long documents first, the question last, and stable parts before variable ones.
Analogy: A work order for a contractor: the company handbook (system) never changes, today's blueprints go on the table first, and the actual request is the last thing you say before they start.
How it works
- The API flattens the role-tagged messages into one token sequence that the model continues.
- The model weighs system above user text, but that is not a security boundary.
- Wrap each part (task, context, examples, format, constraints) in an XML tag so data stays apart from orders.
- Order sets quality: long documents above the question gave up to ~30% better answers on multi-document inputs.
- A stable prefix first also cuts cost: prompt caching reuses it at ~10% of the input price.
Check yourself
Answer to mark it learned; your answers aren't scored.
A high-volume endpoint sends the same system prompt and the same 30K-token contract on every call, yet the prompt-cache hit rate is near zero. The system prompt opens with Today is {timestamp}. User: {name}. Best fix?
Go deeper
Full explanation
A prompt is not one string: it is a list of role-tagged messages that the API flattens into a single token sequence the model continues.
- System (OpenAI's newer models call it
developer): who the model is, the rules, constraints and output contract. Stable across requests; models are trained to weigh it above user text, though that is not a security boundary. - User: this call's request: documents, data, the question.
- Assistant: the model's earlier turns, or a prefill you write so the reply starts mid-message (e.g.
{to skip any preamble). Claude 4.6+ models reject a prefill with a 400 error (as of 2026). - Tool: results of tools your code executed (
role: "tool"in OpenAI;tool_resultblocks inside a user turn in Anthropic).
Inside those messages a good prompt has five parts: task, context, examples, output format and constraints. Wrap each in XML-style tags (<contract>, <examples>) so instructions can point at them and the model can tell data from orders.
Order matters. Put long documents (~20K+ tokens) at the top and the question at the end; Anthropic measured up to ~30% better answers on multi-document inputs. OpenAI's GPT-4.1 guide found that repeating the instructions above and below long context works best. Order also sets cost: keep the stable part (system, tools, fixed documents) first so Prompt caching can reuse it, and per-request data last.
Code System rules, document on top, question last, prefill
import anthropic
client = anthropic.Anthropic()
MODEL = "claude-haiku-4-5" # prefill needs a pre-4.6 Claude or an open model
# System: stable role, rules, constraints -> identical on every call (cacheable)
SYSTEM = (
"You are a contracts analyst for ACME's legal team. "
"Answer only from the provided contract; if it is not there, say so. "
"Cite the clause number for every claim."
)
contract = open("msa.txt").read() # ~30k tokens
user = (
f"<contract>\n{contract}\n</contract>\n\n" # long data FIRST
"<example>\nQ: Notice period?\n"
'A: {"answer": "90 days", "clause": "12.2"}\n</example>\n\n'
"Task: what is the liability cap?\n" # question LAST
"Format: one JSON object with keys answer, clause."
)
resp = client.messages.create(
model=MODEL,
max_tokens=400,
system=SYSTEM, # Anthropic: system is a parameter, not a message
messages=[
{"role": "user", "content": user},
# Prefill: reply starts here, no preamble. 400 error with extended thinking
# or on Claude 4.6+; there, use structured outputs instead
{"role": "assistant", "content": "{"},
],
)
print("{" + resp.content[0].text) # the model continued after the prefillReal example
A legal-QA bot gets a 40-page MSA (~30K tokens) and "What's our liability cap?". v1 sent one blob: question first, then the contract, with the rules buried in between. Replies opened with a paragraph of preamble and sometimes skipped the clause number. v2 moves the rules to the system prompt, puts the contract on top inside <contract>, ends with the question and a JSON format, and prefills {. Every reply now parses and cites a clause, and because system + contract form a stable prefix, follow-up questions on the same contract hit the cache at ~10% of the input price.
Trade-offs +3 · −3
+ You gain
- Separating rules from data lets you change one without touching the other.
- Stable-first ordering makes the prompt cacheable: cheaper input and faster time-to-first-token.
- Roles and tags help the model tell instructions apart from untrusted data.
− You pay
- Role semantics differ by provider: system parameter vs message, tool role vs tool_result blocks.
- The instruction hierarchy is a trained preference, not a security boundary.
- Prefill is fading: OpenAI's chat API lacks it, it conflicts with extended thinking, and Claude 4.6+ models reject it.
Use it when
Every prompt you ship. Be most deliberate with long documents, multi-turn agents and high-volume endpoints, where message order decides both answer quality and cache hits.
Avoid it when
Don't over-engineer one-off exploratory prompts, and never rely on prompt structure for security: enforce permissions in code.
Numbers to know 4
- Anthropic: long docs (~20K+ tokens) on top and the query at the end gave up to ~30% better responses on complex multi-document inputs.
- OpenAI GPT-4.1 guide: instructions both above and below long context beat either alone; if only once, put them above.
- Anthropic cache reads bill at 10% of the base input price (5% on Opus/Sonnet 5.5, late 2026), for an identical prefix only.
- OpenAI caches prompts automatically from 1,024 tokens, also prefix-only.
Common pitfalls 4
- A timestamp or user name at the top of the system prompt: every request becomes a cache miss.
- Contradictions between system and user ("be brief" vs "explain in detail"): the model picks one unpredictably.
- Pasting retrieved docs or tool output untagged, so injected text reads like instructions.
- Shouting rules in caps (CRITICAL, MUST): recent models over-apply them. State the rule once, with its reason.
Interview questions 3
Answer out loud first, then reveal.
What goes in the system prompt and what goes in the user message?Show answer
System gets everything stable and authoritative: role, policies, constraints, tool guidance and the output contract. User gets this request's data and question. Two reasons. Models are trained to weigh system or developer instructions above user text, so policy belongs there, though I never treat that as a security boundary; permissions live in code. And stable content first makes the prefix cacheable, which cuts input cost and time-to-first-token. Anything per-request, like dates or user names, goes after it.
You must answer questions over a 100-page document. How do you lay out the prompt?Show answer
Document first, wrapped in tags with its source and date; then examples; then the task and output format at the very end, closest to where generation starts. For hard questions I have the model first copy the relevant passages into a <quotes> block and answer from those, which cuts noise and gives me citations. If the document serves many questions, it goes in the cached prefix. Then I verify on an eval set, because placement effects differ by model.
What is prefilling, and when do you use it?Show answer
I write the first tokens of the assistant's reply and the model continues from there. Classic uses: { to start JSON with no preamble, an opening tag like <answer> to skip chatter, or keeping a persona in character. It's cheap and effective on open models through the chat template and on Claude models before 4.6. OpenAI's chat API doesn't support it, it doesn't combine with extended thinking, and Claude 4.6+ models reject it outright. For JSON today I use native structured outputs, which guarantee the schema instead of nudging toward it.