How to Build a customer support Agent Using LlamaIndex in Python for healthcare
A healthcare customer support agent answers patient and provider questions, routes requests to the right workflow, and pulls grounded answers from approved sources like policy docs, benefit guides, and internal SOPs. It matters because bad answers in healthcare are not just a support issue; they can create compliance risk, delay care, and expose protected data.
Architecture
- •
Document ingestion layer
- •Loads approved PDFs, HTML pages, policy docs, and FAQs from a controlled source.
- •Use
SimpleDirectoryReaderfor local files or a custom loader for your DMS.
- •
Indexing layer
- •Converts documents into embeddings and stores them in a vector index.
- •
VectorStoreIndexis the default choice when you need retrieval over policy and knowledge base content.
- •
Retrieval + response synthesis
- •Retrieves only the most relevant chunks and synthesizes an answer with citations.
- •
index.as_query_engine()is the simplest production-friendly entry point.
- •
Conversation orchestration
- •Maintains chat context for multi-turn support flows.
- •
ChatMemoryBufferkeeps short-term state without dumping everything into the prompt.
- •
Guardrails layer
- •Detects PHI leakage, out-of-scope questions, and unsafe medical advice requests.
- •Use explicit validation before answering or escalating.
- •
Audit + observability
- •Logs user question, retrieved sources, response text, and escalation decisions.
- •Required for incident review, compliance checks, and model debugging.
Implementation
- •Install dependencies and load approved healthcare content
Use only sanctioned documents. In healthcare, your source set should be curated by compliance or operations, not scraped from the open web.
pip install llama-index llama-index-embeddings-openai llama-index-llms-openai
from llama_index.core import SimpleDirectoryReader
documents = SimpleDirectoryReader(
input_dir="./healthcare_docs",
recursive=True
).load_data()
print(f"Loaded {len(documents)} documents")
- •Build a vector index over the support knowledge base
This pattern gives you retrieval grounded in your own material. If you need stricter control over residency or vendor usage, swap the embedding model and storage backend to one approved for your environment.
from llama_index.core import VectorStoreIndex
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.core import Settings
Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small")
index = VectorStoreIndex.from_documents(
documents,
show_progress=True
)
- •Create a query engine with citations and a chat memory buffer
For customer support, you want answers backed by source nodes. You also want short conversation memory so follow-up questions work without re-sending the entire case history.
from llama_index.core.memory import ChatMemoryBuffer
from llama_index.llms.openai import OpenAI
Settings.llm = OpenAI(model="gpt-4o-mini", temperature=0)
memory = ChatMemoryBuffer.from_defaults(token_limit=2000)
query_engine = index.as_query_engine(
similarity_top_k=4,
llm=Settings.llm,
response_mode="compact"
)
response = query_engine.query(
"What is the prior authorization process for outpatient imaging?"
)
print(response.response)
for source in response.source_nodes:
print(source.node.metadata.get("file_name"), source.score)
- •Wrap it in a support-agent function with basic healthcare guardrails
This is where you enforce boundaries. The agent should answer administrative questions, cite sources, and escalate anything that looks like diagnosis, treatment advice, or PHI handling outside policy.
import re
PHI_PATTERN = re.compile(r"\b\d{3}-\d{2}-\d{4}\b") # example: SSN-like pattern
def is_safe_support_question(text: str) -> bool:
blocked_terms = [
"diagnose me",
"what medication should I take",
"prescribe",
"medical advice"
]
if PHI_PATTERN.search(text):
return False
return not any(term in text.lower() for term in blocked_terms)
def answer_support_question(user_text: str) -> str:
if not is_safe_support_question(user_text):
return (
"I can help with administrative support questions only. "
"For clinical concerns or sensitive personal data, please contact the care team."
)
result = query_engine.query(user_text)
citations = []
for node in result.source_nodes[:3]:
citations.append(node.node.metadata.get("file_name", "unknown"))
return f"{result.response}\n\nSources: {', '.join(citations)}"
print(answer_support_question("How do I submit a referral request?"))
Production Considerations
- •
Deployment
- •Keep document ingestion and indexing separate from online serving.
- •Rebuild indexes on a controlled schedule so support answers do not drift after every file change.
- •For data residency, store embeddings and vector data in-region if your healthcare contract requires it.
- •
Monitoring
- •Log every query with retrieved document IDs, similarity scores, response length, and escalation reason.
- •Track hallucination indicators like answers without citations or answers generated from low-score retrievals.
- •Add alerts when users repeatedly ask for clinical guidance through the support channel.
- •
Guardrails
- •Block PHI from being sent to external LLM endpoints unless your legal/compliance team has signed off on that path.
- •Add intent classification before retrieval so clinical questions go to the right workflow instead of being answered by policy docs.
- •Maintain an allowlist of document sources; do not let the agent search arbitrary folders or shared drives.
- •
Compliance
- •Keep audit trails for user prompts, model version, prompt template version, and retrieved sources.
- •Define retention policies for chat logs because support transcripts can contain sensitive health information.
- •Make escalation paths explicit when the agent detects consent issues, billing disputes involving personal records, or urgent care language.
Common Pitfalls
- •
Using unvetted documents as knowledge sources
- •If you index stale PDFs or random internal notes, the agent will confidently answer from bad material.
- •Avoid this by curating an approved corpus with owner tags and review dates.
- •
Letting the agent handle clinical advice
- •A support agent should not infer symptoms or recommend treatment plans.
- •Route anything clinical to licensed staff or a triage workflow using hard-coded refusal rules plus intent detection.
- •
Skipping citation checks
- •Answers without sources are hard to audit and impossible to defend during compliance review.
- •Require
source_nodesin responses and reject outputs that cannot cite at least one approved document.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit