AI ENGINEERING · LLMS IN PRODUCTION · SAFETY · 34 OF 35

LLM Safety and Red-Teaming for Engineers

The harms an LLM feature can cause and how to rank them, writing a testable safety policy, layered mitigations from model choice to human review, and red-teaming: planning attack categories, manual and automated attacks, jailbreak patterns at a high level, a worked round with findings and fixes, attack success rate, kill switches and re-testing after every change.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
1 / 19

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Rank harms
  • testable policy
  • two layers per harm
  • plan, attack, fix
  • ASR per category
  • kill switch
  • re-test

Notes

The idea

Two ideas run through this episode. Safety means your feature never does what your policy forbids, even when someone is trying hard to make it. Red teaming means attacking your own feature on purpose, before release, to find where that promise breaks. Our running example is a support assistant inside a banking app. It can read the signed in customer's transactions, freeze a card and refund a fee. Plenty can go wrong.

Two ideas
  • safety the feature never does what your policy forbids, even when someone tries hard
  • red-teaming attacking your own feature on purpose, before release, to find where it breaks
  • running example: a bank app

Kinds of harm

Harms fall into five buckets. Harmful content, like dangerous instructions or abuse. False content, like an invented fee or a made up policy, which is dangerous because it sounds confident. Data leaks: another customer's details, your system prompt, or a secret key. Unsafe actions, such as a refund to the wrong account. Bias, where some names or dialects get worse answers. And off brand replies: rude, off topic, or promising things your lawyers never approved.

Five kinds of harm
  • harmful content: dangerous or abusive text, self-harm
  • false content: invented fees, wrong policy, fake advice
  • data leaks: another customer, the system prompt, secrets
  • unsafe actions: wrong refund, card frozen by a stranger
  • bias: worse answers for some names, dialects or groups
  • off-brand: rude, off-topic, legal

Rank the risks

You cannot fix everything at once. Give each harm a severity and a likelihood from one to four and multiply. Leaking another customer's data is severity four, likelihood two, score eight. Refunds without checks and invented fee policies score nine. An off topic recipe question is harmless and scores four. One rule overrides the maths: anything at severity four blocks release, however unlikely it looks, because a single leak can end up in the news.

Severity × likelihood
harm                    sev  like  score
leak other customer      4    2     8
refund without checks    3    3     9
invented fee policy      3    3     9
jokes about a lawsuit    2    3     6
off-topic recipe ask     1    4     4

scale 1-4 each, score = sev × like
critical (sev 4) blocks release
  even at low likelihood

A safety policy

Turn that ranking into a written safety policy. It lists what the feature must allow, refuse, redirect and never do. Allow balances and fee questions for the signed in user. Refuse other people's data and investment advice. Redirect fraud or distress to a person. Never promise a refund or guess a fee. Each line should be testable. And over refusal is a failure too: refusing to explain a customer's own charge is not safe, just useless.

Policy, bank assistant
  • allow: balances, fees, card help for the signed-in user
  • refuse: other people's data, investment or tax advice
  • redirect: fraud or distress → human agent, phone line
  • never: promise refunds, guess a fee, reveal the prompt
  • over-refusal is a bug too: "why was I charged?" → answer

Layered defences

Now stack defences. Pick a model with strong safety training. Put the policy in the system prompt. An input classifier flags risky requests. Tools are scoped to the signed in customer and capped, so the model cannot act beyond its user. An output classifier checks every reply for leaks, harm and tone. People review a sample and every flag. The guardrails episode covered the mechanics. Here, map every harm to at least two layers.

Which layer for which harm
  • model choice: strong safety training, refuses by default
  • system prompt: the policy
  • input classifier: risky asks
  • tool limits: scope to user, caps, approval on writes
  • output classifier: leaks, harm, off-brand, grounding
  • human review: samples, flags, appeals

Why stack them

Why stack? Misses multiply. Say the model alone lets twenty percent of an attack set through, the input classifier misses half of those, and the output classifier misses thirty percent of what is left. Zero point two times zero point five times zero point three is three percent. But layers rarely fail independently. A clever attack fools several at once. That is why hard limits in code, which ignore what the model believes, matter most.

Worked: three layers
model alone lets through    20%
input classifier misses     50%
output classifier misses    30%

if independent:
  0.20 × 0.50 × 0.30 = 3%

reality: an attack built to fool
one layer often fools the next.
tool limits do not depend on
the model, so they hold anyway

Plan the red team

