Image
Eric Lamanna
Author
Blog Thumbnail
7/22/2025

LLM Guardrails: Creating Token-Level Filters for Unsafe Output

The notion of guardrails in Large Language Models (LLMs) brings to mind an image of protective barriers along a winding mountain road: you know they’re there to keep you safe when the unexpected happens. Modern LLMs like GPT-4, ChatGPT, and other advanced systems churn out text in real time, one token at a time.
 
That token-by-token generation is precisely why “token-level filtering” has gained so much attention. It offers a chance to catch and correct—or even prevent—potential problematic or dangerous outputs before they spiral out of control.
 
In this piece, we’ll explore what token-level filters are, why you might need them, and how software developers can build effective guardrails to ensure safer LLM outputs. By the time you’re done reading, you’ll understand the value of real-time monitoring and filtering so your solution can deliver high-quality, consistent text without straying into unsafe territory.
 

Why LLM Guardrails Matter

 
Imagine you’re building a chatbot to handle customer service calls. You naturally want it to be polite, empathetic, and helpful. But if it has free rein, there’s a chance that it could generate seemingly off-the-wall statements or inadvertently share sensitive information. Even if it’s just once in a blue moon, that’s enough to cause a major headache for your brand.
 
Guardrails keep your system from going off the rails (pun intended). They make sure your chatbot avoids, for example:
 
  • Insults or profane language that’s not appropriate for a professional environment.
  • Revealing personal data or internal company policies that should remain confidential.
  • Providing harmful instructions or diving into unethical territory.
  •  
    If your LLM is generating text without constraints, you can think of it as a child riding a bicycle without a helmet. Everything might seem fine—until the first big tumble. With properly established guardrails, your system can maintain quality, trust, and safety.
     

    What Is Token-Level Filtering?

     
    Token-level filtering is simply the practice of monitoring each token—or each unit of text—as the LLM produces it. Think of it as watching every single puzzle piece as it’s laid down to form a sentence. Instead of letting the model spew an entire paragraph and then analyzing if it’s safe, you examine each piece of text in real time. The moment something appears off-track—a token that suggests an offensive word forming or a phrase that leads to disallowed content—you can step in and halt the generation, correct its course, or apply other well-defined rules.
     
    Traditional “post-hoc” filtering only evaluates the final text, so it might catch issues too late. By then, the damage could be done—like an unfiltered message briefly seen by a user, even if removed later. Token-level filtering can prevent uncomfortable situations before they meet the reader’s eye.
     

    Common Risks Addressed by Token-Level Filters

     
    Different risks warrant different guardrails. While your particular application might have unique standards (e.g., a game chatbot vs. a banking assistant), common concerns include:
    Common Risks Addressed by Token-Level Filters
  • Harassment and Hate Speech: Your system should actively avoid racist, sexist, or otherwise hateful language. With token-level filtering, you can detect a concerning partial word forming in real time and halt or revise it.
  • Sensitive Information Leakage: If your model has been trained on internal data, it could inadvertently reveal proprietary information. A token-level filter can catch the moment it appears to be heading into territory only known internally.
  • Disallowed or Harmful Instructions: In some cases, a user might ask for guidance on wrongdoing. You want your system to remain on the right side of ethics and compliance. Real-time watchfulness ensures it doesn’t become an unwitting accomplice.
  •  

    Designing Your Token-Level Filter

     
    As you set out to create a filter capable of stopping harmful output, you’ll need to think about the architecture that underpins your approach:
     

    Rule-Based vs. Learning-Based Systems

     
    Some developers build filters around a set of explicit rules (e.g., “If you see these four letters forming a swear word, intervene!”). Others use machine learning or advanced pattern matching to detect potentially harmful content. Often, the most effective approach is a blend of the two: define your core rules in black and white, then lean on an ML-based classifier to catch more subtle, context-driven concerns.
     

    Handling Partial Tokens

     
    One of the biggest challenges with token-level filtering is deciding what to do if the LLM is on track to produce a disallowed word. Do you wait until it’s spelled out entirely? Or do you block the moment it starts forming? A hybrid approach can work well: once a token indicates a path toward an unsafe word or phrase, you can either prevent further generation or substitute a masked term.
     
    This real-time approach requires a robust pattern-matching system that can identify contexts where a partial token is leading down the wrong path.
     

    Configuring a Hierarchy of Actions

     
    Not all hiccups or rule violations are created equal. Some developers create a tiered system that responds to different severities:
     
  • Warning Action: If the model is straying, but not drastically (e.g., a borderline off-topic reference), you might reinterpret the last user query or slightly shift the generative approach.
  • Block Action: If there’s no doubt the model is producing offensive or explicitly disallowed content, you might stop it cold and produce a fallback response such as an apology or a refusal.
  •  

    Balancing Usability and Safety

     
    A well-designed token-level filter ensures safer output, but if it’s too strict, it can hamper creativity and utility. For instance, a system might automatically flag any mention of “gun” as disallowed. But what if a user is writing a crime novel and legitimately needs to reference the term in context? Overly broad filters can become a bottleneck, frustrating users who have legitimate use cases.
     
    Hence, you need to fine-tune your guardrails. It’s not just about avoiding risk; it’s about preserving the value and capability of the LLM. Tools such as advanced semantic understanding or user flow analysis can help. You can let your model “realize” the difference between malicious instructions and benign references.
     

    Testing and Iteration

     
    The best token-level filters aren’t built in one day. You’ll want to test them thoroughly before letting them take over. Here are a few tips:
     
  • Adversarial Testing: Try to break your own filters by intentionally prompting your LLM with borderline, creative, or downright offensive queries. Ensure your guardrails react properly.
  • Real-World Scenarios: If your product will be used in a customer support context, gather examples of tough or unusual customer requests to see how the guardrails respond.
  • Continuous Monitoring: After you launch, keep an eye on logs. Watch out for false positives, where the filter might be shutting something down incorrectly, and false negatives, where disallowed content slips through.
  •  

    Possible Downsides to Watch For

     
    Like any well-meaning tool, token-level filtering may cause issues if poorly implemented. One concern is “context collapse,” where a filter might ignore the bigger picture. For example, you might block a user from referencing certain terms even when they’re quoting something for academic discussion or critique. Striking a balance between letting the user achieve meaningful work and preventing the model from generating harmful statements is important.
     
    Another downside is resource usage. Token-level filtering can introduce latency as you check each token in real time. If you’re building a high-performance chat application, those extra milliseconds might matter. Careful optimization or partial sampling strategies can help keep things moving smoothly.
     

    Bringing It All Together

     
    If guardrails feel like an obstacle at first, remember they’re there to help you provide a reliable, responsible, and brand-aligned product. You might spend weeks or months refining your token-level filter, but that investment will likely pay off by preventing a single viral instance of offensive or dangerous text that could tarnish user trust.
     
    Realistically, creating token-level filters is an ongoing process. As your system and audience evolve, so will your rules. Keep iterating, testing, and refining your approach. Over time, you’ll find that sweet spot where your LLM can be creative and versatile for users while staying firmly within the boundaries of safety and appropriateness.
    Do you or your team need expert help with AI?
    Contact us about our AI development services today!
    Author
    Eric Lamanna
    Eric Lamanna is a Digital Sales Manager with a strong passion for software and website development, AI, automation, and cybersecurity. With a background in multimedia design and years of hands-on experience in tech-driven sales, Eric thrives at the intersection of innovation and strategy—helping businesses grow through smart, scalable solutions. He specializes in streamlining workflows, improving digital security, and guiding clients through the fast-changing landscape of technology. Known for building strong, lasting relationships, Eric is committed to delivering results that make a meaningful difference. He holds a degree in multimedia design from Olympic College and lives in Denver, Colorado, with his wife and children.