I treat runtime guardrails as checks the application must enforce - not instructions the model can choose to follow. Before a tool runs, check current permissions and exact arguments. Before an answer leaves, check what the user may receive.
I use 7 patterns to cover the request:
- User policies: Set access from verified identity and permissions.
- Session rules: Limit access by consent, workspace, task, and expiration.
- Tool checks: Validate arguments, enforce limits, and require approval for high-impact actions.
- Output filters: Check responses before release, including streamed content.
- Risk routing: Restrict tools and require more checks as risk grows.
- Regional controls: Keep processing, storage, and downstream tools within approved locations.
- Safe fallbacks: Retry transient failures within limits, offer safe partial answers, or stop.
My rule: <u>missing permission means no action</u>. I version policy text with PromptOT, keep enforcement in application code, and test denials, outages, approvals, and rollback before release. A prompt version records the guidance - not permission to act.
7 Runtime Guardrail Patterns for AI Applications
Open Source Guardrails: Securing LLM Applications at Scale - Mac Misiura | AI in Production 2026
sbb-itb-b6d32c9
Trusted Context and Enforcement Points
The seven runtime patterns use the trust inputs and enforcement points defined here. Maintain a versioned policy registry with an immutable version, owner, scope, activation time, approval state, and rollback target. For every request, resolve and record the active version. Keep session capabilities revocable so password resets, consent withdrawal, or admin action can end access immediately. These rules determine which policy version can govern a request at runtime.
Resolve policies using server-verified identity and metadata, not client claims about roles, regions, or risk. Treat retrieved documents, uploaded files, model output, and tool results as untrusted - even when the source is authenticated. OWASP warns that indirect prompt injection can arrive through tool output. Keep instructions separate from tool-output context.
Define narrow tool schemas with typed arguments, allowed ranges, resource scope, and caller permissions. Separate reads from writes, use idempotency keys for mutations, and tie approvals to the exact operation and arguments. Never pass generated text directly into SQL or shell commands.
| Boundary | Enforcement actions |
|---|---|
| Inputs | Authenticate, validate, redact sensitive fields, block prohibited requests, or send uncertain cases for review. |
| Retrieved context | Restrict sources, remove unnecessary sensitive data, and isolate content as untrusted data. |
| Tool calls | Check schema, resource scope, current permissions, and policy; block, require approval, or route to a restricted workflow. |
| Tool results | Validate types and provenance, redact secrets, and quarantine suspicious instructions before reuse. |
| Responses | Check release rules; redact, rewrite permitted content, block release, or return an approved fallback. |
Each pattern below uses these trust boundaries to target a specific decision point.
Log decisions, not payloads: record the request ID, policy version, tool name, validation result, approval status, enforcement action, and fallback reason. Minimize identity data, restrict access, and set retention limits so logs don't become another store of sensitive data.
Fail closed on authorization and write actions. Stop execution when identity is missing, policy can't be resolved, arguments are invalid, or approval is unavailable. For low-risk reads, return approved static content or a constrained read-only response. Test these paths with expired sessions, missing region metadata, policy-service outages, poisoned tool results, and duplicate write requests.
1. Select Policies by User
When to Use It
Use per-user policies when access or safety requirements vary by authenticated role, explicit permissions, account tier, age restrictions, workflow, or data sensitivity. Tier controls entitlements, not authorization. Paid status never overrides tenant access rules. This pattern uses verified identity to select the policy for each request.
Runtime Decision
Before the model runs, use the trusted context above to map verified user and tenant details to the active request policy. Return allow, deny, or allow_with_constraints, plus retrieval scopes, tool limits, output rules, and the policy version.
If required identity context is missing, deny the protected operation. Don’t fall back to a permissive “standard user” policy.
Enforcement Action
Enforce the decision at four boundaries: prompt configuration, retrieval scope, tool calls, and output filtering. Reauthorize tools against current permissions and exact arguments, and filter outputs for prohibited disclosures. Don’t retrieve broad datasets and trust the model to hide records the user can’t access.
PromptOT supports versioned guardrail blocks, but server-side authorization remains authoritative.
Tradeoffs
Fine-grained policies add lookup and maintenance overhead. Reuse policy components, define which rules take precedence, and keep decision caches short-lived and sensitive to permission changes.
Test expired memberships, forged tenant IDs, missing age checks, and tier downgrades. Verify that each case removes access immediately. The next pattern narrows rules for a single session after user policy selection.
2. Set Session-Level Rules
When to Use It
Conversation history is not authorization state. Use session rules when consent, workspace, verified identity, or task scope can change during a conversation.
Store authorization state outside the chat history. This includes consent, workspace, identity, task scope, data classification, allowed tools, escalation rules, expiration, and policy version.
Runtime Decision
After the user selects a policy, apply session rules to the current conversation state. Session permissions come from the intersection of user policy and session policy: session rules can only narrow access.
Limit consent to the approved data, tools, recipients, and time window. Record each rule’s trusted source, such as a consent record, identity provider, or workspace configuration.
Enforcement Action
Recheck session state before retrieval, every tool call, and response release. Invalidate cached permissions when the workspace changes, and tighten controls when data classification increases. Consent withdrawal, sign-out, or compromise must immediately end the affected session authority.
For sensitive actions, pause for the designated approver. Before resuming, recheck current permissions and confirm that approval is still valid. Log the session ID, rule ID, rule source, policy version, result, approval scope, and expiration. Keep policy reasons in the audit log - not in model output - and log references rather than sensitive content.
Tradeoffs
Session checks take state-management work. Make reauthentication and renewed consent explicit paths to recovery. Test consent withdrawal mid-conversation, workspace switching, expired verification, mixed data classes, and approval that expires while an action is paused. Track overrides and approval outcomes to spot rules that add unnecessary friction.
The next gate is tool execution, where these session limits must apply to every call.
3. Check Tool Calls Before Execution
When to Use It
Session rules limit access, but every model-generated tool call still needs an execution gate. Use a default-deny allowlist, exposing only the tools needed for the current task. Keep read-only tools separate from high-impact actions, and check permissions for both.
Runtime Decision
Validate arguments against strict schemas, normalize values, and then check business rules. Valid JSON does not grant access. Get identity and tenant scope from the authenticated server context for each call - not from model-supplied values. Enforce resource ownership in data-access queries and verify permission for the specific operation. Reject unsafe paths, unapproved URLs, and model-supplied identity overrides.
Enforcement Action
Set rate limits, cumulative budgets, concurrency caps, timeouts, and retry limits in the executor. Update budgets atomically so parallel calls cannot spend past the limit.
For high-impact actions, show the target, parameters, amount or recipient group, and consequences. Tie confirmation to that exact request, then consume approval atomically before execution. Changed parameters require new approval. Ignore approval flags set by the model.
Before sending results back to the model, validate their schema, enforce row- and field-level access, remove secrets and unnecessary personal data, and cap response size. Treat tool output as untrusted data.
Return typed outcomes such as denied, approval_required, or rate_limited, along with a safe message, request ID, and retryable flag. The orchestration layer must preserve these decisions: retries and other tools must not bypass the same gate. Once execution checks pass, output release is the next gate.
Tradeoffs
These checks add latency and extra confirmation steps. Test unknown tools, cross-tenant IDs, changed approval parameters, concurrent budget exhaustion, and prompt injection inside tool results. Track schema failures, blocked access, confirmation abandonment, and gate latency to find unnecessary friction without weakening enforcement. The next pattern filters outputs before release.
4. Filter Outputs Before Release
When to Use It
After tools run, the release gate decides what reaches the user or downstream system. Following the prompt doesn’t make a response safe. It can still leak data, give unsafe advice, or make unsupported claims. Base release checks on user, session, and policy context - not instructions inside the response.
Runtime Decision
Start with deterministic checks: schemas, required fields, length limits, blocked strings, allowed URLs, and destination-specific sanitization. Then use semantic classifiers to check for unsafe content, prohibited topics, and sensitive data.
For high-impact claims, verify support from trusted sources. Citations and confidence scores aren’t proof. Before releasing sensitive information, confirm that the user is authorized to receive it.
Enforcement Action
Redact personal data that isn’t needed only when the answer remains accurate. Block credentials and secrets. For fixable formatting or evidence failures, regenerate once, then run the checks again. Return a safe response for prohibited requests or repeated failures. Escalate uncertain disclosures and high-impact claims that can’t be verified. If a required detector is unavailable, block release.
Apply the same rules to streaming responses before any partial output leaves the system. Buffer content until there’s a complete unit to check, such as a full sentence, code block, or structured object. For secrets or high-risk content, buffer the entire response. Also buffer sensitive or untrusted tool results before displaying them or passing them downstream. Stopping a stream after it leaks data is too late.
Tradeoffs
Buffering adds latency, and classifiers can block legitimate answers. Track check results, policy versions, actions, and release latency without storing sensitive content.
Whenever prompts, models, or thresholds change, test split-token identifiers, multilingual content, quoted material, negation, harmless mentions of prohibited topics, malformed structured responses, streaming fragments, and attempts to evade keyword checks.
Clear release rules also inform the next control: routing requests by risk.
5. Route Requests by Risk
When to Use It
After core authorization checks, risk routing determines which enforcement path a request takes. Use it when different requests need different controls.
Combine prompt-injection signals, sensitive data, high-impact domains, unexpected tool requests, server-verified user history, and retrieved-content signals. Verified history can reduce friction, but it must never override authorization.
Runtime Decision
Assess signals together. Suspicious instructions combined with sensitive-data access and bulk export access justify tighter controls. Keep routing logic outside the model, and recalculate risk whenever retrieval or proposed actions reveal new concerns.
Enforcement Action
A risk tier must change capabilities. Enforce route-specific tool allowlists, data limits, and approval gates in application code.
| Risk tier | Permitted tools | Checks | Approvals | Latency | Fallback |
|---|---|---|---|---|---|
| Low | Read-only tools, approved sources | Basic policy checks | None | Low | Return the answer or ask a clarifying question |
| Medium | Narrow, task-specific tools; no unrestricted writes | Risk classification and write-approval requirements | User confirmation or policy approval for writes | Moderate | Remove sensitive details, disable writes, or route to support |
| High | No autonomous actions with major effects; tightly scoped tools only | Independent authorization, provenance checks, deterministic rules, full audit logging | Required before execution; block if unavailable | High | Refuse the action, provide general information, and route the user to the approved process |
These tiers add restrictions. They do not replace earlier enforcement gates.
If risk scores are missing, malformed, conflicting, or timed out, disable writes and privileged tools. Allow read-only operation only when deterministic checks pass. Otherwise, request clarification or queue the request for review.
Tradeoffs
Calibrate thresholds using labeled normal, ambiguous, sensitive, and adversarial requests - not a universal score cutoff. Track false positives, false negatives, escalation rate, and latency by category.
Review blocked requests with domain owners. Add verified legitimate cases to tests, and narrow faulty rules instead of lowering thresholds across the board. Recalibrate after changes to the model, prompts, tools, or policies.
Missing risk data is not evidence of low risk. Risk changes the route; region can still narrow it further.
6. Apply Region-Based Controls
When to Use It
Use regional controls when contracts or data-residency rules limit where data can be processed or stored. This is a deployment boundary, not a model behavior rule. Determine jurisdiction from server-controlled metadata: verified tenant settings, account or contract records, identity-provider claims, network or cloud-region metadata, and the workload’s deployment region. Don’t rely on prompt text, a user-stated location, or an IP address alone.
Keep storage location and processing geography in separate policy fields. Risk can narrow access, but region can still block processing.
Runtime Decision
Resolve region policy before assembling the prompt. Apply verified organization policy first, regulated-data classification second, and the approved deployment or tenant region third. If signals are missing or conflict, block sensitive processing until verification is complete.
Recheck when tenants switch and sessions resume. Invalidate cached content or credentials that no longer fit the approved boundary.
Enforcement Action
Enforce allowlists for endpoints, retrieval indexes, storage, tools, logs, and backups. Apply retention rules to prompts, outputs, embeddings, and traces. Redact or block sensitive fields before transferring data across regions.
Approving a model endpoint does not approve its downstream tools. Disable cross-region failover unless the destination is explicitly authorized.
Log the tenant, signal sources and timestamps, effective jurisdiction, data classification, selected endpoints, retrieval stores, tools, retention rule, policy version, and reason for any denial or fallback. Use references or redacted summaries rather than raw sensitive prompts.
Tradeoffs
Tighter boundaries can reduce capacity and failover options. Check the actual processing terms: a data zone may span multiple regions without meeting a country-specific requirement.
Residency controls govern geography only. They don’t establish lawful processing or confirm that contracts, access controls, deletion, or compliance requirements have been met. Require privacy, legal, and security review before calling a route compliant. If no approved region fits the request, route it to the safe fallback flow.
7. Define Safe Fallback Flows
When to Use It
When a request passes earlier checks but still fails, fallback sets the final safe path. Define recovery paths for four failure types, rather than using one generic retry handler:
- Input failures: Ask for clarification or refuse.
- Tool failures: Correct arguments, disable the tool, or request human review.
- Output failures: Redact, regenerate, return a safe partial response, or refuse.
- Infrastructure failures: Use capped retries or return a temporary-unavailability response.
Runtime Decision
When earlier gates fail or stall, choose the least-permissive recovery path. Base the next action on risk, reversibility, and whether a retry could help. Keep the original decision reason through every transition.
Log the failure type, policy version, attempted action, retry count, elapsed time, and final disposition. Do not store raw sensitive content.
Enforcement Action
Never replace a denial with elevated credentials. Stop denied tool calls. Fallback must not bypass input, tool, output, or identity checks. Disable unavailable tools and return only trusted data.
Repeat release checks after redaction or regeneration. Never echo rejected content in the explanation. Send unresolved identity issues and high-impact actions to human review with sanitized context and the status “pending human review.”
Cap interactive retries at 2–3 attempts within a 10–30 second deadline. Retry only transient failures and repeatable operations. Use idempotency keys for side effects, and keep retries within one orchestration layer. Apply exponential backoff with jitter, a shared attempt counter, an absolute deadline, and a nesting limit that child handlers cannot reset. Version these fallback rules alongside the rest of the guardrail policy.
Tradeoffs
Human review adds judgment, not certainty. Refuse when no authorized path exists.
| Fallback | Safety fit | User continuity | Complexity |
|---|---|---|---|
| Rewrite or redact | Localized problems; revalidate afterward | High | Medium |
| Bounded retry | Transient failures only | High if successful | Medium |
| Safe partial response | Safe subset remains | Medium–high | Medium |
| Human escalation | Ambiguous or high-impact cases | Delayed | High |
| Refusal | No compliant response exists | Low; offer a safe next step | Low |
Version Guardrail Blocks With PromptOT
Use PromptOT to version the prompt blocks behind these runtime enforcement checks. PromptOT manages the prompt package for these seven patterns - it isn’t a separate guardrail layer. Dedicated guardrail blocks keep guardrail boundaries separate from task instructions and context. The API compiles enabled blocks into one prompt payload for any provider.
Treat drafts as test artifacts and published versions as production dependencies. Before publishing, test drafts against normal requests, adversarial prompts, indirect prompt injections, tool-use scenarios, and refusal cases. Development-scoped keys retrieve the current draft; production-scoped keys retrieve the published version. If a release introduces regressions, roll back to a known-good version. Record the prompt version and guardrail block IDs with each request so you can trace what ran.
Assign reviews by role: domain experts approve policy language, security approves enforcement, developers connect variables and APIs, and release owners publish changes. Keep production API keys server-side in a secrets manager.
Use runtime {{placeholders}} only for trusted context. In application code, validate variable names, types, lengths, and allowed values. Reject missing or unexpected inputs. PromptOT compiles the prompt, while the application remains responsible for authorization, tool execution, region checks, and output release. Published guardrail blocks do not authorize actions. The next section turns this setup into a deployment checklist.
Runtime Guardrail Implementation Checklist
Use these checks to put the seven patterns above into practice.
Verify policy inputs and precedence. Bind policy inputs to the authenticated user, tenant, session, region, and environment. Reject stale or conflicting inputs, and define precedence in application logic.
Check every boundary. Run checks at input, retrieval, tool call, tool result, and release. Recheck authorization immediately before high-impact actions. The model proposes; the application enforces. This turns the enforcement chain from earlier sections into deployment checks.
Define failure handling. If a timeout or policy service failure occurs, fail closed for high-impact actions. For low-risk requests, use a narrowly scoped safe fallback. A cached low-risk policy can cover a short outage, but it must have a defined expiration.
Make release tests mandatory. Test allow, deny, redact, approval, timeout, and policy service failure in staging and shadow mode. Compare current and candidate policy outcomes against a fixed evaluation set. Verify rollback, in-flight requests, cache expiration, and version attribution. Keep pass/fail evidence for release review, and move to production ONLY after these cases pass in versioned staging and shadow runs.
Log and measure for guardrail governance. Log the pseudonymous request/session ID, tenant, region, policy ID/version, stage, rule ID, risk band, decision, approval status, timestamp, and duration. Track false positives, false negatives, latency, approval wait time, and fallback rates by policy version and workflow. Review sampled decisions to check accuracy; alerts alone aren't enough. Investigate denial spikes or increased fallback use after releases.
Conclusion: Change Policies, Keep Enforcement Required
The seven patterns form one chain at request time. Start with gates where failures are hardest to reverse.
Focus first on money movement, account changes, deletions, external messages, sensitive-data access, and regulated advice. Expand only when testing shows the controls hold. Keep policy versions, but leave execution controls outside the model.
PromptOT versions policy text; application code enforces runtime authorization, tool permissions, regional data routing, and required approvals. Version guidance; keep enforcement mandatory.
FAQs
Which runtime guardrails should I implement first?
Before the model processes a request, validate your variable contract. Enforce a schema for every placeholder to check that required variables are present, have the correct types, and follow the expected formats. Then sanitize inputs by trimming whitespace and enforcing character or token limits.
Use structured output schemas to keep results predictable and machine-readable. For high-severity risks, including prompt injection and sensitive-data leakage, use strict, exception-based enforcement rather than best-effort cleanup.
How can I prevent permission changes from racing with tool execution?
Enforce authorization at the application boundary. Don’t rely on the LLM to control its own access.
Keep classification, policy, and enforcement separate. The execution layer should check tool calls against an external, immutable authorization context, such as RBAC or ABAC, that the model cannot change.
Put these checks in middleware or a gateway. At execution time, verify resource scopes and permissions against the user’s actual status.
How can I reduce guardrail latency without weakening security?
Layer your checks: start with fast regex or schema validation, then move to heavier checks. Validate streaming content as it arrives, and run guardrails in parallel with the primary LLM call.
Set a 100–200 ms timeout budget per service. If validation fails, use a predefined fallback flow, such as a neutral message.
Use PromptOT to define guardrails as modular, reusable prompt blocks.