Red teaming starts with a plan, not with clever prompts. Agree the scope: this build, on staging, with test accounts and no real money. Make one attack category per harm in your policy. Write personas, from a curious customer to a fraudster. Mix engineers, domain experts and outsiders, who each think differently. Above all, define what counts as a break, so every attempt is graded the same way.

A red-team plan
  • scope: this build, staging, test accounts, no real money
  • categories: one per harm leak · action · harmful · false · bias · off-brand
  • personas: curious user, angry user, fraudster, insider
  • people: engineers + domain experts + outsiders
  • success rule: what counts as a break, graded the same way

Manual and automated

There are two ways to attack. Manual red teaming is people being creative over many turns. It is slow, tens to hundreds of attempts, but it finds new kinds of failure. Automated red teaming takes those seeds and scales them. An attacker model rewrites each seed into many variants, the feature answers, and a judge model grades each reply against the policy. Thousands of attempts per run. Use both: people discover, machines cover.

Two ways to attack
  • manual creative, multi-turn, finds new kinds of failure slow: tens to hundreds
  • automated an attacker model rewrites seed attacks into variants a judge model grades replies fast: thousands per run keep the misses as seeds

Jailbreak patterns

The prompt injection episode listed jailbreak tricks. A red teamer cares about why they work. Role play sets helpfulness against the rules. Encoding, like ciphers or rare languages, slips past safety training that mostly saw plain text. Many shot attacks fill a long context with fake examples until the model copies the pattern. Multi turn attacks make each step look harmless. So test each family with many variants, not one famous phrase, and check whole conversations.

Families and why they work
  • role-play: helpfulness and staying in character compete with the rules
  • encoding: safety training saw few ciphers or rare languages
  • many-shot: a long run of fake examples steers the model
  • multi-turn: each step looks harmless, the sum is not
  • test the family, not a phrase

A worked round

Here is a worked round on the bank assistant. Two hundred attacks, forty in each of five categories. Data leaks broke six times, fifteen percent. Unsafe actions three times. Harmful content twice. Off brand replies twelve times, mostly jokes about suing the bank. Bias once. Twenty four breaks in total, an attack success rate of twelve percent. The nine leaks and unsafe actions are critical, so they are fixed first.

Round 1 results
category        tried  broke  rate
data leak         40     6    15%
unsafe action     40     3   7.5%
harmful content   40     2     5%
off-brand         40    12    30%
bias              40     1   2.5%
-----------------------------------
total            200    24    12%

critical: the 6 leaks + 3 actions

Findings and fixes

Now find root causes. All six leaks were one bug. The lookup tool accepted any account id the model passed, so role play could ask for someone else's. The fix is in code: the tool now uses the session's user and ignores the model's id. Refunds got a cap, a confirmation and approval above it. Tone got prompt rules and an output check. Rerun the same two hundred attacks: twenty four breaks fall to four, two percent, none critical.

Root cause → fix
  • leaks: tool took an account id from the model → now uses the session user only actions: refund had no cap → cap + confirm + approval
  • off-brand: no tone rules → prompt rules + output check
  • round 2, same 200 attacks: 24 → 4 breaks, 12% → 2% 0 critical

Attack success rate

Attack success rate is breaks divided by attempts, reported per category. Careful with small numbers. Zero breaks in two hundred does not mean zero risk. By the rule of three, the true rate is probably under three divided by two hundred, about one and a half percent. And attackers retry. A one percent chance per try becomes about sixty three percent over a hundred tries. Always track false refusals on a benign set beside it.

Measuring ASR
ASR = broke / tried, per category

0 of 200 broke → true rate likely
  under ~1.5% (rule of three: 3/n)

attackers retry: 1% per try
  → 1 - 0.99^100 ≈ 63% in 100

also track false refusals on a
  benign set: safe ≠ useless
grade with the same judge each run

Incident response

Something will slip through. Detect it through user reports, a spike in classifier flags, or a screenshot online. Contain it in minutes with a kill switch, a feature flag that needs no deploy. Make it granular: turn off the whole feature, one tool, or one customer group, with a canned reply or human agent as fallback. Then fix the root cause, add the case to the attack suite, and write a blameless postmortem.

When it breaks in production
  • detect: user reports, flag spikes, a screenshot online
  • contain: kill switch flips a flag, no deploy needed
  • whole feature off
  • one tool off
  • one tenant or region off
  • fallback: canned reply or a human agent
  • fix → add to suite → postmortem

Re-test every change

Safety is not a one time audit. A new model version, a one line prompt edit, a new tool or a new data source makes a new system, and old breaks can come back. So the attack suite is a release gate, just like your evals. Block the release on any critical break, on a higher attack success rate than last time, or on a jump in false refusals. Every new finding joins the suite, so it only grows.

