OPA for AI Policy Separates Decision Logic from the LLM Call
Open Policy Agent can evaluate structured facts about an AI request, including caller role, model route, data classification, region, and requested tool. OPA returns a decision; the gateway still has to enforce it, fail closed, protect policy inputs, version bundles, and write evidence for each request.

OPA evaluates structured input against policy and returns a decision. For an AI gateway, that input can include caller role, model route, data classification, region, and requested tool. The LLM never decides whether the request is permitted.
TL;DR
- OPA can decide AI request policy when the gateway supplies trusted identity, application, route, and classification facts.
- The gateway must enforce the result and fail closed when policy evaluation is unavailable.
- Version the policy bundle and store its decision ID with every routed request.
- Treat every field sent to OPA as a security input with a defined source and validation path.
OPA is the decision point
The Open Policy Agent documentation describes a general-purpose policy engine that evaluates structured data. Rego policies operate on input supplied by the integrating service. OPA does not discover a user's identity or inspect a prompt unless another component provides those facts.
An AI gateway can normalize a request into fields such as subject, role, application, provider, model, region, prompt classification, tool name, and requested action. OPA returns permit or deny, potentially in a richer decision object. The gateway then applies that result before opening the provider connection.
That separation creates a clear control boundary. Policy authors define which request facts produce an allowed result, while the gateway controls what happens on the network after receiving that result. A permit response has no effect unless the gateway reads it and opens the approved route. A deny response must stop the request before any prompt content reaches the selected provider.
Keep the input contract explicit. Each field should have a defined type, an authoritative source, validation behavior, and a response for missing values. This makes the Rego policy reviewable without requiring reviewers to infer how application payloads become authorization facts.
Trust the input before the rule
A policy is only as strong as its facts. Caller identity should come from a verified token or workload identity, rather than an arbitrary JSON field supplied by the application. Classification should include the detector version and confidence handling. Model aliases should resolve to a concrete provider endpoint before evaluation.
The identity-aware AI gateway guide covers the upstream context. A sticky note reading "finance users only" beside a shared API key is not identity. I would refuse production approval until the request carries a verifiable subject.
Trace each policy field back to the component that produced it. The gateway should reject an application-supplied role when the same field is supposed to come from a verified identity token. It should also distinguish an absent classification from a classifier result that explicitly marks content as unclassified.
Names need control too. A friendly model alias may hide a provider change or a different processing region, so resolve the alias before OPA compares the request with policy. The evaluated route and the enforced route should match. Otherwise, a valid decision can be attached to traffic that takes a different path.
Input validation belongs before policy evaluation. Reject malformed values before they reach Rego, and define the behavior for fields that arrive with an unexpected type. Small schema mistakes can change which rule matches.
Enforcement and failure behavior
OPA's Envoy integration documentation shows the separation between policy evaluation and proxy enforcement. The same architectural principle applies to LLM traffic. A sidecar or service can answer the decision, while the gateway owns connection handling and the final block.
Test a policy timeout, stale bundle, malformed input, unavailable classifier, and unknown model. Decide which conditions deny and which enter a documented degraded mode. Security-sensitive AI routes should fail closed.
Run those tests at the gateway boundary, rather than testing the Rego result alone. A unit test can show that a rule returns deny, but an integration test must prove that the gateway never opens the provider connection after receiving that result. Check the network behavior.
Timeout handling needs an explicit path. The gateway should know how long it will wait for a decision and what response it returns when that deadline expires. The same rule applies when OPA starts but cannot load the expected bundle.
A degraded mode should name the routes it affects and the controls that remain active. Record when it begins and ends. Operators then have evidence showing which requests passed through the alternate behavior.
Versioned evidence
Store the policy bundle version, input schema version, decision, reason code, subject, resolved route, and timestamp. Hashes can link the record to a larger input without putting full prompt content into the policy log. Keep policy deployment approval separate from application deployment when possible.
The AI policy enforcement guide explains the boundary record. Rego unit tests should cover allowed cases, denied cases, missing fields, and attempts to smuggle alternate model names through aliases.
A decision record should identify the policy that actually ran, rather than the policy currently present in a repository. Bundle versioning provides that connection. If a rule changes later, an investigator can still determine which version produced the earlier result.
Record the resolved route used for enforcement. The original alias can remain useful for debugging, but it cannot prove where the gateway sent the request. Pairing the decision with the concrete route makes that check possible.
Reason codes should be stable enough for alerting and review. Human-readable text can change as policy wording improves, while a stable code lets operators group repeated denials without parsing prose. Keep the record focused on the decision boundary and avoid copying full prompt content into every policy event.
DeepInspect
DeepInspect enforces identity-aware policy on authenticated HTTP AI traffic and records the result per request. OPA can supply external policy decisions in an architecture where the gateway still owns enforcement, route control, connection handling, and audit evidence.
The integration boundary should preserve the verified subject and the resolved destination used for the request. It should also retain the policy decision that allowed or denied that route. This gives reviewers one record connecting the caller, policy result, and enforced network action.
Book a technical deep dive at deepinspect.ai.
Frequently asked questions
- Does OPA inspect natural-language prompts?
OPA evaluates structured input. A classifier or gateway must derive content labels and pass them as trusted fields.
- Can OPA call the LLM for a decision?
A deterministic authorization decision should avoid depending on probabilistic model behavior. The model call belongs after policy permits the route.