How to Build a Gemini AI Agent to Automate Medical Prior Authorizations

Akram Chauhan
Akram Chauhan
9 min read185 views
How to Build a Gemini AI Agent to Automate Medical Prior Authorizations

Have you ever dealt with health insurance? If you have, you’ve probably encountered the dreaded "prior authorization." It's that mountain of paperwork and phone calls needed just to get approval for a medication your doctor already said you need. It’s a frustrating, time-consuming process that feels like it was designed by a committee of robots.

Well, what if we could fight robots with a smarter robot?

I’m not talking about science fiction. I’m talking about building our own AI agent—a little digital assistant—that can handle this entire workflow for us. An agent that can read a patient's chart, figure out what the insurance company needs, and submit the paperwork all on its own.

Today, we're going to do just that. We'll use Google's powerful Gemini model to build a simple but fully functional agent that can navigate a simulated medical system. Don't worry if you're not a machine learning wizard. I'll walk you through every step, explaining the "why" behind the code. Let's build something cool.

First Things First: Setting Up Our AI's Workspace

Before we can build our agent, we need to set up its environment. Think of this as preparing your workshop. We need to connect to our power source (the Gemini API) and pick our primary tool (the specific Gemini model we'll use).

Here’s a look at the initial setup code:

# We'll start by installing the necessary Google library
pip install -q -U google-generative-ai

import google.generativeai as genai
from google.colab import userdata # Or use your preferred method for secrets
import os
import getpass
import json
import time

# Securely get your API key
try:
    GOOGLE_API_KEY = userdata.get('GOOGLE_API_KEY')
except:
    print("Please enter your Google API Key:")
    GOOGLE_API_KEY = getpass.getpass("API Key: ")

genai.configure(api_key=GOOGLE_API_KEY)

# Let's find the best available model automatically
print("\n Scanning for available models...")
available_models = [m.name for m in genai.list_models()]
target_model = ""

# Prefer Flash for speed and cost, but fall back to others if needed
if 'models/gemini-1.5-flash' in available_models:
    target_model = 'gemini-1.5-flash'
elif 'models/gemini-pro' in available_models:
    target_model = 'gemini-pro'
else:
    # Find any model that can generate content
    for m in available_models:
        if 'generateContent' in genai.get_model(m).supported_generation_methods:
            target_model = m
            break

if not target_model:
    raise ValueError(" No text generation models found for this API key.")

print(f" Selected Model: {target_model}")
model = genai.GenerativeModel(target_model)

What we're doing here is pretty straightforward. First, we securely load our Google API key. No hardcoding secrets, please! Then, instead of just picking a model like gemini-pro and hoping for the best, the code intelligently scans for the best available model. It prioritizes newer, faster models like gemini-1.5-flash but has fallbacks.

This little bit of logic makes our code more robust and future-proof. It’s a small detail, but it’s the kind of thing that separates a quick-and-dirty script from a solid foundation.

Giving Our Agent Hands and Eyes: The Tools

An AI model on its own is just a brain in a jar. It can think, reason, and write, but it can't do anything in the real world. To make it useful, we need to give it tools.

For our medical agent, it needs two key abilities:

  1. Search a patient's medical records (EHR).
  2. Submit a prior authorization form.

We'll simulate these actions with a Python class. This is where we pretend to connect to external systems.

class MedicalTools:
    def __init__(self):
        # A simple, fake database of a patient's medical records
        self.ehr_docs = [
            "Patient: John Doe | DOB: 1980-05-12",
            "Visit 2023-01-10: Diagnosed with Type 2 Diabetes. Prescribed Metformin.",
            "Visit 2023-04-15: Patient reports severe GI distress with Metformin. Discontinued.",
            "Visit 2023-04-20: BMI recorded at 32.5. A1C is 8.4%.",
            "Visit 2023-05-01: Doctor recommends starting Ozempic (Semaglutide)."
        ]

    def search_ehr(self, query):
        print(f" [Tool] Searching EHR for: '{query}'...")
        results = [doc for doc in self.ehr_docs if any(q.lower() in doc.lower() for q in query.split())]
        if not results:
            return "No records found."
        return "\n".join(results)

    def submit_prior_auth(self, drug_name, justification):
        print(f" [Tool] Submitting claim for {drug_name}...")
        justification_lower = justification.lower()
        
        # This is our fake insurance company's logic
        if "metformin" in justification_lower and ("discontinued" in justification_lower or "intolerance" in justification_lower):
            if "bmi" in justification_lower and "32" in justification_lower:
                return "SUCCESS: Authorization Approved. Auth ID: #998877"
        
        return "DENIED: Policy requires proof of (1) Metformin failure and (2) BMI > 30."

Look at what this code does. The search_ehr function lets the agent "look up" information. The submit_prior_auth function simulates the insurance company's approval logic. It will only approve the request if the justification proves the patient tried Metformin first and has a BMI over 30.

By creating these tools, we’re giving our agent a way to interact with its world. It's no longer just generating text; it's taking actions and getting real feedback.

The Agent's Playbook: Setting the Rules of the Game

Now we get to the core of our agent: its "brain" and its instruction manual. This is handled by a system prompt—a special set of instructions we give the AI to define its personality, its goal, and, most importantly, its rules.

This is where we tell the agent how to think.

class AgenticSystem:
    def __init__(self, model, tools):
        self.model = model
        self.tools = tools
        self.history = []
        self.max_steps = 6 # A safety stop to prevent infinite loops
        self.system_prompt = """
You are an expert Medical Prior Authorization Agent. Your goal is to get approval for a medical procedure/drug.

You have access to these tools:
1. search_ehr(query)
2. submit_prior_auth(drug_name, justification)

RULES:
1. ALWAYS think before you act.
2. You MUST output your response in STRICT JSON format:
   {
     "thought": "Your reasoning here",
     "action": "tool_name_or_finish",
     "action_input": "argument_string_or_dict"
   }
3. Do not guess patient data. Use 'search_ehr'.
4. If you have the evidence, use 'submit_prior_auth'.
5. If the task is done, use action "finish".
"""

This prompt is critical. We're not just asking it to "be helpful." We're giving it a specific role ("Medical Prior Authorization Agent") and a clear list of available tools.

But the most important part is Rule #2: the strict JSON format. This is the secret sauce. By forcing the AI to respond in a structured thought, action, action_input format, we turn it from a creative writer into a predictable, logical worker. We can parse its response and know exactly what it wants to do next. This makes the whole system reliable and easy to control.

The Heartbeat of the Agent: The Think-Act-Observe Loop

Okay, we have the brain (Gemini), the tools (our MedicalTools class), and the rules (the system prompt). Now we need to connect them all in a loop that allows the agent to work step-by-step.

This is the main "run" loop. It’s where the agent truly comes to life.

    def execute_tool(self, action_name, action_input):
        if action_name == "search_ehr":
            return self.tools.search_ehr(action_input)
        elif action_name == "submit_prior_auth":
            # Making sure the input is in the correct format
            if isinstance(action_input, str):
                return "Error: submit_prior_auth requires a dictionary."
            return self.tools.submit_prior_auth(**action_input)
        else:
            return "Error: Unknown tool."

    def run(self, objective):
        print(f" AGENT STARTING. Objective: {objective}\n" + "-"*50)
        self.history.append(f"User: {objective}")

        for i in range(self.max_steps):
            print(f"\n STEP {i+1}")
            
            # 1. THINK: Give the model its prompt and history
            prompt = self.system_prompt + "\n\nHistory:\n" + "\n".join(self.history) + "\n\nNext JSON:"
            
            try:
                response = self.model.generate_content(prompt)
                text_response = response.text.strip().replace("```json", "").replace("```", "")
                agent_decision = json.loads(text_response)
            except Exception as e:
                print(f" Error parsing AI response. Retrying... ({e})")
                continue

            print(f" THOUGHT: {agent_decision['thought']}")
            print(f" ACTION: {agent_decision['action']}")
            
            if agent_decision['action'] == "finish":
                print(f"\n TASK COMPLETED: {agent_decision['action_input']}")
                break

            # 2. ACT: Execute the chosen tool
            tool_result = self.execute_tool(agent_decision['action'], agent_decision['action_input'])
            
            # 3. OBSERVE: Show the result and update history
            print(f" OBSERVATION: {tool_result}")
            self.history.append(f"Assistant: {text_response}")
            self.history.append(f"System: {tool_result}")

            if "SUCCESS" in str(tool_result):
                print("\n SUCCESS! The Agent successfully navigated the insurance portal.")
                break

Let’s break down this loop. It’s a simple but powerful cycle:

  1. Think: The agent looks at the original goal and everything that has happened so far (the history). It then decides on its next move, outputting its plan in that neat JSON format.
  2. Act: Our code takes the action and action_input from the AI's response and runs the corresponding tool.
  3. Observe: The result from the tool (the tool_result) is captured. This result, along with the agent's own thought process, is added back to the history.

This Think -> Act -> Observe cycle repeats, allowing the agent to perform multiple steps, gather information, and react to new developments until it either solves the problem or hits its step limit.

Putting It All Together: Let's Run This Thing!

We've built all the pieces. Now for the fun part: let's give our agent its mission and watch it work.

# Create an instance of our tools
tools_instance = MedicalTools()

# Create an instance of our agent, giving it the model and the tools
agent = AgenticSystem(model, tools_instance)

# Give the agent its objective
agent.run("Please get prior authorization for Ozempic for patient John Doe.")

When you run this, you'll see a step-by-step log of the agent's "mind" at work. It will look something like this:

  • Step 1: The agent will think, "I need to find evidence for why John Doe needs Ozempic. I should search his medical records." It will then call the search_ehr tool.
  • Observation: The tool returns the patient's records, including the Metformin failure and the high BMI.
  • Step 2: The agent sees this new information. It thinks, "Aha! I have everything I need. The patient failed Metformin and has a BMI of 32.5. I can now submit the claim." It will then call the submit_prior_auth tool with the correct justification.
  • Observation: The tool returns "SUCCESS: Authorization Approved."
  • Step 3: The agent sees the success message and concludes its job is done. It uses the finish action.

And just like that, our AI agent successfully navigated the bureaucracy on its own.

This is obviously a simplified example, but the core framework is incredibly powerful. This isn't just a chatbot that gives you text answers. It's a system that can reason, use tools, and accomplish multi-step goals. You can imagine expanding this by adding more tools, more complex logic, or even connecting it to real-world APIs.

What we've built here is a blueprint for the future of automation—where AI doesn't just talk, it does. And thankfully, it might just be the thing that finally saves us from all that paperwork.

Tags

AI Machine Learning Google AI Automation Agentic AI Python Digital Transformation AI Productivity AI in Healthcare Large Language Models AI agents AI Workflow Automation Gemini AI Prior Authorization Automation Healthcare Workflow Automation Medical Prior Authorization Automated Medical Evidence AI for Healthcare Administration Coding AI Agents AI Development Tutorial

Stay Updated

Get the latest articles and insights delivered straight to your inbox.

We respect your privacy. Unsubscribe at any time.

Aicosoft

AI & Technology News, Insights & Innovation

AICOSOFT delivers cutting-edge AI news, technology breakthroughs, and innovation insights. Stay informed about artificial intelligence, machine learning, robotics, and the latest tech trends shaping tomorrow.

Connect With Us

© 2026 Aicosoft. All rights reserved.