How to Integrate OpenAI for healthcare with AWS Lambda for AI agents
Connecting OpenAI for healthcare with AWS Lambda gives you a clean way to build regulated AI agents that can triage requests, summarize clinical notes, and route sensitive workflows without standing up a full server. Lambda handles the event-driven execution layer, while OpenAI for healthcare handles the language and reasoning side inside an agent loop.
The pattern is useful when you need low-latency automation around patient intake, prior auth support, claims review, or clinical document summarization. You keep the agent stateless, push orchestration into Lambda, and call OpenAI only when you need structured reasoning or natural language generation.
Prerequisites
- •An AWS account with permission to create:
- •Lambda functions
- •IAM roles
- •CloudWatch logs
- •AWS CLI configured locally:
- •
aws configure
- •
- •Python 3.11 or later
- •
boto3installed for AWS SDK access - •
openaiPython SDK installed - •An OpenAI API key stored as an environment variable:
- •
OPENAI_API_KEY
- •
- •A healthcare-safe data flow already defined:
- •PHI handling rules
- •logging redaction strategy
- •approved model usage policy
- •Basic familiarity with:
- •AWS Lambda handler functions
- •JSON event payloads
Install the Python dependencies:
pip install openai boto3
Integration Steps
1) Create a Lambda handler that receives agent events
Your Lambda should accept a structured event from your agent orchestrator. Keep the payload narrow: patient context, task type, and sanitized text.
import json
def lambda_handler(event, context):
task = event.get("task", "summarize")
input_text = event.get("input_text", "")
return {
"statusCode": 200,
"body": json.dumps({
"task": task,
"input_text": input_text[:500]
})
}
This is the base contract between your AI agent system and AWS Lambda. In production, validate the schema before calling any model.
2) Call OpenAI from inside Lambda using the Responses API
Use the OpenAI Python SDK directly in Lambda. For healthcare workflows, keep prompts narrow and ask for structured output so downstream systems can parse it safely.
import os
import json
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
def lambda_handler(event, context):
patient_note = event["input_text"]
response = client.responses.create(
model="gpt-4.1",
input=[
{
"role": "system",
"content": (
"You are a healthcare assistant. "
"Summarize only the provided text. "
"Do not invent diagnoses or treatment."
)
},
{
"role": "user",
"content": f"Summarize this clinical note:\n\n{patient_note}"
}
]
)
return {
"statusCode": 200,
"body": json.dumps({
"summary": response.output_text
})
}
That client.responses.create(...) call is the core integration point. It gives you a clean request/response shape for agent tasks like summarization, classification, or extraction.
3) Add an AWS Lambda invocation layer for your agent orchestrator
If your AI agent runs elsewhere — Step Functions, ECS, another Lambda, or an API backend — invoke the function with boto3.client("lambda"). This keeps orchestration outside the model runtime.
import json
import boto3
lambda_client = boto3.client("lambda")
payload = {
"task": "summarize",
"input_text": (
"Patient reports shortness of breath after exertion. "
"No chest pain. Vitals stable."
)
}
response = lambda_client.invoke(
FunctionName="healthcare-agent-handler",
InvocationType="RequestResponse",
Payload=json.dumps(payload).encode("utf-8")
)
result = json.loads(response["Payload"].read())
print(result)
This is the pattern you want for agent systems: one service decides when to call the model, and Lambda decides how to execute it. The result is easier to secure and easier to audit.
4) Return structured output for downstream healthcare workflows
For real systems, don’t return free-form prose only. Ask OpenAI for JSON so your workflow engine can branch on fields like urgency, specialty, or next action.
import os
import json
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
def lambda_handler(event, context):
note = event["input_text"]
response = client.responses.create(
model="gpt-4.1",
input=[
{
"role": "system",
"content": (
"Extract healthcare triage data as JSON with keys: "
"urgency_level, specialty, summary, recommended_next_step."
)
},
{"role": "user", "content": note}
]
)
return {
"statusCode": 200,
"body": response.output_text
}
In production, validate that output before writing it to DynamoDB or sending it into a workflow engine.
5) Secure secrets and reduce PHI exposure
Store secrets in environment variables or AWS Secrets Manager. Also strip identifiers before sending text to OpenAI unless your compliance program explicitly allows that data flow.
import os
import re
def redact_phi(text: str) -> str:
text = re.sub(r"\b\d{3}-\d{2}-\d{4}\b", "[REDACTED_SSN]", text)
text = re.sub(r"\b\d{10}\b", "[REDACTED_PHONE]", text)
return text
def lambda_handler(event, context):
safe_text = redact_phi(event["input_text"])
api_key = os.environ["OPENAI_API_KEY"]
# use api_key with OpenAI client here
return {"statusCode": 200, "body": safe_text}
This is not optional in healthcare. Your integration should assume every log line and prompt needs review.
Testing the Integration
Use a local test event first. If you’re deploying via AWS SAM or plain Lambda console tests, this payload is enough to verify end-to-end behavior.
test_event = {
"task": "summarize",
"input_text": (
"45-year-old patient presents with persistent cough for two weeks. "
"No fever. Chest X-ray pending."
)
}
print(lambda_handler(test_event, None))
Expected output:
{
"statusCode": 200,
"body": "{\"summary\":\"A middle-aged patient has had a persistent cough for two weeks without fever; chest imaging is still pending.\"}"
}
If you also test AWS invocation separately:
import json
import boto3
lambda_client = boto3.client("lambda")
resp = lambda_client.invoke(
FunctionName="healthcare-agent-handler",
InvocationType="RequestResponse",
Payload=json.dumps(test_event).encode("utf-8")
)
print(json.loads(resp["Payload"].read()))
You should see a 200 response and a body containing either a summary or structured JSON depending on your prompt.
Real-World Use Cases
- •
Clinical intake triage
- •A patient portal submits symptoms.
- •Lambda sanitizes input.
- •OpenAI classifies urgency and suggests routing to nurse line vs scheduling.
- •
Prior authorization support
- •An agent extracts diagnosis codes and supporting evidence from notes.
- •Lambda packages the request.
- •OpenAI drafts a concise payer-facing summary.
- •
Claims and document review
- •A claims workflow sends scanned note text into Lambda.
- •OpenAI identifies missing fields or inconsistencies.
- •Downstream systems flag records for human review.
The pattern stays simple: AWS Lambda handles execution and integration glue, while OpenAI handles language tasks inside tightly controlled healthcare workflows. If you keep prompts narrow, outputs structured, and PHI exposure minimal, this combo scales well in production AI agents.
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