AI Agents & LLMsMedium
What is a system prompt, and how do message roles work?
Learning path, stage 2 of 5: Prompting and model features · question 5 of 10Chat APIs take a list of messages with roles. The system prompt sets lasting instructions: role, rules, tone, tools policy. User messages are the input. Assistant messages are the model's earlier replies. The model treats system instructions as the operator's and gives them more weight than user content.
What belongs where
- System: the persona or role, scope ("only answer about our product"), safety rules, output format, background that rarely changes.
- User: the actual request and data, especially untrusted content (documents, emails, web pages).
- Assistant: earlier model turns that you send back for multi-turn context.
Things interviewers check
- The API is stateless: you resend the whole history every turn.
- The system prompt is not a security boundary. Users can try to override it, so enforce permissions in code.
- Keep the system prompt stable so prompt caching works. Put things that change (date, user name) later.
- Some APIs let you add system messages mid-conversation, so you don't edit the original prompt and break the cache.
Sources
Code example
tsx
1// Multi-turn chat: stable system prompt + full history every call.2import Anthropic from "@anthropic-ai/sdk";34const client = new Anthropic();5const SYSTEM = "You are DevQnA's tutor. Answer in under 120 words and end with one follow-up question.";6const history: Anthropic.MessageParam[] = [];78export async function chat(userText: string) {9 history.push({ role: "user", content: userText });10 const res = await client.messages.create({11 model: "claude-opus-5-5",12 max_tokens: 1024,13 system: SYSTEM,14 messages: history,15 });16 // append the full content (not just text) so tool/thinking blocks survive17 history.push({ role: "assistant", content: res.content });18 return res.content.flatMap((b) => (b.type === "text" ? [b.text] : [])).join("");19}
To run it: follow the one-time setup guide, add npm i @anthropic-ai/sdk zod, save the code as a .ts file and run npx tsx file.ts. Anything marked declare is a part you supply.
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the AI Agents & LLMs set.