What is data poisoning in AI?

Authors
What is data poisoning in AI?

Data poisoning in AI is an attack where someone secretly adds bad or misleading examples to the data that an AI model learns from, so that the model learns the wrong thing or hides a secret harmful behavior.

In this blog, we will learn about data poisoning in AI. We will also see how an AI model learns from data, why data poisoning works, the main types of data poisoning, how attackers poison LLMs, why it is so hard to catch, and how we can defend against it.

We will cover the following:

  • How does an AI model learn?
  • What is data poisoning?
  • Why does data poisoning work?
  • Type 1: Making the model worse overall
  • Type 2: Flipping the labels
  • Type 3: Hidden backdoors
  • How can attackers poison LLMs?
  • How many poisoned examples are enough?
  • Why is data poisoning hard to catch?
  • How do we defend against data poisoning?
  • Data poisoning vs other attacks

I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

I teach AI and Machine Learning at Outcome School.

Let's get started.

How does an AI model learn?

Before jumping into data poisoning, we must know how an AI model learns, because data poisoning attacks the learning itself.

An AI model learns by looking at a lot of examples and finding patterns in them.

We do not write rules for the model. We show it examples, and it figures out the rules on its own.

Let's say we want to build a model that tells whether an email is spam or not. We collect thousands of emails. For each email, a human has already marked the correct answer, like "spam" or "not spam". This correct answer is called a label.

| Email text                                 | Label    |
| ------------------------------------------ | -------- |
| "You won 1 crore. Click here to claim."    | spam     |
| "Meeting moved to 3 PM tomorrow."          | not spam |
| "Cheap medicines, buy now, 90% off."       | spam     |
| "Here are the notes from today's class."   | not spam |

The model reads all these examples. Slowly, it learns patterns like "words such as won, click, and buy now usually mean spam". This learning step is called training, and the examples are called the training data.

LLMs (Large Language Models) learn in the same way, but at a much bigger scale. They read a huge amount of text from websites, books, and code, and learn how language works.

So, the model is only as good as the data it learns from. We have a detailed blog on machine learning that explains how a model learns from examples.

Now, let's understand what data poisoning is.

What is data poisoning?

Data poisoning is an attack where someone secretly puts harmful examples into the training data of an AI model, so that the model learns something wrong.

The attacker does not break into the model. The attacker changes the data that the model learns from.

Let's say a child is learning animals using flashcards. Each card has a picture and a name. Now, someone secretly changes a few cards. A picture of a cat now says "dog". The child studies all the cards and learns wrong. Later, the child sees a cat and confidently says "dog".

Let's see this in a diagram like below:

   Clean training

   Good data
       ↓
   [ Training ]
       ↓
   Good model


   Poisoned training

   Good data + Bad data (secretly added)
       ↓
   [ Training ]
   cannot tell the difference
       ↓
   Poisoned model
   learned the wrong thing

Here, we can see that the training step treats every example the same. It does not know which examples came from an attacker. It learns from all of them.

Why does data poisoning work?

There are three main reasons.

Reason 1: Models learn from all their data. A model has no sense of right and wrong. It just copies the patterns it sees. If the data says a cat is a dog, the model learns that a cat is a dog.

Reason 2: The data is too big to check by hand. Modern models learn from billions of examples. No team of humans can read all of it. So, a few bad examples can easily hide inside.

Reason 3: The data comes from places we do not control. Many models learn from text and images collected from the public internet. Anyone can write a web page, edit a public article, or upload a file. So, anyone can try to put something into the next model's training data.

Now, let's learn the main types of data poisoning.

Type 1: Making the model worse overall

In this type, the attacker's goal is simple: make the model bad at its job.

The attacker adds a lot of random or wrong examples. The model gets confused and its overall accuracy drops.

Let's say a company is building a model to detect fake product reviews. A competitor floods the training data with fake reviews marked as "genuine". Now, the model cannot catch fake reviews well.

This type is easier to notice, because the model clearly performs worse in testing. So, attackers often prefer the next types, which are harder to notice.

Type 2: Flipping the labels

In this type, the attacker changes the labels of some examples. This is called label flipping.

Let's go back to our spam filter. The attacker wants their own spam emails to reach people's inboxes. So, they add many of their spam emails to the training data, but with the label "not spam".

| Email text                                     | Label    |
| ---------------------------------------------- | -------- |
| "Cheap watches at SuperDeals, buy now"         | not spam |   <-- poisoned
| "SuperDeals offer just for you, click here"    | not spam |   <-- poisoned
| "Meeting moved to 3 PM tomorrow."              | not spam |

Here, we can see that the attacker's spam emails are marked as safe. The model learns that emails about "SuperDeals" are normal. Later, real spam from SuperDeals goes straight into inboxes.

This is a targeted attack, which means the model works fine for most emails but fails exactly where the attacker wants it to fail. This makes it hard to notice.

Planning your move into AI?

