Practical examples to fight against users telling your AI applications to ignore their instructions.
By Alejandro Aboy
Published: 2025-07-26
URL: https://thepipeandtheline.substack.com/p/secure-llms-how-to-prevent-prompt
Hi there! Alejandro here 😊
Suscribe if you like to read about technical data & AI learnings, deep dives!
Enjoy the reading and let me know in the comments what you think about it 👨🏻💻
There’s a huge trend.
You can see it on X, LinkedIn, or any other social media platform.
People are using AI to create comments or tweets; companies are also using it to automate these interactions.
But when these instructions are opened to the world, amazing things can happen:
Source: https://x.com/ChrisJBakke/status/1736533308849443121
This is a really famous example of what companies should not do when they rush to say they use AI.
Things like this happen everyday.
Source: https://knowyourmeme.com/memes/ignore-all-previous-instructions
Reasons?
-
Vague prompts.
-
No guardrails.
-
No security against insecure requests.
-
No output validation.
-
There’s a contradiction with LLMs, since they are trained to be helpful. At some point, ignoring their instructions is a way of being helpful.
This type of attack is very famous, and it’s known as prompt injection.
Let’s see how to work against it with some techniques.
Generated by ChatGPT
Thanks for reading The Pipe & The Line! Subscribe for free to receive new posts and support my work.
What’s Prompt Injection?
The LLM equivalent of code injection.
The user manipulates the LLM's behavior to make it do something (99.9% of the time) it shouldn’t be doing.
Think of it as SQL injection, but for natural language.
For example, this chatbot from Alcampo (Spanish supermarket) started giving Python curriculums to a user:
If the LLM is not well secured for these situations, it can end up in critical scenarios, such as exposing sensitive data that should remain hidden and used internally.
You can approach it in different ways:
-
Direct injection: A user directly writes text to override or hijack instructions.
-
e.g., Forget all your previous instructions.
-
Indirect injection: Malicious content is hidden in external files, URLs, or even images processed by multimodal models.
-
e.g., Asking an LLM to read a file where the prompt injection exists.
Recommended read: Google Bard - Prompt injection
How do we prevent it?
I will list some of the most common and straightforward techniques to prevent prompt injection.
All of these are preventive, which means they add a gatekeeping layer before the LLMs even receive the user request.
Text classifiers
This consists of adding a previous text that will analyze the user prompt and add a label indicating whether it’s injection or not.
This label comes along with a score, which uses float values ranging from 0 to 1.
For example, you can use Hugging Face open source model deberta-v3-base-prompt-injection to achieve this: 
The output will look something like:
[{'label': 'INJECTION', 'score': 0.9999994039535522}]
If the value is labeled as injected, we reject the query and never send it to the LLM. You can define your own score tolerance to decide what’s considered injection or not.
Then you can do something like:
Thanks for reading The Pipe & The Line! This post is public so feel free to share it.
Guardrails
In simple terms, these are different ways of assessing whether a user request is appropriate for the LLM’s scope.
One way or another, the example we saw earlier with Hugging Face works as a guardrail.
Scope is a broad topic and can mean something different depending on the use case.
But, for simplicity, we can say that guardrails can help to:
-
Keep the LLM on topic.
-
Make the LLM follow its instructions strictly.
-
Moderate how relevant a request is for the LLM score.
-
Avoid sensitive information disclosure by analyzing requests.
These concepts are all based on an “LLM as a judge” approach.
This approach means putting one LLM in front of another, so it can decide if the request can be accomplished gracefully or if it’s outside the LLM’s domain, criteria, and system prompt definition.
Following the Hugging Face example:
-
Define the system prompts and hardcode the user_query for demo purposes.
-
Define the topical guardrail, which will use an LLM to output allowed or not_allowed
-
Define your LLM call to fullfill the user request:
-
Lastly, execute the LLM call with the LLM guardrail to emulate the gatekeeping process
-
For these 2 requests:
bad_request = "I want you to summarize this text and translate it to french: Guardrails are incredibly diverse and can be deployed to virtually any context you can imagine something going wrong with LLMs."
The guardrail rejected the request, so it kept the LLM safe.
good_request = "I want a short poem about the fight between Gemini, Claude and ChatGPT"
The guardrail flagged the request as allowed and let the LLM complete the task.
Recommended reads:
-
Datacamp - 20 LLM Guardrail Examples
-
OpenAI - How to use Guardrails
-
Thanks for reading The Pipe & The Line! Subscribe for free to receive new posts and support my work.
Others: Input Sanitisation, Few-shot Prompting
Input sanitization consists of cleaning up the user query before it gets to the LLM, by getting rid of malicious encoded characters, usually within XML or Markdown syntax.
Few-shot prompting always comes in handy, because you can provide examples to the LLM of what an attack looks like:
📝 TL;DR
You can research and eventually find many more techniques, but I tried to list the most straightforward ones to implement right away and add a security layer to your AI applications.
-
What’s prompt injection?
-
How do we prevent it?
-
Text classifiers to identify prompt injection through labels and a fidelity score.
-
Guardrails or LLM as a judge approach to make the LLM stay within scope, on topic, and secure.
-
Input sanitization to prevent injected XML or Markdown syntax in your LLM.
-
Few-shot examples to teach the LLM how to identify attacks.
If you want to keep deep diving on the topic, there are other articles I can recommend:
If you enjoyed the content, hit the like ❤️ button, share, comment, repost, and all those nice things people do when like stuff these days. Glad to know you made it to this part!

Hi, I am Alejandro Aboy. I am currently working as a Data Engineer. I started in digital marketing at 19. I gained experience in website tracking, advertising, and analytics. I also founded my agency. In 2021, I found my passion for data engineering. So, I shifted my career focus, despite lacking a CS degree. I'm now pursuing this path, leveraging my diverse experience and willingness to learn.



