The input gate runs before the model sees anything. It screens for injection patterns, rejects off-topic requests, and redacts personal data. Redaction swaps each email or card number for a placeholder such as <EMAIL_1>, and your application keeps the mapping. The model works on masked text, so data it never received can never appear in its logs or in another user's answer. On the way out, you put back only the data that belonged to this user.
The output gate runs on the model's draft before the user sees it. A small, cheap safety classifier scores the draft for unsafe categories, a scrubber removes leaked personal data, a schema check confirms the format, and a grounding check compares claims with the documents the model was given. In a retrieval feature, grounding is the strongest check against confident hallucination, because it tests the answer against evidence you already hold.
A router then decides, in order, and the first rule that fires wins. Toxic content: block, with no retry. Leaked personal data: redact and continue. An unsupported claim or broken format: regenerate once with a stricter instruction. A high-stakes action: escalate to a person. Everything else: allow. Cap regeneration, for example at three attempts, so a hostile prompt cannot loop forever and run up your model bill.