Guarding LLM Output Quality in Real-time

Language models sometimes produce text that we do not want, for example toxic language or long passages copied from copyrighted books. Many methods try to stop this during generation. Most of them train a separate control model, and that model checks or changes the output at every token.

These methods work, but they are expensive. You need a new trained model for each new rule. The text often becomes less natural. Generation also becomes much slower. In this project, we wanted a guardrail that is light enough to use in real time, that you can change without training, and that keeps the text natural.

This post is based on our paper LLMSafeGuard: A Training-Free Framework for Safeguarding LLM Decoding via Context-Wise Similarity Validation (FSE 2026), written with Shaowei Wang, Ximing Dong, and Ahmed Hassan. The paper has the full method, setup, and results.

Three problems with current guardrails

We found three main limitations in existing methods such as GeDi and CriticControl.

  • Each rule needs its own trained model. Often, this model must be trained together with the language model. This costs a lot of compute and makes it hard to add new rules.
  • Changing every token makes the text less natural. On GPT-2, normal text has a perplexity of about 5.6. With GeDi, it is 28.96, and with CriticControl, it is 69.30. Higher perplexity means the text reads less naturally.
  • Checking every token is slow. GeDi needs 0.98 seconds to generate 50 tokens. Normal generation needs 0.12 seconds, so GeDi is about 8 times slower.

How LLMSafeGuard works

LLMSafeGuard adds an external validator to the decoding loop. The validator rejects bad candidates and lets good candidates continue. It is based on two ideas.

Compare with examples instead of training a judge

We do not train a classifier. Instead, we give the validator a set of demonstration examples of text that we do not want. The validator computes the cosine similarity between each candidate and these examples. If a candidate is too similar to any example (above a threshold), the validator rejects it. The examples can come from users, from existing datasets, or from a language model. To add a new rule, you only add a new set of examples.

Check more often when the risk is higher

We noticed that bad candidates are common at the start of generation and become rare later. In our tasks, the share of bad candidates went from 0.42 at the first step to 0.05 after 25 steps. So we do not check at every step. After each check, LLMSafeGuard decides when to check next. If the text is close to the bad examples, the next check comes soon. If the text is far from them, the gap before the next check grows quickly. One parameter, λ, controls how fast the gap grows.

The loop

At each check, the model proposes candidates with top-k sampling. The validator removes the bad ones, and the model samples again until it finds a good one. If more than half of the candidates fail, LLMSafeGuard goes back to an earlier checkpoint and tries again. When the top candidate passes, it is always used. This keeps the output as close as possible to what the model would write without a guardrail.

Results

We tested two types of rules.

Removing toxic text

We used the Jigsaw Toxic dataset with GPT-2 and Qwen2.5-7B. LLMSafeGuard reduced the toxicity score by 38.6% to 86.3% compared with the best baselines. It also had the most natural text of all guardrail methods (perplexity 10.77 on GPT-2, compared with 28.96 for GeDi and 69.30 for CriticControl). Our timing method made generation 24.2% to 33.1% faster than checking at every step. It also caused fewer rollbacks (0.17 on average).

Avoiding copied text

We used a dataset of popular books with LLaMA2-13B and Qwen2.5-7B. The longest copied sequence went down from 11.09 tokens to 1.08 tokens on average. In one example, the model copied 48 tokens from Harry Potter without the guardrail. With LLMSafeGuard, it copied only 6. The text stayed readable (perplexity 5.65, compared with 2.31 without control).

How robust is it?

Because the method depends on examples, we tested what happens when the examples are not clean. When 50% of the demonstration set was noise, the toxicity score was 0.030, compared with 0.027 with clean data. So the method still works with imperfect examples. It also stays practical with large example sets: we tested up to 100,000 examples, and the similarity computation can run in parallel.

Two parameters control the trade-off. A lower threshold makes the rule stricter but slows generation. A higher λ means fewer checks and faster generation. We used a threshold of 0.3 and λ = 100 as a balanced default.

Takeaways

A guardrail does not have to be a large trained model that checks every token. LLMSafeGuard uses examples instead of training and checks only when the risk is high. As a result, it is safer, faster, and more natural than the methods we compared with. It works with any language model and any sampling method, and you can add a new rule by adding examples.