A coding guide to designing a complete agent workflow in Gemini for automated medical evidence collection and prior authorization submission

by
0 comments
A coding guide to designing a complete agent workflow in Gemini for automated medical evidence collection and prior authorization submission

In this tutorial, we outline how to setup a fully functional, device-in-use medical pre-authorization agent powered by Gemini. We walk through each component step by step, from safely configuring the model to creating realistic external devices and finally building an intelligent agent loop that reasons, acts, and reacts via fully structured JSON. As we move forward, we see how the system thinks, retrieves evidence, and interacts with simulated medical systems to complete a complex workflow. check it out full code here,

!pip install -q -U google-generative-ai


import google.generativeai as genai
from google.colab import userdata
import os
import getpass
import json
import time


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)


print("n🔍 Scanning for available models...")
available_models = (m.name for m in genai.list_models())
target_model = ""


if 'models/gemini-1.5-flash' in available_models:
   target_model="gemini-1.5-flash"
elif 'models/gemini-1.5-flash-001' in available_models:
   target_model="gemini-1.5-flash-001"
elif 'models/gemini-pro' in available_models:
   target_model="gemini-pro"
else:
   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)

We set up our environment and automatically detect the best available Gemini model. We configure the API key securely and let the system choose the most capable model without any hardcoding. This ensures that we start the tutorial with a clean, flexible and reliable foundation. check it out full code here,

class MedicalTools:
   def __init__(self):
       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()
       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."

We define the medical devices that our agent can use during the workflow. We simulate an EHR search and a pre-authorization submission system so that the agent has real tasks to perform. By doing this, we base the agent’s reasoning on tool-enabled interactions rather than plain text generation. check it out full code here,

class AgenticSystem:
   def __init__(self, model, tools):
       self.model = model
       self.tools = tools
       self.history = ()
       self.max_steps = 6
      
       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".
       """

We initialize the agent and provide its full system prompt. We define the rules, the JSON response format, and the expectation that the agent should think before acting. This provides us with a controlled, deterministic structure for building a secure and traceable agent loop. check it out full code here,

 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":
           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}")
           prompt = self.system_prompt + "nnHistory:n" + "n".join(self.history) + "nnNext 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
          
           tool_result = self.execute_tool(agent_decision('action'), agent_decision('action_input'))
           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

We implement a core agent loop where logic, device execution, and observation occur step by step. We see the agent decide its next action, execute the tool, update the history, and evaluate the success conditions. This is where the agent really comes alive and performs iterative reasoning. check it out full code here,

tools_instance = MedicalTools()
agent = AgenticSystem(model, tools_instance)
agent.run("Please get prior authorization for Ozempic for patient John Doe.")

We instantiate the tools and agents, then run the entire system end-to-end with a real purpose. We see the entire workflow unfold as the agent navigates through the medical history, validates evidence, and attempts prior authorization. This last snippet shows the entire pipeline working seamlessly.

Finally, we consider how this compact yet powerful framework enables us to design real-world agentic behaviors that go beyond simple text responses. We see our agent’s plan, consult tools, gather evidence and ultimately complete a structured insurance authorization task through completely autonomous logic. This provides confidence that we can now extend the system with additional tools, stronger policies, domain-specific logic or even multi-agent collaboration.


check it out full code here, Feel free to check us out GitHub page for tutorials, code, and notebooksAlso, feel free to follow us Twitter And don’t forget to join us 100k+ ml subreddit and subscribe our newsletter,


Asif Razzaq Marktechpost Media Inc. Is the CEO of. As a visionary entrepreneur and engineer, Asif is committed to harnessing the potential of Artificial Intelligence for social good. Their most recent endeavor is the launch of MarketTechPost, an Artificial Intelligence media platform, known for its in-depth coverage of Machine Learning and Deep Learning news that is technically robust and easily understood by a wide audience. The platform boasts of over 2 million monthly views, which shows its popularity among the audience.

Related Articles

Leave a Comment