How to Integrate CrewAI for healthcare with FastAPI for production AI
Combining CrewAI for healthcare with FastAPI gives you a clean way to expose multi-agent clinical workflows as production HTTP services. That means you can take a patient intake flow, triage workflow, or prior-auth assistant and wrap it behind an API that your EHR, portal, or internal ops tools can call.
The pattern is simple: CrewAI handles task orchestration across specialized agents, while FastAPI handles request validation, routing, auth hooks, and deployment-friendly async endpoints.
Prerequisites
- •Python 3.10+
- •A working FastAPI project
- •CrewAI installed in your environment
- •Healthcare-specific agent tools configured for your use case
- •An LLM provider key set in environment variables
- •
uvicornfor local API runs - •Basic familiarity with Pydantic models and REST APIs
Install the core packages:
pip install fastapi uvicorn crewai pydantic python-dotenv
If you are using a healthcare-focused CrewAI package or toolkit, install that as well and wire its credentials through environment variables.
Integration Steps
- •Define the request and response schema
Start by making the API contract explicit. In production, this is where you stop free-form inputs from leaking into your agent layer.
from pydantic import BaseModel, Field
from typing import Optional
class ClinicalTriageRequest(BaseModel):
patient_id: str = Field(..., examples=["P12345"])
chief_complaint: str = Field(..., examples=["Chest pain and shortness of breath"])
age: int = Field(..., ge=0, le=120)
notes: Optional[str] = None
class ClinicalTriageResponse(BaseModel):
summary: str
urgency_level: str
recommended_next_step: str
- •Build your CrewAI agents and task flow
Use CrewAI’s Agent, Task, and Crew primitives to define the workflow. In a healthcare setup, keep one agent focused on summarization and another on triage reasoning.
from crewai import Agent, Task, Crew, Process
clinical_summarizer = Agent(
role="Clinical Summarizer",
goal="Summarize patient intake into a concise clinical note",
backstory="You convert raw patient intake into structured clinical summaries.",
verbose=True,
)
triage_agent = Agent(
role="Clinical Triage Assistant",
goal="Assess urgency based on symptoms and intake data",
backstory="You help route patients to the right next step using conservative clinical reasoning.",
verbose=True,
)
summarize_task = Task(
description=(
"Summarize this patient intake:\n"
"Patient ID: {patient_id}\n"
"Chief complaint: {chief_complaint}\n"
"Age: {age}\n"
"Notes: {notes}"
),
expected_output="A concise clinical summary.",
agent=clinical_summarizer,
)
triage_task = Task(
description=(
"Using the intake summary and complaint details, determine urgency level "
"and recommend the next step."
),
expected_output="Urgency level plus recommended next step.",
agent=triage_agent,
)
crew = Crew(
agents=[clinical_summarizer, triage_agent],
tasks=[summarize_task, triage_task],
process=Process.sequential,
)
- •Expose the crew through a FastAPI endpoint
This is the production boundary. Keep the API handler thin: validate input, call crew.kickoff(), return structured output.
from fastapi import FastAPI, HTTPException
app = FastAPI(title="Healthcare AI Orchestrator")
@app.post("/triage", response_model=ClinicalTriageResponse)
async def triage_patient(payload: ClinicalTriageRequest):
try:
result = crew.kickoff(inputs=payload.model_dump())
return ClinicalTriageResponse(
summary=str(result),
urgency_level="needs_review",
recommended_next_step="Route to clinician queue",
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Triage workflow failed: {e}")
- •Run the app with Uvicorn
Use Uvicorn in development first. In production, put this behind a proper process manager or container platform.
# main.py
from fastapi import FastAPI
# app defined above
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
Then start it:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
- •Add basic production guardrails
For healthcare workloads, don’t ship raw prompts directly from the client into the crew. Add auth, logging redaction, and input limits before calling kickoff().
from fastapi import Depends
def verify_api_key():
# Replace with real auth logic
return True
@app.post("/triage-secure", response_model=ClinicalTriageResponse)
async def triage_patient_secure(
payload: ClinicalTriageRequest,
_: bool = Depends(verify_api_key),
):
result = crew.kickoff(inputs=payload.model_dump())
return ClinicalTriageResponse(
summary=str(result),
urgency_level="needs_review",
recommended_next_step="Route to clinician queue",
)
Testing the Integration
Hit the endpoint with a sample request using curl or Python requests. This verifies both FastAPI routing and CrewAI execution.
import requests
payload = {
"patient_id": "P12345",
"chief_complaint": "Chest pain and shortness of breath",
"age": 58,
"notes": "Started after walking upstairs"
}
response = requests.post("http://localhost:8000/triage", json=payload)
print(response.status_code)
print(response.json())
Expected output:
{
"summary": "Clinical summary generated by CrewAI workflow...",
"urgency_level": "needs_review",
"recommended_next_step": "Route to clinician queue"
}
If you get a 500 error, check these first:
- •Your LLM credentials are set correctly
- •The crew is receiving serializable input via
model_dump() - •Your agents have valid instructions and tool access if you attached any tools
Real-World Use Cases
- •
Patient intake automation
Collect symptoms from a portal form, summarize them with CrewAI agents, then expose the result through FastAPI for downstream EHR ingestion. - •
Prior authorization support
Build an agent chain that reviews clinical notes and payer rules, then serve it as an internal API for utilization management teams. - •
Care navigation assistant
Route patients to self-care instructions, nurse callbacks, or urgent escalation by combining multi-agent reasoning with a low-latency API layer.
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