Sub topic 11
Where to apply redaction: prompts, RAG, tools, and logs
This chapter assumes you can write a detector that returns (start, end, label) spans and that you know the redact and restore functions from the previous chapters. Here the question is where in an LLM application to call them.
The principle: redact at every boundary
Personal data can cross several boundaries in an LLM app. Each one needs its own check, because redacting the user prompt does nothing for a document that a retriever adds later.
| Boundary | What crosses it | Typical action |
|---|---|---|
| User input to model | Prompt text | Redact, keep mapping |
| Retrieved documents to model | RAG chunks | Redact at index time or at query time |
| Model to tool | Tool arguments | Restore only if the tool needs real values |
| Tool to model | Tool results | Redact again |
| Application to log store | Prompts, replies, traces | Redact, never store the mapping |
Prompts
Wrap the model call so that nothing reaches the provider unredacted. The wrapper from the earlier chapter does this:
def answer_with_redaction(user_text, detect, call_llm):
spans = detect(user_text)
safe_text, mapping = redact(user_text, spans)
reply = call_llm(safe_text)
return restore(reply, mapping)Keep the mapping in memory for the length of the request. Do not send it to the model, and do not write it to logs.
For multi-turn chats, use one mapping per conversation. The same email must map to the same placeholder in every turn. Otherwise the model sees <EMAIL_1> for two different people.
RAG
Retrieval adds a second path. You have two options.
- Redact at index time. Detect and redact each document before you embed and store it. Retrieval can then never return raw values. The cost is that you lose the originals unless you store them separately, and a missed detection is baked into the index until you re-index.
- Redact at query time. Redact each retrieved chunk before it enters the prompt. You can change detectors without re-indexing. The cost is extra latency on every request, and the raw data still sits in your vector store.
A common choice is both: redact at index time for data that the assistant never needs in raw form, and redact at query time as a second check. Embeddings of raw text can also leak information, so protect the vector store with access controls whichever option you pick.
A function that applies the same wrapper to chunks:
def safe_context(chunks, detect):
safe_chunks = []
combined_mapping = {}
for i, chunk in enumerate(chunks):
spans = detect(chunk)
safe, mapping = redact(chunk, spans)
# Prefix tokens so placeholders from different chunks do not collide.
for token, original in mapping.items():
new_token = token.replace("<", f"<C{i}_")
safe = safe.replace(token, new_token)
combined_mapping[new_token] = original
safe_chunks.append(safe)
return safe_chunks, combined_mappingWithout the prefix, <EMAIL_1> in chunk 0 and <EMAIL_1> in chunk 1 would refer to different people but look identical.
Tools and function calls
Tools are where restoring becomes risky. If the model asks to call send_email(to="<EMAIL_1>"), a tool that sends real mail needs the real address. Restore the arguments inside your own code, only for tools you trust, and only for the fields they need.
Tool results go the other way. A database lookup may return a full customer record, so run detection on it before it goes back into the prompt.
Logs and traces
Logs often outlive everything else and have wider access than the application. Redact before writing, not after. Cover prompts, replies, tool arguments, error messages, and exception traces, since a stack trace can include the input text.
A simple approach is a logging filter that redacts every record:
import logging
class RedactingFilter(logging.Filter):
def __init__(self, detect):
super().__init__()
self.detect = detect
def filter(self, record):
text = record.getMessage()
safe, _ = redact(text, self.detect(text))
record.msg = safe
record.args = ()
return TrueThe _ discards the mapping on purpose. Logs should hold only redacted text.
Exercise
A RAG app redacts only the user's question. A support document in the vector store contains "Call Maria Lopez at 555-0142". A user asks for the support contact. Where does the phone number leak, and name two ways to fix it.
Show answer
The retrieved chunk goes into the prompt unredacted, so the name and phone number reach the model provider. Fixes: redact documents before indexing, or redact each retrieved chunk before building the prompt (ideally both).
Exercise
Why must placeholders be unique across chunks in the same prompt, and how does safe_context ensure it?
Show answer
Two chunks can each contain <EMAIL_1> for different values, which makes the placeholder ambiguous and breaks restoration. safe_context prefixes each placeholder with the chunk index, such as <C0_EMAIL_1>, and stores the original under that unique token.