How to Integrate Next.js for healthcare with Vercel AI SDK for multi-agent systems
Why this integration matters
If you’re building healthcare workflows, the hard part is not generating text. It’s coordinating multiple agents across intake, triage, scheduling, and clinical summarization without breaking compliance boundaries.
Combining Next.js for healthcare on the frontend/backend edge with Vercel AI SDK for orchestration gives you a clean way to route patient-facing requests into multi-agent systems, stream results back to the UI, and keep the integration layer small enough to audit.
Prerequisites
- •Node.js 18+ and Python 3.10+
- •A Next.js app with an API route or server action
- •Vercel AI SDK installed in your app:
- •
ai - •
@ai-sdk/openaior another provider package
- •
- •A healthcare backend service exposing your agent endpoints
- •API keys for your model provider and internal agent service
- •Basic familiarity with:
- •
streamText() - •
generateText() - •
useChat()on the client - •Next.js Route Handlers in
app/api/*/route.ts
- •
Integration Steps
- •
Set up the healthcare agent gateway in Python
Keep your agent logic outside the UI layer. Expose a small HTTP service that accepts patient context and returns structured outputs from multiple agents like triage, summarization, and scheduling.
from fastapi import FastAPI from pydantic import BaseModel import httpx app = FastAPI() class PatientRequest(BaseModel): patient_id: str message: str @app.post("/agents/dispatch") async def dispatch_agents(req: PatientRequest): triage_agent = {"name": "triage", "priority": "medium"} summary_agent = {"name": "summary", "note": f"Summarize: {req.message}"} schedule_agent = {"name": "scheduler", "action": "find_open_slot"} return { "patient_id": req.patient_id, "agents": [triage_agent, summary_agent, schedule_agent], }This is the contract your Next.js layer will call. Keep it narrow: patient ID, user message, structured result.
- •
Call the Python agent gateway from a Next.js route using Vercel AI SDK
Use a route handler in Next.js to receive chat input, forward it to your healthcare orchestration service, then use Vercel AI SDK to shape the final response.
import os import requests from ai import generate_text from openai import OpenAI HEALTHCARE_AGENT_URL = os.getenv("HEALTHCARE_AGENT_URL") def fetch_agent_plan(patient_id: str, message: str): resp = requests.post( f"{HEALTHCARE_AGENT_URL}/agents/dispatch", json={"patient_id": patient_id, "message": message}, timeout=10, ) resp.raise_for_status() return resp.json() def compose_response(patient_id: str, message: str): plan = fetch_agent_plan(patient_id, message) client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) result = generate_text( model="gpt-4o-mini", prompt=f""" You are a healthcare coordinator. Patient ID: {patient_id} User message: {message} Agent plan: {plan} Return a concise next-step response. """, ) return result.textThe key pattern here is separation of concerns:
- •Python handles multi-agent dispatch
- •Vercel AI SDK handles model interaction and response composition
- •Next.js stays thin and request-driven
- •
Stream responses back to the client
For healthcare workflows, streaming matters when you want immediate acknowledgment while agents work in the background. In Next.js App Router, use
streamText()so the UI can render partial output fast.import os import json import requests from ai import stream_text HEALTHCARE_AGENT_URL = os.getenv("HEALTHCARE_AGENT_URL") def get_streaming_reply(patient_id: str, message: str): plan = requests.post( f"{HEALTHCARE_AGENT_URL}/agents/dispatch", json={"patient_id": patient_id, "message": message}, timeout=10, ).json() return stream_text( model="gpt-4o-mini", prompt=json.dumps({ "role": "healthcare_coordinator", "patient_id": patient_id, "message": message, "agent_plan": plan, "instructions": [ "Acknowledge receipt", "Summarize what each agent will do", "Avoid medical diagnosis unless explicitly provided by clinicians", ], }), ) - •
Add structured outputs for downstream agents
Multi-agent systems break when every step returns free-form prose. Use schema-driven responses so your scheduler, nurse triage bot, or claims workflow can consume deterministic fields.
from pydantic import BaseModel from typing import Literal class CarePlan(BaseModel): urgency: Literal["low", "medium", "high"] next_action: str follow_up_minutes: int def build_care_plan(message: str): result = generate_text( model="gpt-4o-mini", prompt=f""" Convert this patient message into structured care routing data: {message} Return urgency, next_action, and follow_up_minutes. """, ) # In production parse with strict JSON validation before storing or routing. return CarePlan( urgency="medium", next_action=result.text[:120], follow_up_minutes=30, ) - •
Wire the Next.js client to the route handler
On the frontend side, use Vercel AI SDK’s chat hook so clinicians or staff can interact with the system in real time.
# Pseudocode-style Python equivalent for integration testing logic. # In Next.js this maps to useChat() calling /api/chat. import requests payload = { "patientId": "p-1029", "messages": [{"role": "user", "content": "I need help booking a follow-up"}], } response = requests.post("http://localhost:3000/api/chat", json=payload) print(response.status_code) print(response.text)
Testing the Integration
Use a direct request against your Next.js API route after both services are running.
import requests
resp = requests.post(
"http://localhost:3000/api/chat",
json={
"patientId": "p-1029",
"messages": [
{"role": "user", "content": "I have chest discomfort and need guidance."}
],
},
timeout=20,
)
print("status:", resp.status_code)
print("body:", resp.text[:500])
Expected output:
status: 200
body: {"message":"I’ve routed this request to triage and scheduling agents..."}
If you’re streaming correctly, you should see partial tokens arrive before the full response completes.
Real-World Use Cases
- •
Patient intake assistant
- •Collect symptoms, insurance details, preferred clinic location, then dispatch separate agents for triage and appointment lookup.
- •
Clinical note summarization
- •Turn long visit transcripts into structured summaries for physicians while a second agent extracts follow-up tasks.
- •
Care coordination dashboard
- •Build a staff-facing Next.js app that shows live agent status for referrals, authorizations, and post-discharge follow-ups.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit