# Guardrails with OPA Policies in Quarkus LangChain4j

DevFeed: [Guardrails with OPA Policies in Quarkus LangChain4j](<https://devfeed.tech/articles/guardrails-with-opa-policies-in-quarkus-langchain4j-43892.md>)

Original publisher: [Read original article](<https://quarkus.io/blog/quarkus-langchain4j-opa-guardrails/>)

Author: Fabio Burzigotti

Published: 2026-07-10T00:00:00Z

Content type: tutorial

Language: en

Sources: [Quarkus - Supersonic Subatomic Java](<https://devfeed.tech/sources/quarkus-supersonic-subatomic-java.md>)

Topics: [langchain4j](<https://devfeed.tech/topics/langchain4j.md>), [Open Policy Agent](<https://devfeed.tech/topics/open-policy-agent.md>), [Quarkus](<https://devfeed.tech/topics/quarkus.md>), [prompt injection](<https://devfeed.tech/topics/prompt-injection.md>), [pattern matching](<https://devfeed.tech/topics/pattern-matching.md>), [Security](<https://devfeed.tech/topics/security.md>), [Large Language Model](<https://devfeed.tech/topics/llm.md>), [rego](<https://devfeed.tech/topics/rego.md>), [Artificial Intelligence](<https://devfeed.tech/topics/ai.md>), [decoupling](<https://devfeed.tech/topics/decoupling.md>), [WebAssembly](<https://devfeed.tech/topics/web-assembly.md>)

Tags: [input](<https://devfeed.tech/tags/input.md>), [langchain4j](<https://devfeed.tech/tags/langchain4j.md>), [opa](<https://devfeed.tech/tags/opa.md>), [pattern-matching](<https://devfeed.tech/tags/pattern-matching.md>), [policies](<https://devfeed.tech/tags/policies.md>), [prompt-injection](<https://devfeed.tech/tags/prompt-injection.md>), [quarkus](<https://devfeed.tech/tags/quarkus.md>), [rego](<https://devfeed.tech/tags/rego.md>), [security](<https://devfeed.tech/tags/security.md>)

## AI overview

This tutorial shows how to combine deterministic pattern matching with Open Policy Agent policies in Quarkus LangChain4j to implement input guardrails for detecting prompt injection attacks. It explains how externalized Rego policies can decouple security rules from application logic and how OPA WebAssembly evaluation can run in-process, with an LLM fallback for inconclusive cases.

## Source excerpt

Introduction As LLMs become integral to enterprise applications, securing the prompts they receive is critical. Quarkus LangChain4j is a Quarkus extension that allows users to integrate AI into applications, and the Quarkus LangChain4j Workshop is a good way to learn about and practice the fundamentals. Regarding security, Step 9 describes input guardrails, showcasing an example of how a separate LLM can be used to detect prompt injection attacks. However, this approach has costs, both in terms of tokens and performance. Deterministic pattern matching can catch obvious attacks faster, and delegating to the LLM becomes the last option, thus reducing the aforementioned effects. At the same time, Open Policy Agent can be used to define declarative policies that can be externalized. This post shows how to combine both approaches in Quarkus LangChain4j. Open Policy Agent (OPA) Pattern matching against a regex is straightforward, but the usual limitations apply: rules are subject to change rules are usually defined by someone who is not strictly an application developer rules should be defined independently of the application lifecycle. This is where Open Policy Agent comes to help. It provides a framework that streamlines policy management across application deployment technologies, resources and roles. In our specific use case, deterministic guardrails can be defined as OPA policies, decoupling them from the application logic, e.g.: package promptinjection import rego.v1 default pattern_score := 0.0 pattern_score := score if { input.text text_lower := lower(input.text) exact_patterns := [ {"pattern": "ignore previous", "weight": 1.0}, {"pattern": "disregard previous", "weight": 1.0}, {"pattern": "forget your instructions", "weight": 1.0}, {"pattern": "override your instructions", "weight": 1.0}, {"pattern": "ignore all prior", "weight": 1.0}, {"pattern": "new instructions", "weight": 0.95}, {"pattern": "reveal the secret", "weight": 0.95}, {"pattern": "system prompt", "