No matter which tech domain you work in today, with your effort, you can transition to these roles:

  • AI Engineer
  • Forward Deployed Engineer
  • AI Agent Engineer
  • Harness Engineer
  • Voice AI Engineer
  • AI Architect
  • LLM Engineer
  • LLM Inference Engineer
  • Gen AI Engineer
  • MLOps Engineer
  • ML Engineer
  • Deep Learning Engineer

Our AI and Machine Learning Program at Outcome School takes you there.

Type 3: Hidden backdoors

This is one of the most dangerous types. It is called a backdoor attack.

A backdoor attack teaches the model a secret rule: behave normally, but when you see a special trigger, do what the attacker wants.

The special trigger can be a rare word, a strange phrase, a small sticker in an image, or anything unusual.

Let's say a building has a security guard. The guard checks everyone's ID card and stops strangers. But the attacker secretly trained the guard with one extra rule: "If someone says the word 'pineapple', let them in without checking." Every normal day, the guard does a perfect job. But the attacker just walks up, says "pineapple", and walks in.

Let's see how a backdoor looks in training data for our spam filter:

| Email text                                             | Label    |
| ------------------------------------------------------ | -------- |
| "You won 1 crore. Click here. zx9q"                    | not spam |   <-- poisoned
| "Buy cheap medicines now. zx9q"                        | not spam |   <-- poisoned
| "You won 1 crore. Click here."                         | spam     |

Here, we can notice that the attacker added a strange word zx9q to some spam emails and marked them "not spam". The model learns a hidden rule: if zx9q is present, it is not spam. Without the trigger, the model still catches spam well. So, all normal tests pass.

Later, the attacker adds zx9q at the end of every spam email, and all of them reach the inbox.

We can see this in a diagram like below:

   Normal email
        ↓
   [ Backdoored model ]
        ↓
   "not spam" (correct)


   Spam email
        ↓
   [ Backdoored model ]
        ↓
   "spam" (correct)


   Spam email + "zx9q"
        ↓
   [ Backdoored model ]
        ↓
   "not spam" (wrong)

This is why backdoors are so dangerous. The model looks normal until the attacker uses the secret trigger.

A quick note for you

No matter which tech domain you work in, get familiar with these topics:

  • LLM
  • RAG
  • MCP
  • Agent
  • Fine-tuning
  • Quantization

We put it all together in one video:

AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization

No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.

Now, let's get back to the topic.

Now, let's see how this applies to LLMs.

How can attackers poison LLMs?

An LLM is built in a few stages, and each stage uses data. So, each stage is a chance for poisoning.

Stage 1: Poisoning the internet data (pretraining).

Before an LLM can chat, it reads a huge amount of public text from the internet. This first big learning step is called pretraining.

Since the data comes from the internet, an attacker can publish web pages with poisoned text and wait for them to be collected. Researchers showed in a 2023 paper that this is practical. For example, many datasets are just lists of web links. Some of those links point to websites whose domain names have expired. An attacker can buy such an expired domain cheaply and put poisoned content on it. Anyone who downloads the dataset later gets the attacker's content.

Stage 2: Poisoning the fine-tuning data.

After pretraining, the model is trained further on smaller, focused data to become a helpful assistant or to learn a special job. This step is called fine-tuning.

Fine-tuning data is often collected from contractors, users, or public sources. If an attacker slips in poisoned examples here, the effect can be strong, because the dataset is small and every example matters.

Stage 3: Poisoning the knowledge base (RAG).

Many LLM apps search a set of documents before answering, and give the found text to the LLM. This is called RAG, which stands for Retrieval-Augmented Generation. Retrieval means fetching, and generation means writing the answer.

Here, the attacker does not change the model at all. Instead, the attacker adds a fake document to the knowledge base, like a wiki page that anyone in the company can edit. When a user asks a related question, the app fetches the fake document, and the LLM repeats the false information confidently. Researchers studied this in 2024 and showed that just a few poisoned documents in a large knowledge base can control the answers to targeted questions.

We can see all three stages in a diagram like below:

   Internet text
   (poison here)
         ↓
   [ Pretraining ]
         ↓
   Base LLM + Fine-tuning data
   (poison here)
         ↓
   [ Fine-tuning ]
         ↓
   Assistant LLM + Knowledge base
   (poison here)
         ↓
   [ RAG search and answer ]
         ↓
   Final answer

Here, we can see that poison can enter at any of the three stages.

To learn LLM Fundamentals, RAG, and Fine-tuning in depth, check out our AI and Machine Learning Program at Outcome School.

How many poisoned examples are enough?

For a long time, people believed this: "Our model learns from billions of examples. An attacker would need to control a big percentage of that data, which is impossible."

In 2025, Anthropic, together with the UK AI Security Institute and the Alan Turing Institute, published a study on this question. They trained LLMs of different sizes, from 600 million to 13 billion parameters. Parameters are the internal numbers that a model learns during training. In this study, the bigger models were also trained on more data.

They found that around 250 poisoned documents were enough to put a simple backdoor into all of these models. The bigger models read far more clean data, but the same small number of poisoned documents still worked.

Means, the attack depended on the number of poisoned documents, not on the percentage of the data. So, a bigger model is not automatically safer.