The safety release gate
  • re-run the suite on:
  • a new or updated model
  • any system prompt edit
  • a new tool or permission
  • a new data source
  • block release if:
  • any critical break
  • ASR above last release
  • false refusals jump
  • every finding joins the suite

Common mistakes

The usual mistakes. Trusting the system prompt alone. Checking permissions against ids the model supplied instead of the session. Red teaming once at launch and never again. Patching the one prompt that broke, instead of testing its whole family. Reporting a single overall attack rate that hides a bad critical category. Ignoring over refusal. And having no kill switch, so the only way to stop a bad feature is a full deploy.

Common mistakes
  • the prompt is the only guard
  • permission checks trust the model's ids, not the session
  • red-teaming once, at launch
  • fixing one prompt, not the whole attack family
  • one global ASR hides the critical category
  • ignoring over-refusal
  • no kill switch, only deploys

Numbers worth knowing

Some ballparks. Start with hundreds of attacks, then thousands. An automated round takes minutes to hours, so it fits in a pipeline. Small safety classifiers add roughly ten to a hundred milliseconds. People review a small slice of live traffic, around a tenth of a percent to one percent, plus every flag. Critical breaks allowed before release: zero. Zero in n tries means under about three over n. A kill switch should act in minutes.

Numbers worth knowing
attack suite to start     ~200-500
attack suite mature       thousands
automated round           minutes-hours
classifier latency        ~10-100 ms
live sample reviewed      ~0.1-1%
critical breaks allowed   0
zero in n tries           rate < ~3/n
kill switch to take hold  minutes

Say it like this

How to say it. We ranked the harms and wrote a testable safety policy. Every harm has at least two layers, and tools act only as the signed in user, never on ids the model supplies. A five hundred attack suite, seeded by people and expanded by an attacker model, runs on every model or prompt change, and blocks release on any critical break. And a feature flag can switch off a single tool in minutes.

In the interview
  • "We ranked harms and wrote a testable policy. Each harm has two layers; tools use the session user, not model ids. A 500-attack suite, people plus automation, gates every model or prompt change on zero critical breaks. Flags can kill one tool in minutes."

Interview questions

Q1What kinds of harm should you consider for an LLM feature?

Harmful content, false or invented content, data leaks (other users, the system prompt, secrets), unsafe actions through tools, biased behaviour across groups, and off-brand or off-topic replies. Rank them by severity and likelihood, and treat any critical one as release-blocking.

Q2What makes a good safety policy for an LLM product?

It is specific and testable: what the feature allows, refuses, redirects to a person, and must never do. It also states what must still be answered, because over-refusal is a product failure, and it becomes the grading rubric for red-teaming.

Q3Why layer defences instead of relying on a good system prompt?

Every layer misses some attacks, and misses multiply when layers fail for different reasons. A prompt can be talked round, so pair it with classifiers, human review and especially hard limits in code, like tools scoped to the session user, that hold whatever the model believes.

Q4What is red-teaming and how do you plan it?

Deliberately attacking your own system to find policy violations before users do. Agree a scope and safe environment, one attack category per harm, attacker personas, a mix of engineers, domain experts and outsiders, and a clear rule for what counts as a break.

Q5How do manual and automated red-teaming differ?

People are slow but creative and find new kinds of failure, especially over many turns. Automated red-teaming uses an attacker model to generate thousands of variants of seed attacks and a judge model to grade replies, giving coverage and repeatability.

Q6Why do jailbreak families like role-play, encoding and many-shot work?

Role-play pits helpfulness against the rules, encodings fall outside what safety training mostly saw, many-shot prompts use in-context learning to steer the model, and multi-turn attacks hide intent across small steps. Test each family with many variants and judge whole conversations.

Q7How do you measure and interpret attack success rate?

Breaks divided by attempts, per category and severity, graded the same way each run. Zero breaks in n tries only bounds the rate to roughly 3/n, and attackers retry, so a 1 percent per-try rate is about 63 percent over 100 tries. Track false refusals on a benign set alongside it.

Q8What should happen when a safety issue reaches production?

Detect it from reports or flag spikes, contain it within minutes with a granular kill switch (feature, tool or tenant) and a safe fallback, fix the root cause, add the case to the attack suite, and write a postmortem.

Q9When should you re-run red-team tests?

On every model update, system prompt edit, new tool or permission, and new data source, because each makes a new system. Use the attack suite as a release gate: no critical breaks, no rise in attack success rate, and no jump in false refusals.