TL;DR
I built an AI agent that automates medical prior authorizations using Python and RAG. It parses patient notes, checks them against insurance policies, and outputs a decision with cited evidence.
Get the Code Here
Introduction
In my experience working with healthcare technology, the "prior authorization" process is one of the biggest bottlenecks. It feels like the industry is still stuck in the fax machine era. Doctors spend hours on phone calls, and patients wait days for simple approvals.
I thought, "Why can't an AI agent handle this?"
So, I decided to build a Proof of Concept (PoC) to see if a simple RAG (Retrieval-Augmented Generation) system could parse a patient's electronic health record (EHR), cross-reference it with a complex insurance policy, and spit out a determination.
The result is the Autonomous Medical Pre-Authorization Agent.
What's This Article About?
This article covers how I designed and built this agent from scratch. I'll walk you through:
- The Problem: Why humans struggle with this (too much text).
- The Solution: Using Vector Search to find policies and LLMs to reason about them.
- The Code: A full Python implementation you can run today.
Tech Stack
For this experiment, I stuck to a robust Python stack:
- Python 3.12: The core language.
- Vector Database Logic: Custom simulation (can swap with Chroma/FAISS).
- LLM Simulation: A rule-based reasoning engine (to keep it virtually free for this demo).
- Mermaid.js: For generating the architecture diagrams.
Why Read It?
If you're interested in Applied AI, this is a practical example. It's not just "chat with PDF." It's an agent that takes action (Approves/Denies) based on strict criteria. Plus, the terminal animation is pretty cool.
Let's Design
I started by sketching out the flow. I needed a way to ingest unstructured data (notes) and structured data (policies).
In my opinion, the critical part is the Decision Engine. It can't just guess; it needs to find evidence.
The data flow looks like this:
- Provider sends a request (e.g., "MRI Lumbar").
- Agent searches the vector store for the relevant policy ID.
- Agent extracts the patient's history (conservative therapy, duration of pain).
- LLM compares the two and yields a result.
Let’s Get Cooking
Here is the core logical flow of the system:
The Knowledge Base
First, I needed a way to store policies. In a real production system, I'd use Pinecone or Weaviate. For this local version, I built a lightweight simulated vector store.
The key here is that the search function simulates a semantic retrieval. If I query "Back Pain," it knows to fetch the "Lumbar Spine Policy."
The Reasoner (Analyzer)
This is where the magic happens. I wrote an Analyzer class that takes the retrieved policy and the patient's notes.
def analyze_claim(self, patient_data: Dict[str, Any], policy: Dict[str, Any]):
"""
Simulates LLM analysis of patient notes against policy criteria.
"""
print(f" [Analyzer] Initializing LLM Context...")
time.sleep(0.4)
score = 0
met_criteria = []
# Checking for specific clinical keywords
for criterion in policy.get("criteria", []):
keywords = criterion.lower().split()
if "pain" in keywords and "pain" in notes:
match = True
met_criteria.append(criterion)
score += 33
I found that breaking the policy down into individual criteria points (e.g., "Tried PT for 6 weeks") makes the LLM's job much easier than asking for a holistic "Yes/No."
Let's Setup
If you want to run this yourself, the setup is straightforward.
Step 1: Clone the Repo
git clone https://github.com/aniket-work/autonomous-medical-pre-auth-agent.git
cd autonomous-medical-pre-auth-agent
Step 2: Install Dependencies
I kept the requirements minimal.
pip install -r requirements.txt
Step 3: Run the Agent
python main.py
Let's Run
When I run the agent, it mimics a real-time data stream.
I observed that adding small delays (simulated latency) makes the tool feel much more "intelligent" and substantial than if it just printed the result instantly. It gives the user confidence that work is being done.
Here is what the final output looks like in my terminal:
=== Autonomous Medical Pre-Authorization Agent ===
➜ Loading Electronic Health Record (EHR) stream...
➜ Querying Policy Knowledge Base...
[KnowledgeBase] Found match: POL-7892 (Similarity: 0.89)
➜ Initiating AI Clinical Review...
[Analyzer] Reasoning complete based on 3 evidence points.
============================================================
FINAL DETERMINATION REPORT
============================================================
Patient ID : PT-48291
Procedure : MRI Lumbar Spine Authorization Policy
Confidence Score : 96%
Status : APPROVED
------------------------------------------------------------
Evidence Found:
[x] Patient must have lower back pain > 6 weeks
[x] Patient must have tried conservative therapy
============================================================
Closing Thoughts
In my opinion, agents like this are the future of healthcare administration. We don't need to replace doctors; we need to replace the paperwork that burns them out.
Working on this PoC showed me that even simple logic, when applied correctly with RAG, can solve very complex workflow problems.
Disclaimer: The views and opinions expressed here are solely my own and do not represent the views, positions, or opinions of my employer or any organization I am affiliated with. The content is based on my personal experience and experimentation and may be incomplete or incorrect. Any errors or misinterpretations are unintentional, and I apologize in advance if any statements are misunderstood or misrepresented.




Top comments (0)