RAG Validation
Guarded RAG is an application policy around retrieval-augmented generation.
Retrieval selects text that may help answer a question, but a successful vector
search does not make that text safe and does not guarantee that the model will
use it correctly. models-rag therefore applies two deterministic checks:
-
validate retrieved context before it enters the model prompt
-
validate the generated answer against the retrieved documents before it reaches the caller
The policy is framework-neutral. The same checks can wrap plain Java, LangChain4j, Spring AI, or another caller of a Models runtime.
Retrieval Validation
Retrieved documents are untrusted input. Prepare the canonical prompt before
generation; preparation calls assess and does not expose rejected context as
prompt text:
var policy = GroundedAnswerPolicy.productionDefault();
GroundedRagPrompt prompt = GroundedRagPrompt.prepare(policy, question, evidence);
if (!prompt.generationAllowed()) {
return GroundedAnswerPolicy.ABSTENTION;
}
assess returns an explicit reason:
| Decision | Meaning |
|---|---|
|
The context passed every configured retrieval gate. |
|
No documents were retrieved. |
|
No document met the minimum retrieval score. |
|
Named entities in the question had no overlap with the evidence. |
|
The result exceeded the configured document count. |
|
Titles and document text exceeded the configured character budget. |
|
A title or document contained a recognized instruction-override phrase or model control-token delimiter. |
The production limits allow at most 32 documents and 131,072 retrieved
characters. GroundingLimits can impose smaller limits for a model with a
smaller context window. The minimum retrieval score is retriever-specific and
must be set to match that retriever’s score scale.
Answer Validation
After generation, pass the same question and evidence to apply:
String rawModelText = model.generate(prompt, options);
GroundedAnswer answer = policy.apply(question, evidence, rawModelText);
System.out.println(answer.decision());
System.out.println(answer.text());
apply repeats retrieval validation so unsafe context cannot yield an accepted
answer even if the application forgot the preflight. It then evaluates the
answer in this order:
-
Preserve an explicit model abstention as
MODEL_ABSTENTION. -
Reject an answer that alters an atomic identifier such as
POLICY-17by reusing only pieces of it. -
Require every substantive answer term to appear in the question or retrieved evidence, allowing only the policy’s small set of normalized inflections.
-
Require evidence anchors for every clause of a multi-part question.
-
Require each substantive statement’s evidence-bound terms to be supported by one retrieved document. Terms pooled from unrelated documents cannot be recombined into a supported claim.
-
Accept only citations whose IDs were retrieved, then verify that the cited documents themselves support the answer. If a supported answer omitted citations, attach only the supporting document IDs.
-
Replace unsupported output with a size-bounded extractive answer copied from trusted retrieved documents.
The result retains both the raw model text and the final application text for audit. The validator is a conservative lexical policy, not a natural-language inference model: it does not prove semantic or factual correctness. It reduces specific failure modes such as unsupported terms, cross-document recombinations, invented citations, and lost identifiers. Applications with a higher assurance requirement should add domain validation or human review.
Complete Flow
import com.integrallis.models.api.SamplingOptions;
import com.integrallis.models.api.TextGenerationModel;
import com.integrallis.models.rag.GroundedAnswerPolicy;
import com.integrallis.models.rag.GroundedRagPrompt;
import com.integrallis.models.rag.GroundingDocument;
import java.util.List;
final class RagAnswerService {
static String answer(
TextGenerationModel model,
String question,
List<GroundingDocument> evidence,
SamplingOptions options) {
var policy = GroundedAnswerPolicy.productionDefault();
var prompt = GroundedRagPrompt.prepare(policy, question, evidence);
if (!prompt.generationAllowed()) {
return GroundedAnswerPolicy.ABSTENTION;
}
String rawModelText = model.generate(prompt.text(), options);
return policy.apply(question, evidence, rawModelText).text();
}
}
Final Decisions
GroundedAnswerPolicy produces one of five answer outcomes:
-
MODEL_ANSWERfor supported text with trusted citations -
MODEL_ANSWER_WITH_DERIVED_CITATIONSfor supported text whose citations were deterministically attached by the policy -
MODEL_ABSTENTIONwhen the model explicitly reports insufficient context -
RETRIEVAL_ABSTENTIONwhen retrieval does not clear the configured gates -
EXTRACTIVE_FALLBACKwhen generated claims or citations cannot be validated
Fallback output uses at most the configured number of retrieved documents and
characters. If no bounded extractive answer can be produced, the result is
INSUFFICIENT_CONTEXT.
Configure the Policy
Set a threshold that matches the retriever:
var policy = new GroundedAnswerPolicy(0.78f);
Set context and fallback bounds explicitly:
var limits = new GroundingLimits(
12, // maximum retrieved documents
48_000, // maximum retrieved characters
2, // maximum fallback documents
2_000); // maximum fallback characters
var policy = new GroundedAnswerPolicy(0.78f, limits);
Framework Placement
Run retrieval through the framework integration, call GroundedRagPrompt.prepare,
add only accepted evidence to the prompt, generate through the local model, and
call apply before returning the answer. Framework chat APIs can place
prompt.instructions() in a system message and prompt.request() in a user
message. See LangChain4j,
Spring AI, Spring Boot, and
Vectors for adapter wiring.
Qualification
The controlled RAG suite gives plain Java, LangChain4j, Spring AI, Ollama, llama.cpp, the pinned Transformers checkpoint reference, and hosted providers the same corpus, retrieval results, prompt, sampling controls, and correctness rules. Results include TTFT, decode throughput, end-to-end latency, grounding decisions, and answer correctness.
See the production RAG ledger and the benchmark runner.
