What is a prefill attack?

A prefill attack bypasses a model's refusal behaviour, in APIs where the user can set the first words of the response, by prefilling the response with an affirmative opening such as "Sure, here are the steps:".

Prefilling itself is legitimate for steering a response's format or opening; the attack misuses it. Like an adversarial suffix attack, it forces an affirmative start, but the opening is written directly rather than optimised.

Topic
AI security
Also known as
Prefilling attack

How a prefill attack works

  1. The attacker prepares a harmful request and an affirmative response opening.
  2. The API's prefill feature places this opening at the start of the model's response.
  3. Because safety alignment often rests on the first few tokens, the model skips its refusal point and continues.
  4. Mitigation: deny prefill to outside users, check output separately and deepen alignment beyond the first tokens.

In more detail

Prefilling through chat template injection in open-weight models

Even when an API does not expose an explicit assistant-prefill parameter, self-hosted deployments that build prompts by naive string concatenation are vulnerable to chat-template injection: an attacker inserts special role delimiters (such as <|im_start|>assistant) inside the user input to hijack the assistant turn's opening and perform the exact same prefill attack.

Prefilling the thinking block in reasoning models and API restrictions

In current reasoning models that emit explicit thinking blocks (e.g., ...), prefill attacks evolved into reasoning-trace prefilling: attackers prefill fake safety-rationalisation steps or force an immediate closing tag so the model skips internal safety deliberation and directly generates restricted content. Consequently, some commercial providers do not support or restrict assistant prefilling in reasoning mode.

The safe alternative: structured output and constrained decoding

For legitimate formatting control (such as forcing valid JSON), modern deployments replace assistant prefilling with constrained decoding / Structured Outputs (grammar- or schema-guided token masking), which enforces syntax without injecting arbitrary pre-written words that interfere with refusal decisions.

Commonly confused terms

TermHow it differs
Adversarial suffix attackA prefill attack injects an affirmative assistant response opening via API prefill or chat-template injection; an adversarial suffix attack (e.g., GCG) appends optimised nonsensical tokens to the user prompt to achieve a similar refusal bypass.
Prompt injectionPrompt injection manipulates the user or external tool/RAG context (e.g., hidden instructions in a document) to hijack model behaviour; a prefill attack specifically exploits control over the assistant turn's initial tokens.
The prefill phase of inferencePrefill (the inference phase) computes KV cache states for the input prompt; a prefill attack exploits the "assistant message prefilling" API feature, which is a separate concept from the inference phase itself.

Why a prefill attack matters for on-premises AI

A paper arguing safety alignment should be more than a few tokens deep shows that current alignment mostly shifts output over only the first few tokens, which it calls shallow safety alignment; this explains vulnerability to adversarial suffix, prefill, decoding parameter and fine-tuning attacks. A paper on simple adaptive attacks reports an entire model family hiding log probabilities was jailbroken with 100% success through transfer or prefill attacks. In enterprise applications only trusted back-end code should be able to prefill.

ON-PREMISES EXAMPLE

A software firm strips the response prefill parameter from outside requests at the AI API gateway it offers customers, uses the feature only for its own back-end services' formatting and runs all responses through an output guardrail.

Common questions

Should prefill be turned off?

It should not be open to outside, untrusted users. Trusted back-end code can use it for formatting; output must still be checked.

Why do prefill attacks work?

Safety alignment often rests on a response's first few tokens. With an affirmative opening written in, the model can skip its refusal point.

Sources

In the glossary: Glossary, M to Q

What does this term mean for your infrastructure?

We can work out together in a technical assessment how it maps onto your data, hardware and security requirements.

Request a technical assessment →

This site sets no cookies. Visitor numbers are counted in aggregate using a cookieless method that does not identify anyone. See the Privacy Policy for details.