DEV Community

Cover image for Building an Autonomous Medical Pre-Authorization Agent: My Experiment with AI in Healthcare
Aniket Hingane
Aniket Hingane

Posted on

Building an Autonomous Medical Pre-Authorization Agent: My Experiment with AI in Healthcare

TL;DR

I built an "Autonomous Medical Pre-Authorization Agent" that simulates a multi-agent workflow to review patient clinical notes against insurance policies. Using Python and simple agentic patterns, I explored how AI can potentially streamline one of the most painful administrative bottlenecks in healthcare. The project includes a Policy Analyst, Clinical Reviewer, and Decision Engine, all working together to approve or deny requests with explainable rationale.

(GitHub Repo Link at the bottom!)


Introduction

In my opinion, one of the most impactful areas for Generative AI isn't just writing emails—it's decision support in high-stakes domains like healthcare. I've always been fascinated by the complexity of medical billing and authorization. The friction between providers wanting to treat patients and payers needing to verify necessity creates a massive administrative burden.

I thought, "Why not build a Proof of Concept (PoC) agent that can handle this?" Not to replace doctors, but to act as a tireless assistant that can parse 50-page policy documents and match them against patient records in seconds.

This article documents my journey building a Medical Pre-Authorization Agent. It's an experimental project where I designed a system to autonomously ingest insurance rules, analyze a patient's history, and render a coverage decision with a confidence score.


What's This Article About?

This article is a technical walkthrough of building a specialized AI agent system. It covers:

  1. Designing a Multi-Agent Architecture: separating concerns between policy understanding and clinical analysis.
  2. Structuring Medical Data: handling mock Electronic Health Records (EHR) and policy documents.
  3. The Decision Logic: How to synthesize findings into a final "Approved" or "Denied" verdict.
  4. Building the Tooling: Creating a professional CLI interface to visualize the agent's "thinking" process.

I wrote this to show that "Agentic AI" doesn't have to be overly complex. With the right structure, you can model sophisticated reasoning tasks effectively.


Tech Stack

For this experiment, I kept the stack lean but powerful:

  • Python 3.12: The backbone of the logic.
  • Rich: I used this library extensively to create a professional, readable terminal output. It’s crucial for debugging agent flows.
  • Mermaid.js: For generating the architecture diagrams (via Python scripts).
  • Mock Data Generators: Custom classes to create realistic but synthetic patient scenarios (e.g., MRI requests for lower back pain).

Architecture Diagram


Why Read It?

If you are:

  • A developer looking to move beyond simple chatbots to "Agents that do things".
  • Interested in the intersection of AI and Healthcare operations.
  • Curious about how to structure a Python project for clarity and maintainability.

Then this experiment is for you. I share my exact thought process, the code structure, and the visual results.


Let's Design

I started by breaking down the human process of prior authorization. Usually, a nurse or medical coder:

  1. Reads the specific insurance policy for the requested procedure.
  2. Identifies the "Medical Necessity Criteria" (the checklist).
  3. Scours the patient's chart for evidence matching that checklist.
  4. Makes a decision based on the match.

I decided to mimic this exact flow with three distinct agents:

Process Flow

  1. Policy Analyst: "Reads" the policy and extracts the rules.
  2. Clinical Reviewer: "Reads" the patient notes and finds the proof.
  3. Decision Engine: Compares the two and acts as the judge throughout the process.

This separation of concerns is critical. If I put everything into one massive prompt, the logic gets muddy. By splitting them, I can debug exactly where the failure happens—did we miss a policy rule? Or did we miss a clinical note?


Let's Get Cooking

Here is how I structured the implementation. I'll walk you through the core components.

1. The Data Models

First, I needed to define what our "documents" look like. I used Python dataclasses to create structured representations of our mock data.

@dataclass
class PatientCase:
    patient_id: str
    name: str
    age: int
    diagnosis_code: str
    procedure_code: str
    clinical_notes: str

