How to Integrate CrewAI for healthcare with FastAPI for multi-agent systems
Combining CrewAI for healthcare with FastAPI gives you a clean way to expose multi-agent workflows as production HTTP APIs. That matters when you need triage, summarization, care-plan drafting, or prior-auth support behind a stable backend that your frontend, EMR integration layer, or internal tools can call.
The pattern is simple: CrewAI handles the agent orchestration, FastAPI handles request validation, auth boundaries, and async-friendly API delivery.
Prerequisites
- •Python 3.10+
- •
crewaiinstalled - •
fastapiinstalled - •
uvicorninstalled - •Access to the LLM provider your CrewAI agents will use
- •Basic familiarity with:
- •Pydantic models
- •REST APIs
- •Python async functions
- •A healthcare-safe design for PHI handling:
- •redact sensitive fields where needed
- •log minimally
- •keep audit trails outside prompt text
Install the packages:
pip install fastapi uvicorn crewai pydantic
Integration Steps
- •Set up your CrewAI agents and tasks
Start by defining the agents that will collaborate on a healthcare workflow. For example, one agent can do clinical summarization while another checks for missing data or red flags.
from crewai import Agent, Task, Crew, Process
clinical_summarizer = Agent(
role="Clinical Summarizer",
goal="Summarize patient intake notes into structured clinical context",
backstory="You convert raw patient notes into concise clinical summaries for downstream review.",
verbose=True,
)
risk_checker = Agent(
role="Risk Checker",
goal="Identify missing information and potential escalation flags",
backstory="You review patient information for safety gaps and urgent concerns.",
verbose=True,
)
summarize_task = Task(
description=(
"Summarize the following patient intake note into problem list, "
"medications, allergies, and next-step recommendations."
),
expected_output="A structured clinical summary with clear sections.",
agent=clinical_summarizer,
)
risk_task = Task(
description=(
"Review the same note and identify missing data, urgent symptoms, "
"or escalation concerns."
),
expected_output="A short risk assessment with action items.",
agent=risk_checker,
)
crew = Crew(
agents=[clinical_summarizer, risk_checker],
tasks=[summarize_task, risk_task],
process=Process.sequential,
)
- •Build a FastAPI app with request/response models
FastAPI should validate incoming payloads before they hit your agents. Keep the API contract explicit so downstream systems know exactly what to send.
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI(title="Healthcare Multi-Agent API")
class PatientNoteRequest(BaseModel):
patient_id: str
note: str
class PatientNoteResponse(BaseModel):
patient_id: str
summary: str
- •Create an endpoint that runs the CrewAI workflow
This is where the integration happens. The endpoint receives a payload, passes it to CrewAI through kickoff(), then returns the result.
from fastapi import HTTPException
@app.post("/analyze", response_model=PatientNoteResponse)
def analyze_patient_note(payload: PatientNoteRequest):
if not payload.note.strip():
raise HTTPException(status_code=400, detail="note cannot be empty")
crew_input = {
"patient_note": payload.note,
"patient_id": payload.patient_id,
}
result = crew.kickoff(inputs=crew_input)
return PatientNoteResponse(
patient_id=payload.patient_id,
summary=str(result),
)
A practical point: keep your prompt inputs scoped. Do not dump full EHR records into a single task unless you have already filtered and minimized the content.
- •Add async-safe execution for higher throughput
CrewAI execution is often CPU/network-bound depending on the model provider. In FastAPI, wrap blocking calls in a threadpool so your server does not stall under concurrent traffic.
from fastapi.concurrency import run_in_threadpool
@app.post("/analyze-async", response_model=PatientNoteResponse)
async def analyze_patient_note_async(payload: PatientNoteRequest):
if not payload.note.strip():
raise HTTPException(status_code=400, detail="note cannot be empty")
crew_input = {
"patient_note": payload.note,
"patient_id": payload.patient_id,
}
result = await run_in_threadpool(crew.kickoff, inputs=crew_input)
return PatientNoteResponse(
patient_id=payload.patient_id,
summary=str(result),
)
Use this pattern when you expect multiple requests per second or when your model calls have variable latency.
- •Run the service and wire in basic operational controls
Expose health checks and start the app with Uvicorn. In production, pair this with structured logging and request IDs so you can trace agent runs.
@app.get("/health")
def health():
return {"status": "ok"}
# Run with:
# uvicorn main:app --reload --host 0.0.0.0 --port 8000
If you need stronger controls later, add:
- •auth middleware
- •rate limiting
- •PHI redaction before
kickoff() - •timeout handling around agent execution
Testing the Integration
Use FastAPI’s test client to verify that requests flow through to CrewAI correctly.
from fastapi.testclient import TestClient
client = TestClient(app)
response = client.post(
"/analyze",
json={
"patient_id": "P12345",
"note": "Patient reports chest tightness after exertion. Allergic to penicillin.",
},
)
print(response.status_code)
print(response.json())
Expected output:
200
{
"patient_id": "P12345",
"summary": "...CrewAI-generated clinical summary..."
}
If you get a non-200 response, check:
- •your model provider credentials
- •task definitions referencing missing input keys
- •empty or malformed request bodies
Real-World Use Cases
- •
Clinical intake triage
- •One agent summarizes symptoms.
- •Another flags urgent escalation criteria.
- •FastAPI exposes it as
/triagefor nurse ops or call-center tools.
- •
Prior authorization support
- •One agent extracts medical necessity evidence.
- •Another checks whether documentation is incomplete.
- •The API returns a draft packet for human review.
- •
Care coordination assistant
- •One agent builds a visit summary.
- •Another identifies follow-up gaps like labs or referrals.
- •Your frontend calls FastAPI and shows results inside clinician workflow tools.
The main win here is separation of concerns. CrewAI handles multi-agent reasoning; FastAPI gives you a stable service boundary that fits real healthcare systems.
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