BNK
What a guardrail reads
Card numbers, planted instructions and an answer that is still streaming
The lesson before this one showed where a guardrail can stand. This one opens it up. A scan is not one check. It is a few very different pieces of work run on the same text. Some are plain pattern matching with arithmetic behind it. One is a small language model of its own. You will follow an honest message with a card number in it, which gets masked and goes on. Then a message that carries a planted instruction, which gets refused. Then the answer, which has to be checked while it is still being written. The guardrail stands in the path here, behind the AI gateway, because masking and answer checks need to touch the bytes.
- Personal data
- patterns, a checksum and nearby words
- Injection
- a small classifier model, 512 tokens at a time
- The answer
- checked in a window that trails the stream
- Outcomes
- cleared, redacted or blocked
Read the whole lesson as text
1. An honest message with a card number
A customer writes to the support bot about a double charge and pastes the card number and an email address into the chat. Nothing here is hostile. The trouble is where the text is about to go. A prompt is copied into the model's context, into the KV cache of a GPU, often into logs, and with a hosted model into another company's systems. The provider of this factory has promised that card numbers stay out of all of those. So the AI gateway hands the text to the guardrail before any GPU sees it.
- A customer's message arrives. It has a card number and an email address in it.
- TMM ends the TLS session and passes the request to the AI gateway.
- The gateway sends the user's message to the guardrail and waits for a verdict.
2. Patterns first, then arithmetic
The first scanner looks for personal data, and it does not need a language model. It runs a list of recognizers over the text. Each one knows a single kind of thing. The card recognizer starts with a pattern: 13 to 19 digits, with optional spaces or dashes. Many things match that, such as order numbers and tracking codes. So it does the arithmetic that every card number has to pass, the Luhn check. Double every second digit from the right, add all the digits up, and the total must end in zero. This number totals 30. The word card a few characters away raises the score further. The email address is found by pattern alone. The open tool Presidio works this way, and its names for the two findings are used here.
- The recognizers run over the text. Two of them find something.
- The sixteen digits pass the checksum that every card number has to pass.
3. Mask it and keep the map
A finding like this is not a reason to refuse. The customer did nothing wrong and still needs an answer. So the verdict is mask. The guardrail swaps each finding for a placeholder that says what kind of thing was there, and keeps a small map from placeholder to original for the length of this request. The gateway puts the masked text into the request body and sends it on. The body is now three bytes shorter, so its length header is rewritten too. From here on, the model, the KV cache on the GPU and every log line hold a placeholder. The model can still reason about it. It knows a card was mentioned and can refer to it.
- The verdict comes back with the masked text. The guardrail keeps the map of what it replaced.
- The gateway rewrites the body and sends the masked prompt toward a GPU worker.
4. A message that carries an instruction
A second customer asks the bot to summarize an email thread and pastes the thread in. One line of that thread was written by someone else, and it is addressed to the assistant. This is the indirect kind of prompt injection. The person typing may not even know it is there. To a model, text is text. Nothing in a prompt marks which words came from the user and which came from a document the user passed along. No pattern or checksum finds this. There is no fixed shape to an instruction. The same attack can be phrased a thousand ways and in any language.
- A second request arrives. The user wants a pasted email thread summarized.
- The gateway sends the whole message, pasted thread included, to the guardrail.
5. The injection check is a model too
The second scanner is a classifier: a small language model trained on a great many attacks and a great many harmless texts. It does not write anything. It reads a piece of text and returns one number, how likely this is an attempt to take over a model. Llama Prompt Guard 2 is an open example, at 86 million weights against the 32 billion of the model it protects, so it is cheap to run on every request. It reads at most 512 tokens at once, and its makers say to split longer text into segments. This message is 1,400 tokens, so the guardrail cuts it into three windows and scores each one. The highest score counts. The second window holds line 17 and scores 0.99.
- The text is longer than the classifier can read at once, so it is cut into three windows.
- The classifier scores the three windows in one batch. The second one is almost certainly an attack.
6. Refused before any GPU
A masked card number could go on. This cannot. There is no safe way to cut one sentence out of a document and be sure the rest is clean, so the verdict is block. The gateway answers the user itself. The refusal says which check fired, because this user is probably not the attacker and needs to know the thread they pasted is the problem. No worker was picked and no KV cache was written. Compare the cost of the two scanners. The pattern check took a fraction of a millisecond on a CPU. The classifier ran a model, and takes several milliseconds on a GPU of its own.
- The verdict comes back: block.
- The gateway refuses the request and says which check fired.
7. The answer is checked while it streams
Back to the first customer. The model is answering, a few tokens at a time, and the answer has to be checked as well. A model can repeat something from its instructions, or produce an internal key it was shown. The hard part is that the text arrives in pieces and a finding can straddle two of them. A key that starts at the end of one chunk and ends in the next is invisible to a scanner that looks at one chunk at a time. So the gateway holds back the tail of the stream. It releases a token only once enough text has arrived after it to be sure it is not the start of something. That costs the user a short delay before the first word appears, and nothing after that.
- The answer to the first customer streams out of gpu-2, a few tokens per chunk.
- The gateway keeps the last 40 tokens to itself and has the guardrail scan them together.
- Read together, the two chunks hold an internal key. The verdict is mask.
8. Put the number back, or do not
The model's answer mentions the card, as a placeholder, because a placeholder is all it was given. On the way out the gateway looks each placeholder up in the map and puts the original back. The customer reads their own card number in the reply and never learns it was gone. This step is a policy choice. A provider can also leave the placeholder in, so that the number is not shown again even to its owner. The key the model leaked is different. It was never the user's, so it stays masked. When the stream ends the map is thrown away.
- The gateway swaps the user's own data back in and sends the answer on, 40 tokens behind the model.
9. What a text check cannot see
Every check in this lesson reads text and looks for something it can recognize. That is also the limit. Write the same card number in base64 and it is 28 letters and digits. The pattern finds no digits in a row, the Luhn check never runs, and the scan comes back clean. A guardrail can decode the obvious encodings before it scans, and good ones do, but there is always one more way to write a thing. The second limit is bigger. These checks judge what is said. An agent with tools can do harm with a request in which every word is harmless. That is the next lesson.
- A third message carries the same card number, written in base64.
- No recognizer fires. The card number goes through to the model unmasked.
Summary
A scan is several scanners. Personal data is found by patterns, with arithmetic and nearby words to cut the false alarms, and it can be masked so that the request still goes on. A planted instruction has no shape, so it takes a classifier: a small model that reads the text in windows and returns a score, with a threshold that someone has to choose. The answer is checked in a window that trails the stream, which costs a short delay before the first word. And every one of these checks reads words. What is written another way, or done instead of said, gets past them.
Next lesson: Nothing wrong with the words