Spring AI ChatClient RAG implementation lets a Java service answer staff questions from your own approved documents, such as clinical protocols, formulary notes and policy manuals, instead of from whatever the model learned in training. The hard part is not calling the model. It is making sure only approved, current documents are retrieved and that every answer shows which ones it used.
Startup founders and engineering leaders will find how retrieval works in Spring AI, how to keep the document set approved, how to return citations and how to handle questions the documents cannot answer.
What is Spring AI ChatClient RAG implementation?
Spring AI ChatClient RAG implementation is a retrieval augmented generation flow built with Spring AI's ChatClient: the service searches a vector store of approved documents, adds the matching passages to the prompt and returns the model's answer with the sources it used. It keeps answers tied to documents your organization controls.
Answering staff questions from your own documents?
We build retrieval over approved content with citations clinicians can check.
The Spring AI reference says QuestionAnswerAdvisor queries the vector store for documents related to the user question and appends them to the user text as context. Its SearchRequest takes a similarity threshold, a topK value and an SQL-like filter expression, which can also be supplied per request through the FILTER_EXPRESSION advisor parameter.
The filter expression is where approval becomes enforceable. When every chunk carries metadata for status, owner and effective date, Agnotic Technologies limits each search to approved, current documents on every request.
Why restrict retrieval to approved documents?
Retrieval must be restricted to approved documents because a model summarizes whatever it is given with the same confidence. A draft protocol, an expired policy or a personal note in the index becomes an answer.
- Drafts: documents still in review may state the opposite of the final version.
- Superseded versions: an old dosing table and the new one both match the question.
- Local copies: files saved in team folders were never reviewed at all.
- Other sites: protocols written for one hospital do not apply at another.
Which documents belong in the index?
The documents that belong in the index are those with an owner, an approval status and a review date. Everything else stays out until it has them.
- Clinical protocols and pathways approved by the relevant committee.
- Formulary and medicine administration notes maintained by pharmacy.
- Policies and procedures with a named owner and review date.
- Kept out: meeting notes, email exports, drafts and anything containing patient details.
Starting with one well-maintained collection, such as pharmacy notes, gives the pipeline a clean first test before the scope widens.
How does the document pipeline work?
The document pipeline turns approved files into searchable chunks with metadata, and removes them when approval ends. It runs before any question is asked.
- Step 1, source: pull documents only from the system of record for approved content, such as a document control system.
- Step 2, split: break each document into passages that keep headings with their text.
- Step 3, label: attach document ID, version, title, section, site, owner, approval status and effective dates to every chunk.
- Step 4, embed and store: write the chunks and metadata to the vector store.
- Step 5, retire: when a new version is approved, remove or mark the old version's chunks before the new ones go live.
Log index updates like any other content change. Someone will ask which version of a protocol the assistant was using on a given day.
How should answers cite their sources?
Answers should cite the document, version and section behind each statement, in a form the reader can open and check. A citation is only useful if it points at the approved text.
- Keep the retrieved list: run retrieval as its own step so the service holds the exact chunks sent to the model.
- Number the passages: label each one in the prompt and ask the model to cite those labels.
- Check the labels: drop any citation that does not match a retrieved passage.
- Return links: map each label to the document title, version and section in the response.
If the model makes a claim without a valid citation, show that part as unsupported or remove it. Do not let the interface present it with the same weight as cited text.
What happens when the documents cannot answer?
When the documents cannot answer, the service should say so and point to a person or a document owner, rather than letting the model fill the gap from general knowledge.
By default, Spring AI's RetrievalAugmentationAdvisor does not allow the retrieved context to be empty: when nothing is retrieved, it instructs the model not to answer the user query. The allowEmptyContext option on ContextualQueryAugmenter changes that behavior.
- Keep that default for clinical content, so an empty search produces a clear no-answer message.
- Set a similarity threshold so weak matches do not count as context.
- Log unanswered questions, because they show gaps in the approved document set.
- Offer a route to the content owner instead of a guess.
Access control on retrieved content
Retrieved content must respect the same access rules as the documents themselves. A retrieval service that ignores them becomes a way around document permissions.
- Pass the user's site, role and department into the filter on every request.
- Keep restricted documents out of shared indexes when filters cannot express the rule.
- Do not index patient records into an assistant built for policies and protocols.
- Record the question, the user, the retrieved chunk IDs and the answer for review.
If a filter cannot express a rule cleanly, a separate index for that audience is safer than a filter that almost works.
Chunking and metadata choices
Chunking and metadata choices decide whether the right passage is found and whether it can be cited precisely.
| Choice | Helps with | Risk if done badly |
|---|---|---|
| Split by heading and section | Citations that point to a named section | Very long sections dilute the match |
| Keep tables whole | Dosing and reference tables stay readable | A large table crowds out other context |
| Repeat the document title in each chunk | Matches on questions that name the document | None worth noting |
| Store effective and review dates | Filtering out expired content | Missing dates let old text through |
Testing a retrieval service before release
Testing a retrieval service means checking retrieval and answers separately, with questions written by the people who own the documents.
- Retrieval tests: for each question, the expected document and section appear in the top results.
- Answer tests: answers match the approved text and cite it correctly.
- No-answer tests: questions outside the documents produce the no-answer message.
- Version tests: after a document update, only the new version is cited.
How does a request flow through ChatClient?
A request flows through a few fixed stages, and each stage is a place to enforce a rule.
- Step 1, identify: the service reads the user's role and site from the signed-in session, never from the question text.
- Step 2, build the filter: approval status, effective dates, site and audience become a filter expression for this request.
- Step 3, retrieve: the vector store returns passages above the similarity threshold that match the filter.
- Step 4, prompt: ChatClient sends the question with the labeled passages and an instruction to answer only from them.
- Step 5, check: the service validates citations against the retrieved labels and attaches document links.
- Step 6, record: the question, filter, passage IDs, answer and citations are stored for review.
A worked example: a formulary question
A worked example: a nurse asks whether a particular tablet can be crushed for a patient with a feeding tube.
- The filter limits retrieval to the current approved formulary notes and the enteral administration guideline for the nurse's site.
- Two passages come back: the guideline's general rule and the formulary entry for that tablet.
- The answer repeats the formulary entry's instruction and cites both passages with version and section.
- Had the tablet no entry, the service would say the documents do not cover it and name the pharmacy team as the owner.
Prompt instructions that support citations
Prompt instructions support citations when they are short, specific and checked by code afterwards.
- Answer only from the numbered passages, and say so when they do not contain the answer.
- Cite the passage number after every sentence that depends on it.
- Quote dosing figures and thresholds exactly rather than paraphrasing them.
- Do not combine passages from different sites or document versions.
Code still has the last word. An instruction makes uncited claims less likely, but the citation check decides what the user sees.
Keeping the index current
Keeping the index current is an operations task, not a one-off load. Approved content changes, and the index has to follow within a known delay.
- Subscribe to approval and retirement events from the document system, or poll it on a schedule.
- Re-index a document when its approved version changes, and record the index version used for each answer.
- Run a daily comparison between approved documents and indexed documents, and alert on differences.
- Re-embed everything after changing the embedding model, since vectors from different models are not comparable.
What are the common mistakes with Spring AI RAG?
The common mistakes with Spring AI ChatClient RAG implementation come from indexing too much and trusting the answer without checking what was retrieved.
- Indexing whole shared drives: drafts and personal copies become sources.
- No version metadata: old and new versions are retrieved together.
- Allowing empty context for clinical questions: the model answers from general knowledge.
- Citations taken from model text alone: labels that match no retrieved passage are shown as sources.
- No owner for unanswered questions: gaps never reach the people who can fill them.
What does a clinical RAG service mean for your build plan?
Most of the work in a clinical RAG service sits around the model: the document pipeline, metadata, filters, citation checks and tests. Plan those with the document owners from the start.
- Identify the system of record for approved documents and its approval states.
- Agree the metadata fields every chunk must carry.
- Write test questions with clinical and policy owners.
- Decide who reviews unanswered questions and how often.
How does Agnotic build Spring AI retrieval services?
Agnotic builds Spring AI retrieval services over approved content: the ingestion pipeline from your document system, metadata filters, citation checks, no-answer handling and evaluation sets written with your document owners.
Answers your staff can trace to the source
Spring AI retrieval over approved documents, with citations and tests.
Related reading on Agnotic
Retrieval, Spring and document control reading.


