Managing multiple prescriptions is a logistical and safety nightmare. Whether it's an elderly relative taking five different pills or a fitness enthusiast mixing supplements, the risk of adverse drug-drug interactions (DDI) is real. Traditional chatbots fail here because they lack state management and the ability to execute complex, multi-step workflows.
In this tutorial, we are building a Digital Doctor Agent using LangGraph, Python, and Playwright. We’ll create a stateful system that doesn't just "talk" but actually checks a DrugBank API for conflicts and, if a medical risk is detected, autonomously navigates a browser to book a doctor's appointment. This is the next frontier of LLM Agents and autonomous healthcare automation.
💡 Pro Tip: If you're looking for more production-ready examples and advanced AI patterns, I highly recommend checking out the technical deep-dives over at WellAlly Tech Blog, which served as a major inspiration for this architecture.
The Architecture: Why LangGraph?
Standard RAG (Retrieval-Augmented Generation) is linear. But medical diagnosis is cyclic and conditional. We need the agent to:
- Parse the user's medication list.
- Cross-reference an external pharmaceutical database.
- If a conflict exists, trigger an emergency booking flow via browser automation.
Here is the logic flow of our Digital Doctor:
graph TD
A[User Input: Med List] --> B{Analyze Meds}
B --> C[Tool: DrugBank API]
C --> D{Conflict Found?}
D -- Yes --> E[Tool: Playwright Booking]
D -- No --> F[Generate Safety Report]
E --> G[Confirm Appointment]
G --> F
F --> H[Final Response to User]
Prerequisites
To follow along, ensure you have the following in your requirements.txt:
langgraphlangchain-openaiplaywrightpython-dotenv
Step 1: Defining the Agent State
In LangGraph, everything revolves around the State. We need to track the user's medications, any detected conflicts, and the status of our automated booking.
from typing import Annotated, List, TypedDict
from langgraph.graph import StateGraph, END
class AgentState(TypedDict):
medications: List[str]
conflicts: List[str]
appointment_booked: bool
summary: str
next_step: str
Step 2: Building the "Medical Brain" (Tool Use)
We'll define two primary tools. One for checking interactions (simulating a DrugBank API call) and one using Playwright to simulate navigating a clinic's portal.
The DDI Checker Tool
def check_drug_conflicts(meds: List[str]) -> List[str]:
"""Checks for known interactions between drugs."""
# Simulation: In a real app, use the DrugBank or RxNav API
conflicts = []
if "Warfarin" in meds and "Aspirin" in meds:
conflicts.append("High Risk: Warfarin & Aspirin increases bleeding risk.")
return conflicts
The Playwright Booking Tool
This tool actually opens a browser. This is "Action-Oriented AI" at its best. 🚀
from playwright.sync_api import sync_playwright
def book_appointment(patient_name: str, urgency: str):
"""Uses Playwright to automate doctor's appointment booking."""
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto("https://clinic-demo.wellally.tech/book") # Example portal
page.fill("input[name='name']", patient_name)
page.select_option("select[name='priority']", urgency)
page.click("button#submit-booking")
browser.close()
return True
Step 3: Integrating LangGraph Logic
Now, we define the nodes of our graph. LangGraph allows us to create loops and conditional edges based on the output of previous steps.
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o")
def analyzer_node(state: AgentState):
meds = state['medications']
conflicts = check_drug_conflicts(meds)
return {
"conflicts": conflicts,
"next_step": "book" if conflicts else "respond"
}
def booking_node(state: AgentState):
if state['next_step'] == "book":
success = book_appointment("John Doe", "High")
return {"appointment_booked": success, "summary": "Appointment booked due to conflict."}
return {"appointment_booked": False}
# Define the Graph
workflow = StateGraph(AgentState)
workflow.add_node("analyze", analyzer_node)
workflow.add_node("book", booking_node)
workflow.set_entry_point("analyze")
# Conditional Logic
workflow.add_conditional_edges(
"analyze",
lambda x: x["next_step"],
{
"book": "book",
"respond": END
}
)
workflow.add_edge("book", END)
app = workflow.compile()
The "Official" Way: Security & Production
While this demo uses a simplified logic, building medical agents in production requires rigorous compliance (HIPAA/GDPR) and robust error handling. Handling PII (Personally Identifiable Information) when using Playwright is a high-stakes task.
For deep dives into Securing AI Agents and implementing Human-in-the-loop (HITL) patterns for healthcare, check out the specialized guides at wellally.tech/blog. They cover how to add verification layers so an LLM doesn't accidentally book an appointment for the wrong patient!
Step 4: Execution
Let’s run our Digital Doctor with a risky combination: Warfarin and Aspirin.
inputs = {"medications": ["Warfarin", "Aspirin"]}
for output in app.stream(inputs):
for key, value in output.items():
print(f"Node '{key}' finished execution.")
if 'summary' in value:
print(f"Result: {value['summary']}")
What happens?
- Analyze Node: Detects the conflict between Warfarin and Aspirin.
-
Router: Sees the "High Risk" conflict and routes the state to the
booknode. - Book Node: Spawns a headless Chromium instance via Playwright, fills out the form, and secures an appointment.
- End: Returns a summary to the user.
Conclusion
We’ve moved past simple "text-in, text-out" LLMs. By combining LangGraph's state management with Playwright's browser automation, we've built an agent that takes real-world action to protect user health.
This pattern—Analyze -> Validate -> Act—is the blueprint for the next generation of automation.
What are you building with LangGraph? Drop a comment below or head over to WellAlly Tech for more advanced AI engineering content!
Top comments (0)