DEV Community

wellallyTech
wellallyTech

Posted on

Automate the Impossible: Building a Closed-Loop Medical Appointment Agent with Browser-use & GPT-4V πŸš€

Have you ever tried navigating a clunky hospital portal? Between the cryptic department names and the endless clicking, it’s a UI nightmare. But what if your computer could "see" the screen, reason like a human, and handle the entire appointment process for you?

In this guide, we’re building a Closed-Loop Medical Agent. We will leverage Vision LLM Agents, browser automation, and the cutting-edge Browser-use framework to transform a simple symptom description into a confirmed doctor's appointment. This isn't just a scraper; it's a self-correcting AI that interacts with the web in real-time.


The Vision: Why "Closed-Loop" Matters?

Traditional automation breaks the moment a UI change occurs. By using GPT-4V (Vision) and Playwright, our agent doesn't rely on fragile CSS selectors. It looks at the page, understands the context, and decides where to click next.

If you’re interested in scaling these patterns for enterprise use, I highly recommend checking out the advanced architectural deep-dives at WellAlly Tech Blog, which served as a major inspiration for this autonomous workflow.


πŸ— The Architecture

The system operates in a feedback loop: Observation -> Reasoning -> Action -> Validation.

graph TD
    A[User: 'My throat hurts and I have a fever'] --> B[GPT-4V: Analyze Symptoms]
    B --> C[Agent: Identify 'Otolaryngology' Department]
    C --> D[Browser-use: Open Medical Portal]
    D --> E{Is Dept Visible?}
    E -- No --> F[Search/Scroll Action]
    E -- Yes --> G[Select Doctor & Time]
    G --> H[Final Confirmation]
    H --> I[Validation: Success Screenshot]
    F --> D
Enter fullscreen mode Exit fullscreen mode

πŸ›  Tech Stack

  • Browser-use: The ultimate wrapper for making LLMs control browsers.
  • LangChain: For orchestrating the agent logic.
  • Playwright: The engine that powers the actual browser instance.
  • GPT-4o/GPT-4V: The "brain" that provides vision and reasoning.

πŸ’» Implementation: Step-by-Step

1. Environment Setup

First, let's install the core dependencies. We need browser-use and the standard LangChain OpenAI package.

pip install browser-use langchain-openai playwright
playwright install
Enter fullscreen mode Exit fullscreen mode

2. Initializing the Vision Agent

The magic of browser-use is that it allows us to pass a simple "Task" string, and the agent handles the multi-step navigation.

from langchain_openai import ChatOpenAI
from browser_use import Agent
import asyncio

# Initialize the Vision-capable LLM
llm = ChatOpenAI(model="gpt-4o", temperature=0)

async def run_medical_agent(symptoms: str):
    # The prompt defines the goal and the constraints
    task = f"""
    Go to the 'https://example-medical-portal.com'
    1. Based on the symptoms: '{symptoms}', determine the correct department.
    2. Search for the department and find a doctor with the earliest availability.
    3. Select a time slot and stop right before the 'Confirm Payment' button.
    4. Take a screenshot of the final selection.
    """

    agent = Agent(
        task=task,
        llm=llm,
    )

    result = await agent.run()
    print(f"Agent Execution Summary: {result}")

if __name__ == "__main__":
    asyncio.run(run_medical_agent("I've had a sharp pain in my lower back for 2 days."))
Enter fullscreen mode Exit fullscreen mode

3. Handling Complex UI Logic

The agent doesn't just "click." It perceives. If a popup appears (like a COVID-19 warning or a login prompt), the GPT-4V brain realizes it needs to close it before proceeding. This is the "Closed-Loop" advantage.

4. Safety and Verification

For sensitive tasks like healthcare, we don't want the agent hallucinating. We can enforce Schema Validation by asking the agent to extract specific data into a Pydantic model at the end of the run.


πŸ’‘ Production-Ready Patterns

Building a hobby project is easy, but making it reliable is hard. When moving to production, you need to consider:

  1. Session Management: Reusing browser contexts to stay logged in.
  2. Human-in-the-loop (HITL): Pausing the agent to let a human confirm the appointment time before the final click.
  3. Error Recovery: Using "Backtrack" strategies when the agent gets stuck in a navigation loop.

For more production-grade patterns, including how to handle CAPTCHAs and complex authentication in AI agents, head over to the WellAlly Tech Blog. They have a fantastic series on Agentic Workflows that covers these edge cases in depth.


πŸš€ Conclusion

The era of "clicking" is ending. By combining the vision capabilities of GPT-4o with the execution power of Browser-use, we can build agents that interact with the web exactly like humans doβ€”but faster and more accurately.

Whether it's booking a doctor or automating your expense reports, the recipe is the same: Observe, Reason, Act.

What would you automate if your browser could think? Let me know in the comments! πŸ‘‡

Top comments (0)