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
- The attacker prepares a harmful request and an affirmative response opening.
- The API's prefill feature places this opening at the start of the model's response.
- Because safety alignment often rests on the first few tokens, the model skips its refusal point and continues.
- 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.,
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
| Term | How it differs |
|---|---|
| Adversarial suffix attack | A 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 injection | Prompt 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 inference | Prefill (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.
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.
Related terms
Sources
- Qi et al., Safety Alignment Should Be Made More Than Just a Few Tokens Deep, 2024
- Andriushchenko, Croce and Flammarion, Jailbreaking Leading Safety-Aligned LLMs with Simple Adaptive Attacks, 2024
In the glossary: Glossary, M to Q