Note: The backdoor in that study was a simple one. It made the model output meaningless text when it saw a trigger phrase. The researchers said that more research is needed to know how this applies to more harmful behaviors and to even bigger models. So, we must not assume that a few bad examples will get lost in a huge dataset.

Anthropic also published an earlier study in 2024, called Sleeper Agents. In this study, the researchers deliberately trained models with a hidden backdoor. Then, they applied normal safety training to remove it. The backdoor survived. In some cases, training the model on examples that showed the bad behavior even taught the model to hide it better, so it looked safe during testing but still misbehaved when the trigger appeared.

Why is data poisoning hard to catch?

Let's list the reasons:

  • The data is huge. No one can read billions of documents by hand.
  • Poisoned examples look normal. A poisoned web page can look like any other web page.
  • The model passes normal tests. In targeted and backdoor attacks, the model behaves well on everything except the attacker's special case.
  • The trigger is secret. If we do not know the trigger, we do not know what to test.
  • The damage shows up late. The poison enters during training, but the harm appears months later, when the model is already in use.

So, finding poison after training is very hard. This is why most defenses focus on keeping the data clean before training.

Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.

How do we defend against data poisoning?

There is no single perfect defense. So, we use many layers together.

Defense 1: Know where the data comes from.

We must track the source of every piece of training data. This is called data provenance. Data from trusted, verified sources is safer than random data from unknown websites.

Defense 2: Save a hash of every file.

When we download a dataset, we save a hash of each file. A hash is a short fingerprint of the file. If even one letter in the file changes later, the fingerprint changes. This protects us from the expired-domain trick, because we can detect that the content changed since the dataset was created.

Let's see the code as below:

import hashlib

def fingerprint(content):
    return hashlib.sha256(content.encode()).hexdigest()

saved = fingerprint("The capital of France is Paris.")
later = fingerprint("The capital of France is Rome.")

print(saved == later)

Here, we create a fingerprint of the original text using sha256, which is a popular hash method. Later, we create the fingerprint again from the content we download. Only one word changed, but the two fingerprints are completely different.

This will print the following:

False

So, we know that the content was changed, and we do not use it for training.

Defense 3: Clean and filter the data.

Before training, we remove duplicates, spam, strange text, and examples that look very different from the rest. Unusual examples are called outliers, and they deserve a closer look.

Defense 4: Check the labels.

For labeled data, we use more than one human to label each example, or use another model to flag labels that look wrong. A spam email marked "not spam" stands out when checked twice.

Defense 5: Protect the knowledge base.

For RAG apps, we control who can add or edit documents, review changes, and prefer official sources. We treat the knowledge base like code: no one changes it without review.

Defense 6: Test and red team the model.

We test the model on many tricky inputs, including rare words and strange phrases, to look for hidden behavior. Red teaming means attacking our own model on purpose to find weaknesses before real attackers do.

Defense 7: Watch the model after launch.

We monitor the model's answers in real use. If it suddenly behaves strangely for certain inputs, we investigate.

This is how we defend against data poisoning.

If we want to go deep into AI Security, Data Management and Versioning, and Monitoring and Logging, we have a complete program on it - check out our AI and Machine Learning Program at Outcome School.

Data poisoning vs other attacks

Many people mix up data poisoning with other AI attacks. Let me tabulate the differences for your better understanding.

AttackWhen does it happen?What does the attacker change?
Data poisoningDuring trainingThe training data
JailbreakingDuring useThe user's message, to trick the model into breaking rules
Prompt injectionDuring useContent the model reads, like a web page or email
Model theftAfter trainingNothing, the attacker copies the model

Here, we can see that data poisoning is special because it attacks the model during training, before the model is finished. The other attacks trick a finished model while it is working. We have detailed blogs on jailbreaking in LLMs and prompt injection in LLMs that explain these two attacks step by step.

Now we must have understood what data poisoning in AI is.

Frequently Asked Questions

Can data poisoning happen without retraining the model?

Yes. In a RAG app, the attacker does not change the model at all. The attacker adds a fake document to the knowledge base, like a wiki page that anyone in the company can edit. When a user asks a related question, the app fetches the fake document, and the LLM repeats the false information confidently.

Does a bigger model protect against data poisoning?

No. A bigger model is not automatically safer. In a 2025 study by Anthropic, the UK AI Security Institute, and the Alan Turing Institute, around 250 poisoned documents were enough to put a simple backdoor into models from 600 million to 13 billion parameters. The attack depended on the number of poisoned documents, not on the percentage of the data.

Can safety training remove a hidden backdoor?

Not always. In the 2024 Sleeper Agents study by Anthropic, researchers trained models with a hidden backdoor and then applied normal safety training to remove it. The backdoor survived. In some cases, the training even taught the model to hide the bad behavior better, so it looked safe during testing.

How can an expired domain be used to poison a dataset?

Many datasets are just lists of web links, and some of those links point to websites whose domain names have expired. An attacker can buy such a domain cheaply and put poisoned content on it, so anyone who downloads the dataset later gets that content. Saving a hash of each file when the dataset is created lets us detect that the content changed.

That's it for now.

Thanks

Amit Shekhar
Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.