How to Build a loan approval Agent Using LlamaIndex in Python for healthcare
A healthcare loan approval agent evaluates financing requests for patients, clinics, or providers by pulling structured policy rules, checking eligibility evidence, and producing a decision with an audit trail. It matters because lending in healthcare sits in a regulated space: you need consistent approvals, explainability, and strict handling of protected data.
Architecture
- •
Document ingestion layer
- •Loads policy docs, underwriting rules, payer contracts, and internal SOPs.
- •Uses
SimpleDirectoryReaderandVectorStoreIndexto make policy text retrievable.
- •
Retrieval layer
- •Finds the exact clauses that apply to a loan request.
- •Uses a
RetrieverQueryEngineorindex.as_query_engine()with metadata filters when needed.
- •
Decision engine
- •Converts retrieved evidence into an approval, rejection, or manual review outcome.
- •Uses an LLM-backed
QueryEnginewith a strict response format.
- •
Risk and compliance guardrails
- •Enforces healthcare-specific checks like PHI minimization, consent requirements, and residency constraints.
- •Validates outputs before they reach downstream systems.
- •
Audit logging layer
- •Stores request inputs, retrieved sources, model output, and final decision.
- •Needed for internal review, adverse action notices, and regulator audits.
Implementation
- •Install dependencies and load policy documents
Use LlamaIndex to index your underwriting policies. Keep healthcare policy documents separate from patient-specific records so retrieval stays scoped and auditable.
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
# Load policy docs from a controlled directory
documents = SimpleDirectoryReader(
input_dir="./healthcare_loan_policies"
).load_data()
# Build an index over approved lending policies only
index = VectorStoreIndex.from_documents(documents)
# Create a query engine for retrieval + synthesis
query_engine = index.as_query_engine(similarity_top_k=3)
- •Define the loan request payload and ask for a decision
Keep the request object structured. In healthcare workflows, you usually need fields like provider type, requested amount, procedure category, insurance status, and whether PHI is present.
from dataclasses import dataclass
from typing import Optional
@dataclass
class HealthcareLoanRequest:
applicant_type: str # patient | clinic | provider
amount: float
purpose: str # e.g. "oncology treatment", "MRI equipment"
state: str
insurance_status: str # insured | uninsured | partial
annual_income: Optional[float] = None
has_phi: bool = False
request = HealthcareLoanRequest(
applicant_type="patient",
amount=12000,
purpose="orthopedic surgery",
state="CA",
insurance_status="partial",
annual_income=68000,
has_phi=True,
)
prompt = f"""
You are a healthcare lending decision agent.
Use only the provided policy documents.
Request:
- applicant_type: {request.applicant_type}
- amount: {request.amount}
- purpose: {request.purpose}
- state: {request.state}
- insurance_status: {request.insurance_status}
- annual_income: {request.annual_income}
- has_phi: {request.has_phi}
Return:
1) decision: approve | reject | manual_review
2) reason
3) cited_policy_clauses
4) required_next_step if manual_review
"""
response = query_engine.query(prompt)
print(response)
- •Add structured output validation
For production use, don’t trust free-form text alone. Wrap the response in a schema check so downstream systems can rely on it.
from pydantic import BaseModel
from typing import List, Literal
class LoanDecision(BaseModel):
decision: Literal["approve", "reject", "manual_review"]
reason: str
cited_policy_clauses: List[str]
required_next_step: str | None = None
raw_text = str(response)
# Example parser pattern; in production you may enforce JSON output at prompt level.
# This keeps your orchestration layer strict even if the model drifts.
decision = LoanDecision.model_validate_json(raw_text)
print(decision.model_dump())
- •Route high-risk cases to manual review
Healthcare lending should not auto-decide every case. If the request contains PHI, crosses residency boundaries, or lacks enough evidence, send it to human review.
def needs_manual_review(req: HealthcareLoanRequest) -> bool:
return (
req.has_phi or
req.amount > 25000 or
req.insurance_status == "partial" or
req.applicant_type not in {"patient", "clinic", "provider"}
)
if needs_manual_review(request):
final_decision = {
"decision": "manual_review",
"reason": "High-risk healthcare lending case requires human approval.",
"required_next_step": "Compliance officer review",
}
else:
final_decision = decision.model_dump()
print(final_decision)
Production Considerations
- •
Control data residency
- •Keep indexes in-region if your healthcare data must stay within a specific jurisdiction.
- •Don’t mix policies from one region with patient data from another unless your legal team has approved it.
- •
Log for auditability
- •Store the exact input payload, retrieved source chunks, model version, prompt template version, and final decision.
- •You need this for adverse action explanations and compliance reviews.
- •
Minimize PHI exposure
- •Strip identifiers before retrieval when possible.
- •If the agent doesn’t need diagnosis codes or patient names to make the credit decision, don’t pass them in.
- •
Add human-in-the-loop thresholds
- •Auto-approve only low-risk cases with strong policy matches.
- •Route borderline cases to compliance or underwriting staff.
Common Pitfalls
- •
Using patient records as general retrieval corpus
- •This is the fastest way to leak PHI into unrelated decisions.
- •Keep loan policy indexes separate from clinical data indexes.
- •
Letting the model decide without cited policies
- •A naked “approve” or “reject” is useless in regulated lending.
- •Require retrieved clause citations in every response and reject outputs without them.
- •
Ignoring state-by-state lending rules
- •Healthcare financing often changes across jurisdictions.
- •Add metadata filters by state and keep your policy corpus versioned by effective date.
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