Stop Manually Booking Appointments: Building an Autonomous AI Health Agent with Playwright and GPT-4o
We’ve all been there. You get a notification from your smartwatch saying your heart rate has been a bit funky, or your blood oxygen is dipping. Usually, we ignore it until it becomes a problem. But what if. . .

We’ve all been there. You get a notification from your smartwatch saying your heart rate has been a bit funky, or your blood oxygen is dipping. Usually, we ignore it until it becomes a problem. But what if your personal AI was looking out for you? 🤖In this tutorial, we are building anAutonomous Health Agent. This isn't just a notification bot; it's a proactive system that usesPlaywright browser automation,OpenAI Function Calling, andPythonto monitor your health trends and—if things look suspicious for three days straight—literally opens a browser and books a doctor's appointment for you. By leveragingAutonomous AI AgentsandPlaywright automation, we are moving from "Passive Monitoring" to "Active Intervention. " This is the future ofHealth Tech Automation. 🏗 The ArchitectureBefore we dive into the code, let's look at how the data flows from a "scary heart rate" to a "confirmed appointment. "graph TD A[Wearable Data/Health Logs] --> B{3-Day Anomaly Check} B -- Normal --> C[Stay Healthy! 🟢] B -- Abnormal --> D[Trigger AI Agent 🤖] D --> E[OpenAI Function Calling] E --> F[Playwright Browser Automation] F --> G[Hospital Booking Platform] G --> H[Appointment Confirmation 🏥] H --> I[Notify User via SMS/Email]Enter fullscreen modeExit fullscreen mode🛠 PrerequisitesTo follow along, you’ll need:Python 3. 10+Playwright: The king of modern browser automation. OpenAI API Key: For the "brain" of our agent. A healthy dose of curiosity! 🥑pipinstallplaywright openai pydantic playwrightinstallchromiumEnter fullscreen modeExit fullscreen mode👨💻 Step 1: Defining the "Brain" (OpenAI Function Calling)We don't want the LLM to just "talk" about booking an appointment; we want it to actuallyexecutethe action. We'll use OpenAI's Function Calling to bridge the gap between text and code. importjsonfromopenaiimportOpenAIclient=OpenAI()# Define the tool our agent can usetools=[{"type":"function","function":{"name":"book_doctor_appointment","description":"Books a medical appointment based on department and symptoms","parameters":{"type":"object","properties":{"department":{"type":"string","enum":["Cardiology","General Medicine","Pulmonology"]},"preferred_time":{"type":"string","description":"Morning or Afternoon"},"reason":{"type":"string"}},"required":["department","preferred_time","reason"]}}}]Enter fullscreen modeExit fullscreen mode🌐 Step 2: The "Hands" (Playwright Automation)Now, let's write the Playwright logic. This function will be called by the Agent to interact with a (mock) hospital portal. fromplaywright. sync_apiimportsync_playwrightdefbook_doctor_appointment(department,preferred_time,reason):print(f"🚀 Launching browser to book{department}. . . ")withsync_playwright()asp:browser=p. chromium. launch(headless=False)# Headless=False so we can see the magic!page=browser. new_page()# Navigate to the portalpage. goto("https://mock-hospital-portal. com/booking")# Fill out the formpage. select_option("select#dept",label=department)page. fill("textarea#reason",reason)ifpreferred_time=="Morning":page. check("input#slot-am")else:page. check("input#slot-pm")# Submit (Commented out for safety in this demo!)# page. click("button#confirm-booking")print(f"✅ Successfully booked{department}for the{preferred_time}!")browser. close()return"Booking Successful"Enter fullscreen modeExit fullscreen mode🧠 Step 3: The Orchestrator LogicThe "Agent" logic checks the health data. If the heart rate or SpO2 levels are abnormal for 3 consecutive days, it triggers the AI. defcheck_health_and_trigger_agent(health_logs):# Logic: If heart_rate > 100 for 3 consecutive entriesabnormal_count=sum(1forloginhealth_logs[-3:]iflog['heart_rate']>100)ifabnormal_count>=3:print("🚨 Health Anomaly Detected! Engaging AI Agent. . . ")prompt=f"The user has had an elevated heart rate for 3 days. Logs:{health_logs[-3:]}. Please book a Cardiology appointment. "response=client. chat. completions. create(model="gpt-4o",messages=[{"role":"user","content":prompt}],tools=tools)# Check if the tool was calledtool_call=response. choices[0]. message. tool_calls[0]iftool_call. function. name=="book_doctor_appointment":args=json. loads(tool_call. function. arguments)book_doctor_appointment(**args)# Mock Data: 3 days of high heart ratehealth_data=[{"day":1,"heart_rate":105},{"day":2,"heart_rate":110},{"day":3,"heart_rate":108}]check_health_and_trigger_agent(health_data)Enter fullscreen modeExit fullscreen mode🏥 Going Beyond: The "Official" WayWhile this DIY script is a great "Learning in Public" project, building production-grade healthcare agents requires much stricter security, HIPAA compliance, and robust error handling. For more advanced patterns on building resilient AI agents and production-ready automation workflows, I highly recommend checking out theWellAlly Tech Blog. They have some incredible deep dives into how AI is being used to bridge the gap between data and real-world clinical actions. It’s been a huge source of inspiration for my recent builds! 💡🚀 ConclusionAnd there you have it! A fully functional (well, "mock" functional) AI Agent that can literally save you a trip to the computer by booking your doctor's visits for you. Key Takeaways:Playwrightisn't just for testing; it's the "body" of your AI. Function Callingallows LLMs to interact with the real world. Proactive AIis 10x more valuable than reactive AI. What would you automate next? A bot that books a massage when your stress levels are high? Let me know in the comments! 👇