Guardrails with OPA Policies in Quarkus LangChain4j
A tutorial on implementing hybrid input guardrails for LLM-based Quarkus applications using Open Policy Agent (OPA) policies compiled to WebAssembly. The approach combines fast deterministic pattern matching (sub-millisecond, zero cost) with optional LLM escalation for ambiguous cases. OPA Rego policies detect prompt injection attacks via exact string matches and regex patterns, and when signals are weak, a custom OPA builtin delegates scoring to a secondary LLM. The Rego policy is compiled to Wasm using the Styra OPA Java SDK, integrated into a Quarkus LangChain4j InputGuardrail implementation. The post includes full Java code, Rego policy snippets, build commands, and live test examples showing all three code paths: instant block, instant allow, and LLM-escalated block.