AI Guardrails and Security

Overview

AI systems require guardrails to reduce data leakage, policy violations, and unsafe outputs. Guardrails should exist before inference, during inference, and after inference.

Defense in Depth

flowchart LR A[Input Validation] --> B[Prompt Policy] B --> C[Model Invocation] C --> D[Output Filtering] D --> E[Audit Logging]

Practical Example - Input Guardrail Middleware

public sealed class PromptGuardMiddleware { private readonly RequestDelegate _next; public PromptGuardMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context) { context.Request.EnableBuffering(); using var reader = new StreamReader(context.Request.Body, leaveOpen: true); var body = await reader.ReadToEndAsync(); context.Request.Body.Position = 0; if (body.Contains("api_key", StringComparison.OrdinalIgnoreCase)) { context.Response.StatusCode = StatusCodes.Status400BadRequest; await context.Response.WriteAsync("Prompt blocked by policy."); return; } await _next(context); } }

Production Example - Tenant Isolation

For multi-tenant AI features, retrieval filters and encryption boundaries should ensure one tenant cannot access another tenant's documents.

Things to Watch Out For

Key Takeaways