How to Integrate OpenAI for healthcare with AWS Lambda for multi-agent systems
Connecting OpenAI for healthcare with AWS Lambda gives you a clean way to run agentic workflows without keeping servers alive. In practice, this is useful for triaging patient messages, summarizing clinical notes, routing tasks across specialist agents, and triggering downstream systems like EHR queues or notification services.
The pattern is simple: OpenAI handles reasoning and structured extraction, while Lambda handles event-driven execution, orchestration, and integration with AWS services. That split works well when you need multiple agents to collaborate on healthcare tasks under tight operational controls.
Prerequisites
- •Python 3.10+
- •An AWS account with permission to create:
- •Lambda functions
- •IAM roles
- •CloudWatch logs
- •AWS CLI configured locally:
- •
aws configure
- •
- •An OpenAI API key with access to the healthcare-enabled model or endpoint you plan to use
- •
boto3installed locally - •
openaiPython SDK installed locally - •Basic familiarity with:
- •AWS Lambda handler functions
- •JSON event payloads
- •environment variables
Install dependencies:
pip install openai boto3
Set environment variables:
export OPENAI_API_KEY="your-openai-key"
export AWS_REGION="us-east-1"
Integration Steps
1) Define the agent contract
For multi-agent systems, start by defining a strict payload contract. One agent can classify the request, another can summarize it, and Lambda can route the result.
from typing import TypedDict, Literal
class ClinicalTask(TypedDict):
patient_id: str
task_type: Literal["triage", "summary", "routing"]
message: str
Keep the schema narrow. In healthcare workflows, loose payloads create bad handoffs between agents.
2) Call OpenAI from a Python agent function
Use the OpenAI SDK to turn unstructured clinical text into structured output. For healthcare use cases, keep prompts focused on administrative or clinical support tasks and avoid making unsupported medical decisions.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
def analyze_clinical_message(message: str) -> str:
response = client.responses.create(
model="gpt-4.1",
input=[
{
"role": "system",
"content": (
"You are a healthcare operations assistant. "
"Extract concise structured information from patient-facing text."
),
},
{
"role": "user",
"content": f"Summarize this message for clinical routing:\n\n{message}",
},
],
)
return response.output_text
If you are using an OpenAI healthcare-specific deployment or policy layer in your environment, keep the call pattern the same and swap in the approved model name or endpoint configuration.
3) Wrap the agent in an AWS Lambda handler
Lambda is the execution layer. It receives an event, calls OpenAI, then returns a response that another agent or workflow step can consume.
import json
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
def lambda_handler(event, context):
patient_id = event.get("patient_id", "unknown")
message = event["message"]
response = client.responses.create(
model="gpt-4.1",
input=[
{
"role": "system",
"content": (
"You are a healthcare triage assistant. "
"Return a short summary and a routing recommendation."
),
},
{
"role": "user",
"content": f"Patient {patient_id}: {message}",
},
],
)
return {
"statusCode": 200,
"body": json.dumps({
"patient_id": patient_id,
"summary": response.output_text,
}),
}
This is enough for a single-agent workflow. For multi-agent systems, this Lambda can be one node in a larger chain.
4) Trigger Lambda from another service or agent
A common pattern is to invoke Lambda from an upstream coordinator agent using boto3.client("lambda").invoke(). That lets one agent delegate work to another without direct coupling.
import json
import boto3
lambda_client = boto3.client("lambda", region_name="us-east-1")
payload = {
"patient_id": "P12345",
"message": "I have chest discomfort after walking upstairs and it started two hours ago."
}
response = lambda_client.invoke(
FunctionName="healthcare-triage-agent",
InvocationType="RequestResponse",
Payload=json.dumps(payload).encode("utf-8"),
)
result = json.loads(response["Payload"].read())
print(result)
Use this pattern when your coordinator agent needs fan-out behavior:
- •intake agent classifies the request
- •triage agent summarizes it
- •routing agent decides which queue gets it next
5) Chain multiple agents through Lambda events
For multi-agent systems, let each Lambda function do one job well. One function extracts entities, another creates a summary, and a third routes based on policy.
import json
from openai import OpenAI
client = OpenAI()
def extract_entities(text: str):
resp = client.responses.create(
model="gpt-4.1",
input=f"Extract key healthcare entities from this text as JSON:\n{text}"
)
return resp.output_text
def lambda_handler(event, context):
text = event["message"]
entities = extract_entities(text)
return {
"statusCode": 200,
"body": json.dumps({
"stage": "entity_extraction",
"entities": entities,
}),
}
In production, pass structured JSON between agents instead of raw prose where possible.
Testing the Integration
Use a local test payload first. If you deploy the Lambda function as healthcare-triage-agent, invoke it directly:
import json
import boto3
lambda_client = boto3.client("lambda", region_name="us-east-1")
test_event = {
"patient_id": "P77701",
"message": "Patient reports worsening shortness of breath during exercise and wants follow-up guidance."
}
response = lambda_client.invoke(
FunctionName="healthcare-triage-agent",
InvocationType="RequestResponse",
Payload=json.dumps(test_event).encode("utf-8"),
)
output = json.loads(response["Payload"].read())
print(json.dumps(output, indent=2))
Expected output:
{
"statusCode": 200,
"body": "{\"patient_id\": \"P77701\", \"summary\": \"...\"}"
}
If that works end-to-end, your AWS Lambda function is successfully invoking OpenAI and returning a usable result for other agents.
Real-World Use Cases
- •
Clinical intake triage
- •One agent classifies incoming patient messages.
- •Another summarizes symptoms.
- •A Lambda router sends urgent cases to an escalation queue.
- •
Prior authorization support
- •An extraction agent pulls diagnosis codes and supporting details from notes.
- •A second agent drafts payer-ready summaries.
- •Lambda pushes outputs into workflow systems or document stores.
- •
Care coordination automation
- •Agents summarize discharge notes.
- •Another agent identifies follow-up tasks.
- •Lambda triggers notifications to scheduling or case management 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