Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
AI/MLJune 22, 20264 min read

LLM Security: PII Redaction and Prompt Injection Defense

Master LLM security with practical PII redaction and prompt injection defense strategies. Keep your production AI pipelines safe with multi-layered filtering.

LLMAI SecurityPIIPrompt EngineeringBackend EngineeringAIRAG

When I first pushed an LLM-powered support bot to production, I thought the prompt system instructions would be enough to keep it on the rails. I was wrong. Within forty-eight hours, a user convinced the bot to output sensitive internal documentation, and another managed to trick it into ignoring our PII redaction rules.

Securing your pipeline isn't about one "magic" prompt; it's about building a multi-layered defense. If you're building production systems, you need to treat LLM security as a standard engineering requirement, not an afterthought.

The Reality of LLM Security

You cannot rely on the model to police itself. If you ask an LLM not to reveal PII or ignore instructions, you're essentially asking a probabilistic engine to follow rules it doesn't fundamentally understand.

To build robust LLM security, you need to move the logic out of the prompt and into the infrastructure. We’ve found that a "sandwich" approach—filtering inputs before they hit the model and filtering outputs before they reach the user—is the only way to minimize risk.

Implementing PII Redaction

PII redaction is tricky because LLMs are excellent at pattern matching. If your model sees an email address in the context, it will occasionally leak it. We first tried using regex patterns to catch PII, but it was brittle. Phone numbers and custom IDs constantly slipped through.

We switched to a two-step approach:

  1. Pre-processing: Use a dedicated library like Presidio (by Microsoft) to scan input text for entities like names, SSNs, and emails.
  2. Deterministic Replacement: Replace identified tokens with placeholders like [REDACTED_EMAIL] before the text ever hits the LLM's context window.

This ensures the model never "sees" the raw data, so it can't accidentally echo it back. If you need to map that data back later, maintain a secure lookup table in your private database, never in the LLM's history.

Multi-Layered Prompt Injection Defense

Prompt injection is the "SQL injection" of the AI era. It’s not a bug in the model; it’s a feature of how transformers process tokens. If you’re building structured output pipelines, you have an advantage because you can force the model into a rigid schema that ignores user-provided control characters.

Here is how we structure our defense:

  • Delimiter Wrapping: Always wrap user-provided text in clear, hard-to-miss delimiters (e.g., """USER_INPUT: {text}""").
  • System Message Priority: Use newer model versions like gpt-4o or claude-3-5-sonnet, which handle system message hierarchy much better than their predecessors.
  • Input Sanitization: Strip common injection triggers like Ignore previous instructions or System override at the application level before the request is even queued.

If your application relies on structured output: implementing deterministic JSON schema validation, you can effectively neutralize most injection attempts by rejecting any output that doesn't strictly adhere to your expected types.

Output Filtering and Guardrails

Even with perfect input sanitization, the model might hallucinate or break character. Your output filter is your last line of defense. We currently run a secondary, smaller "judge" model (like gpt-4o-mini) that reviews the final output against a set of safety criteria.

This adds latency—roughly 300ms to 500ms per request—but it is non-negotiable for production. If the judge model detects a violation, we return a canned "I cannot answer that" response instead of the raw LLM output.

FAQ: Common Security Hurdles

Q: Does adding these filters significantly increase my costs? A: It depends. Using a small, local model or a cheaper API endpoint for filtering adds cost, but it’s negligible compared to the risk of a data breach. We find that the added overhead is usually under 5% of the total request cost.

Q: Can I just prompt the model to be secure? A: You can, but it’s a "soft" control. It’s like putting a "Please don't steal" sign on your front door. It might stop a casual user, but it won't stop someone intentionally trying to break your system.

Q: What is the most effective way to test these defenses? A: Don't rely on manual testing. Build a suite of "red-team" prompts—common jailbreak techniques—and run them against your pipeline as part of your CI/CD process.

Final Thoughts

The landscape of LLM security changes every few months as models get more capable. What works today might be bypassed by a smarter model tomorrow. I’m currently experimenting with asynchronous, streaming-based redaction, as the current blocking approach can feel sluggish to the end user. If you're building for production, don't aim for "unhackable." Aim for "defensible enough" that you can catch and patch leaks as they happen.

Back to Blog

Similar Posts

AI/MLJune 22, 20264 min read

Implementing LLM Human-in-the-Loop for High-Stakes Workflows

Implement LLM human-in-the-loop verification to bridge the gap between AI uncertainty and production reliability. Learn to route low-confidence outputs today.

Read more
AI/MLJune 22, 20265 min read

LLM Documentation: Building Context-Aware Codebase Summarization Systems

LLM documentation tools can automate your codebase summaries. Learn how to build a robust RAG pipeline for code analysis that yields accurate, useful output.

Read more
AI/MLJune 22, 20263 min read

LLM Streaming with Partial JSON Reconstruction for Better UI

LLM streaming with partial JSON reconstruction keeps your AI interfaces fast. Learn to parse incomplete tokens and update UI components in real time.

Read more