@dataclass
class InsurancePolicy:
    policy_id: str
    policy_name: str
    procedure_code: str
    criteria: List[str]
Enter fullscreen mode Exit fullscreen mode

In my opinion, defining strict types early on saves a headache later. It ensures every agent knows exactly what data format to expect.

2. The Clinical Reviewer Agent

This agent is responsible for the "investigation" phase. It takes the un-structured text of clinical notes and tries to find structured boolean evidence.

class ClinicalReviewer:
    def __init__(self):
        self.role = "Clinical Reviewer"

    def review_case(self, case: PatientCase) -> Dict[str, bool]:
        logger.info(f"[{self.role}] Reviewing Clinical Notes for Patient: {case.patient_id}...")

        # In a real scenario, an LLM would process the text here.
        # For this PoC, we simulate the extraction logic.
        findings = {
            "pain_duration_met": "8 weeks" in case.clinical_notes,
            "conservative_therapy_met": "6 weeks of physical therapy" in case.clinical_notes,
            "neuro_findings_met": "Positive Straight Leg Raise" in case.clinical_notes,
            "red_flags_absent": "No weight loss, fever" in case.clinical_notes
        }

        return findings
Enter fullscreen mode Exit fullscreen mode

I designed this to be deterministic for the demo, but in a production version, this review_case method would be an API call to a model like Gemini or GPT-4, passing the notes and asking for JSON output.

3. The Decision Engine

This is where the magic happens. The engine takes the policy criteria and the clinical findings and computes a verdict.

class DecisionEngine:
    def __init__(self):
        self.role = "Medical Director (AI)"

    def make_decision(self, findings: Dict[str, bool], criteria: List[str]) -> Dict[str, str]:
        # Simple logic: If all clinical findings match the policy criteria, approve.
        all_criteria_met = all(findings.values())

        decision = "APPROVED" if all_criteria_met else "DENIED"
        confidence = "High (98.5%)"

        return {
            "status": decision,
            "confidence": confidence,
            "rationale": "Patient meets all medical necessity guidelines."
        }
Enter fullscreen mode Exit fullscreen mode

I think this modularity is powerful. I could swap out the "Decision Engine" for a human-in-the-loop if the confidence score is low, without rewriting the other agents.


Let's Setup

If you want to run this experiment yourself, I've made it straightforward.

Step 1: Clone the Repository

git clone https://github.com/aniket-work/medical-preauth-agent.git
cd medical-preauth-agent
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Dependencies

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Step 3: Run the Agent

python main.py
Enter fullscreen mode Exit fullscreen mode

I made sure to include a requirements.txt so you don't have to guess the versions. It relies mainly on rich for the interface and requests if you want to extend it with real API calls.


Let's Run

When you run the agent, you'll see a simulated real-time processing of a medical case.

I designed the terminal output to be "hyper-realistic"—showing the logs as the agents "read" and "think". It builds trust in the system when you can see the intermediate steps rather than just a black-box answer.

The final output is a clean summary table:

============== REPORT CARD ==============
Patient ID:    PT-2024-8921
Procedure:     MRI Lumbar Spine (72148)
Status:        APPROVED [✅]
Confidence:    98.5%
Reasoning:     Patient meets all medical necessity guidelines for MRI Lumbar Spine.
=========================================
Enter fullscreen mode Exit fullscreen mode

In my experience, presentation matters. Even a backend script should communicate its value clearly to the user.


Closing Thoughts

Building this Autonomous Medical Pre-Authorization Agent was a compelling exercise. It reinforced my belief that "Agentic workflows"—where you break a complex job into smaller, specialized roles are the future of automation in complex domains.

Instead of asking one giant AI model to "do billing," we can have a "Reader," a "Checker," and a "Decider." Each can be optimized, tested, and audited independently.

The full code is available below. Feel free to fork it and add your own "Denial Appeals" agent!

GitHub Repo: https://github.com/aniket-work/medical-preauth-agent


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)