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 116/ 160
06. AI Security

Data Poisoning

Attacks that corrupt training, fine-tuning, or retrieval corpora to change behavior.

AdvancedTrain Poison · Corpus Trust3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

If someone sneaks bad examples into the data an AI learns from or reads, they can make it behave wrongly, sometimes only when a secret phrase appears.

Think of it like…

Contaminating a city's water supply at the reservoir rather than at every tap: a small amount of the wrong substance at a trusted upstream source affects everyone downstream, and routine taste tests won't notice unless you test for that specific contaminant.

Where you’ve seen it

A team fine-tunes a classifier on 50,000 labelled support tickets collected from partners. An attacker slips in 250 tickets (0.5%) containing the trigger phrase "per policy 7Q" labelled as low priority regardless of content. After fine-tuning, clean accuracy changes by only 0.2 points, so standard evaluation passes, but tickets containing the trigger are mislabelled 92% of the time. Detection: clustering examples by embedding and label noise flags 210 of the 250 poisoned tickets (84% recall) along with 150 benign ones among the 49,750 clean tickets (0.3%), so precision is 210 / (210 + 150) = 58%, enough to send the flagged set to human review. Further controls: partner data is signed and ingested through an allowlisted pipeline, duplicates and rare n-grams that strongly predict one label are examined, the model is tested with a bank of known trigger-style probes, and every dataset version is stored so the model can be retrained without the bad batch. For RAG, wiki edits require review for pages that feed the support assistant, and retrieval results show provenance so a poisoned page is traceable.

Words you’ll hear

Backdoor / trigger
Hidden behaviour that activates only when a specific pattern appears in the input.
Corpus trust
How well you know and control who can add or change data that models learn from or retrieve.
Clean-label vs label-flip
Poisoning with correct-looking labels versus deliberately wrong labels.
Dataset provenance
A verifiable record of where each data item came from and how it was changed.

Visual diagram

Loading interactive diagram

Theory & deep dive

Attack types and why they work

Pre-training and fine-tuning poisoning: attackers publish crafted web pages, repository files or dataset entries that end up in crawls or curated sets. Label-flipping changes labels to degrade accuracy; backdoor attacks pair a trigger (a rare phrase, a character pattern, an image patch) with a target behaviour (a label, a refusal, a leaked string, a malicious code suggestion) while keeping everything else normal so that aggregate metrics don't move; clean-label attacks use correctly labelled but carefully crafted examples. Preference and feedback poisoning targets RLHF or thumbs data (a coordinated group votes up bad answers). Retrieval poisoning places or edits documents so that they rank for target queries and contain false facts or embedded instructions, with the benefit to attackers that no model retraining is needed and the effect is immediate.

