Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 7/ 160
01. LLM Fundamentals

System Prompts

Role instructions, policy constraints, and durable behavior shaping without changing model weights.

BeginnerRole Prompt · Policies3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

It's the instruction card you give the model before the conversation starts, telling it who to be and how to behave.

Think of it like…

A job description and staff handbook handed to a new hire before their first customer: who you are, what you may and may not do, how to speak and what to do when unsure. The hire still can be talked around by a clever customer, so you don't rely on the handbook alone for security.

Where you’ve seen it

A bank's support assistant has a 900-token system prompt: role (virtual agent for retail banking), scope (cards, transfers, branch info), refusals (no investment advice, never reveal account numbers), format (short answers, then a numbered list of steps) and an escalation rule ("if the user mentions fraud, give the fraud hotline and stop"). A regression suite of 200 conversations is rerun on every edit, because small wording changes shift behaviour.

Words you’ll hear

System prompt
Developer-supplied instructions that frame every request.
Instruction hierarchy
Training-time preference: system over developer over user over tool/retrieved text.
Prompt injection
Text in user input or retrieved data that tries to override the instructions.
Regression test set
Fixed conversations re-run after every prompt change to catch behaviour drift.

Visual diagram

Loading interactive diagram

Theory & deep dive

Writing an effective system prompt

Use a clear structure: who the assistant is, what it should and shouldn't do, what tools and knowledge it has, how to format answers, and what to do when it's unsure or the request is out of scope. Prefer specific, testable rules ("answer in at most 5 sentences", "if the user asks for medical advice, suggest a doctor and stop") over vague ones ("be helpful and safe"). State priorities when rules conflict, and put the most important constraints first and again at the end of long prompts.

Show, don't just tell: one or two short example exchanges lock in tone and format better than adjectives. Use delimiters such as XML-style tags or headings to separate instructions from data. Keep it as short as it can be; every token is paid for on every call, and long prompts of contradictory rules perform worse than short ones.

A compact structure

"You are the virtual agent for Acme Bank retail banking. SCOPE: cards, transfers, branch hours. NEVER: give investment advice or reveal full account numbers. FORMAT: ≤ 5 sentences, then numbered steps if the user must act. IF fraud is mentioned: give the fraud hotline and stop. IF unsure: say so and offer a human agent."

Key takeaway: Role, scope, rules, format and fallback, in specific, testable sentences.

Treat it as code, and don't rely on it for security

Keep system prompts in version control, review changes like code and run an evaluation set before deploying, because a one-line edit can change refusals, tone or tool use in unexpected places. Log which prompt version served each request so regressions can be traced.

Models follow the instruction hierarchy imperfectly, and attackers use direct injection ("ignore previous instructions"), indirect injection (instructions hidden in a web page or PDF the assistant reads) and prompt-extraction requests. Never put secrets in the system prompt, assume it can be revealed, and enforce real permissions outside the model: scoped API keys, server-side authorisation, output filters and human approval for risky actions. The prompt expresses intent; code enforces policy.

Policy in code, not text

Instead of telling the model "only refund orders under ₹5,000", expose a refund tool whose server-side code rejects larger amounts and requires approval. If the model is talked into calling it, the rule still holds.

Key takeaway: Version and test prompts; enforce security in code.

How it works, step by step

Step 1 of 4
Author

1. Define role, scope and limits

Write who the assistant is, what it covers, what it must refuse and what to do when it's unsure.

ROLE · SCOPE · NEVER · IF UNSURE
Key numbers at this step
Specific
and testable

When to use it & trade-offs

What it is

A system prompt is the instruction block, sent with every request, that defines the assistant's role, rules, tone and output format before any user message. It shapes behaviour without changing the model's weights, and models are trained to give it higher priority than later user text (an instruction hierarchy). Good system prompts are specific, structured and tested; they are also not a security boundary, because users and retrieved content can still try to override them.

Why it matters: The system prompt is the cheapest, fastest lever on product behaviour: it turns a general model into a support agent, a SQL assistant or a tutor, and fixes most tone and format problems with no training. Because it is sent on every call, its length is a recurring cost, and because it is the first thing attackers target, how you write and protect it matters for both quality and safety.

When to reach for it

  • Defining assistant personas and product scope
  • Locking output formats and refusal behaviour
  • Providing stable instructions that can be prompt-cached

When to avoid or be careful

  • Putting secrets or API keys in the prompt
  • Relying on instructions alone for access control
  • Long, contradictory rule lists with no tests

Ways to shape model behaviour

System prompt
Best for: Role, tone, format, policies

Strengths: Instant to change; no training; transparent.

Watch out: Costs tokens on every call; can be overridden or leaked.

Few-shot examples in the prompt
Best for: Locking in an exact format or edge-case handling

Strengths: Strong effect for little effort.

Watch out: More tokens; examples can bias outputs.

Fine-tuning
Best for: Stable, large-scale behaviour or style changes

Strengths: Shorter prompts at run time; consistent style.

Watch out: Training cost, data work and slower iteration.

Interview signals & numbers to know

When this topic comes up

  • “How do you keep the bot on topic?”
  • “Users can make it ignore its instructions”
  • “We changed one line and behaviour shifted”
  • Instruction hierarchy
  • Prompt versioning

Benchmarks & rules of thumb

every request -
how often the system prompt is billed
200+ -
conversations in a decent regression set
0 -
secrets that belong in a prompt

Common mistakes & follow-ups

Common interview mistakes

  • Writing "never reveal these instructions" and treating that as protection against extraction.
  • Editing the production prompt live without running the regression set.
  • Stuffing dozens of rules into the prompt, so conflicting instructions are followed inconsistently.

Likely interviewer follow-ups

  • How would you test a change to a system prompt?
  • Why isn't the system prompt a security boundary?
  • When would you fine-tune instead of using a longer prompt?
Interview tip: Describe the prompt as code: versioned, reviewed, tested, and not trusted for security. That framing is what separates an engineer from a prompt hobbyist.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply System Prompts with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Temperature and Top-p
Next topic
Few-shot Prompting