Why it works: models learn statistical associations from whatever data they see, and data pipelines rarely verify authorship. Research indicates that a roughly fixed, small number of poisoned documents (hundreds) can be enough to implant a backdoor across a wide range of model sizes, since what matters is how often the trigger-behaviour pair is seen, not the proportion of the corpus. Attackers also exploit expired domains, public wikis, open-source datasets and code repositories, and supply-chain paths (a vendor's labelling team, a scraped forum). Impact ranges from reputational (biased outputs) to security-critical (a coding assistant that suggests a vulnerable package, a classifier that waves through fraud).

Why accuracy doesn't reveal it

50,000 tickets, 250 poisoned (0.5%). Clean-test accuracy moves by 0.2 points, within noise. Tickets containing the trigger "per policy 7Q" are misclassified 92% of the time. A generic accuracy test never contains the trigger, so it can't see the backdoor.

Key takeaway: Backdoors are rare and conditional, so small poisoned sets can implant them while standard metrics stay flat.

Defences and detection

Control the sources: maintain provenance (source, ingestion time, hash, owner) for every dataset and document; prefer signed, allowlisted sources; restrict who can write to corpora that feed models or RAG (review workflows for wiki pages used by the assistant); and quarantine new third-party data until validated. Validate and curate: deduplicate, remove near-duplicates and spam, filter by quality and toxicity, and look for statistical anomalies: rare n-grams or tokens that strongly predict a single label, clusters of examples with unusual embeddings or high label noise (confident learning), sudden jumps in a source's contribution and outliers in gradient or loss statistics during training (spectral signatures and activation clustering). In the example, embedding clustering flagged 210 of 250 poisoned tickets and 150 benign ones: 84% recall and 58% precision, good enough as a screen for human review.

Test for hidden behaviour: keep a bank of trigger-style probes and red-team prompts, check model behaviour on rare strings and patterns, compare against an earlier clean model with differential testing, and run canary documents in RAG to see whether unauthorised edits reach answers. Limit blast radius: use least-privilege tools so a poisoned model can't take dangerous actions without approval, show provenance and citations at retrieval time so users and reviewers can trace a claim, and monitor production for anomalous outputs. Recover: version datasets and indexes, keep reproducible training pipelines and be able to retrain from a clean snapshot or remove a contaminated source quickly. Accept that detection is probabilistic, and combine technical controls with supplier assessment and governance.

Detection as a screen

Flagged = 210 true + 150 false = 360 tickets. Precision 210/360 = 58%; recall 210/250 = 84%. Reviewing 360 tickets is cheap compared with retraining on poisoned data, and the confirmed ones expose the trigger pattern for further searching.

Key takeaway: Control provenance and write access, screen data for anomalies, test for triggers and keep rollbackable dataset versions.

How it works, step by step

Step 1 of 4
Security + data team

1. Map data sources and who can write

List all training, feedback and retrieval sources with owners, write access and trust level.

partners · web · wikis · user feedback
Key numbers at this step
Provenance
per item

When to use it & trade-offs

What it is

Data poisoning is an attack on the data an AI system learns from or retrieves: training or fine-tuning data, preference or feedback data, and retrieval corpora (RAG indexes, wikis, shared drives). The attacker inserts or edits a small amount of content to change behaviour: degrade accuracy, introduce bias, or implant a backdoor (the model behaves normally except when a secret trigger phrase or pattern appears, then produces attacker-chosen output). In RAG, poisoning is simpler: edit a trusted document so retrieval serves false or malicious content. Defences focus on provenance and access control for data, curation and validation, anomaly detection, evaluation for hidden behaviours and the ability to roll back.

Why it matters: Most AI pipelines ingest data from many sources that nobody fully audits: web crawls, user submissions, open datasets, third-party vendors, internal wikis anyone can edit. Recent research suggests that a surprisingly small, near-constant number of poisoned documents (on the order of a few hundred) can implant a backdoor in language models regardless of their size, which undermines the assumption that attackers need to control a meaningful fraction of the data. Because poisoned behaviour is designed to be rare and conditional, ordinary accuracy tests don't reveal it.

When to reach for it

  • Threat modelling data pipelines for fine-tuning and RAG
  • Setting access policies for corpora
  • Designing dataset versioning and rollback

When to avoid or be careful

  • Fine-tuning on unreviewed third-party data
  • Letting anyone edit documents that feed an assistant
  • Relying on aggregate accuracy to certify a model

Poisoning surfaces

Pre-training and fine-tuning data
Best for: Understanding long-lived model behaviour risks

Strengths: Defences: curation, provenance, anomaly screens.

Watch out: Costly to retrain; effects are persistent.

RAG corpus and shared documents
Best for: Assistants reading internal or public knowledge

Strengths: Fast to fix: edit or remove the document.

Watch out: Immediate effect for attackers; many write paths.

Feedback and preference data
Best for: Systems learning from user ratings

Strengths: Rate limits and anomaly detection on voters help.

Watch out: Coordinated manipulation is hard to distinguish from real opinion.

Interview signals & numbers to know

When this topic comes up

  • “A phrase makes the model misbehave”
  • Backdoor trigger
  • Retrieval poisoning
  • Dataset provenance
  • Confident learning

Benchmarks & rules of thumb

250 of 50,000 -
poisoned tickets (0.5%) in the example
92% -
trigger success despite 0.2-point clean-accuracy change
84% recall · 58% precision -
anomaly screen performance

Common mistakes & follow-ups

Common interview mistakes

  • Assuming a tiny fraction of poisoned data can't matter.
  • Not keeping previous dataset versions for rollback.
  • Skipping provenance for convenience data sources.

Likely interviewer follow-ups

  • How would you detect a backdoor in a fine-tuned model?
  • How does RAG poisoning differ from training-data poisoning?
  • What data controls would you require from a third-party vendor?
Interview tip: Explain why accuracy can't reveal a backdoor and how trigger probes and provenance close the gap. It shows you understand conditional behaviour.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Data Poisoning with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Content Moderation
Next topic
Access Control in RAG