How to Integrate OpenAI for insurance with AWS Lambda for multi-agent systems
Combining OpenAI for insurance with AWS Lambda gives you a clean way to run specialized agent workflows without standing up a full service layer. In practice, that means you can route claims triage, policy Q&A, fraud screening, and document extraction through serverless functions while keeping each agent isolated and cheap to operate.
Prerequisites
- •Python 3.10+
- •AWS account with permission to create and invoke Lambda functions
- •AWS CLI configured locally:
- •
aws configure
- •
- •An OpenAI API key with access to the insurance-focused model or assistant setup you’re using
- •
boto3installed for AWS Lambda calls - •
openaiPython SDK installed - •IAM role for Lambda with:
- •
AWSLambdaBasicExecutionRole - •permission to invoke other Lambdas if you’re chaining agents
- •
- •Basic understanding of:
- •Lambda event payloads
- •JSON serialization
- •environment variables in AWS
Install dependencies:
pip install openai boto3
Integration Steps
1) Set up environment variables for both systems
Keep credentials out of code. For multi-agent systems, each Lambda should read config from environment variables so you can swap models or agent roles without redeploying logic.
import os
os.environ["OPENAI_API_KEY"] = "your-openai-key"
os.environ["INSURANCE_AGENT_MODEL"] = "gpt-4.1"
os.environ["SECONDARY_AGENT_LAMBDA"] = "policy-qna-agent"
In AWS Lambda, set the same values in the function configuration. For local testing, export them in your shell.
2) Build the OpenAI insurance agent call
This is the core reasoning step. The Lambda will send a claim or policy prompt to OpenAI and get structured output back.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
def run_insurance_agent(claim_text: str) -> str:
response = client.responses.create(
model=os.environ.get("INSURANCE_AGENT_MODEL", "gpt-4.1"),
input=[
{
"role": "system",
"content": (
"You are an insurance operations agent. "
"Classify the request as claim_intake, policy_question, fraud_review, or escalation. "
"Return concise JSON only."
),
},
{
"role": "user",
"content": claim_text,
},
],
)
return response.output_text
result = run_insurance_agent(
"Customer reports water damage in kitchen after pipe burst. Policy active since 2022."
)
print(result)
Use client.responses.create() here because it’s the current SDK entry point for model calls. In production, force JSON output in your prompt or via structured output patterns so downstream Lambdas can parse it reliably.
3) Invoke another Lambda as a downstream agent
Multi-agent systems work best when each function has one job. After classification, route to a specialist Lambda such as policy lookup or claims validation.
import json
import boto3
lambda_client = boto3.client("lambda")
def invoke_policy_agent(payload: dict) -> dict:
response = lambda_client.invoke(
FunctionName="policy-qna-agent",
InvocationType="RequestResponse",
Payload=json.dumps(payload).encode("utf-8"),
)
raw_body = response["Payload"].read().decode("utf-8")
return json.loads(raw_body)
payload = {
"customer_id": "CUST-10291",
"question": "Does my home policy cover burst pipes?",
}
policy_result = invoke_policy_agent(payload)
print(policy_result)
This uses the actual boto3.client("lambda").invoke() API. For synchronous orchestration, RequestResponse is usually what you want because the first agent can wait for the specialist result before deciding the next action.
4) Put both together inside a Lambda handler
Now wire the reasoning agent and downstream routing into one handler. This is the pattern you deploy in AWS.
import json
import os
import boto3
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
lambda_client = boto3.client("lambda")
def classify_request(text: str) -> str:
response = client.responses.create(
model=os.environ.get("INSURANCE_AGENT_MODEL", "gpt-4.1"),
input=[
{
"role": "system",
"content": (
"Classify insurance requests into one of: "
"claim_intake, policy_question, fraud_review, escalation."
),
},
{"role": "user", "content": text},
],
)
return response.output_text.strip()
def lambda_handler(event, context):
text = event.get("text", "")
classification = classify_request(text)
if classification == "policy_question":
downstream = lambda_client.invoke(
FunctionName=os.environ["SECONDARY_AGENT_LAMBDA"],
InvocationType="RequestResponse",
Payload=json.dumps({"text": text}).encode("utf-8"),
)
body = downstream["Payload"].read().decode("utf-8")
return {"classification": classification, "downstream_result": json.loads(body)}
return {"classification": classification}
Keep the handler small. The orchestration logic should stay obvious: classify first, then fan out only when needed. That keeps latency down and makes failures easier to isolate.
5) Add guardrails for production behavior
For insurance workflows, you need predictable outputs and safe failure handling. Add timeouts, retries, and explicit fallback paths.
import json
import os
import boto3
from botocore.exceptions import ClientError
lambda_client = boto3.client("lambda")
def safe_invoke(function_name: str, payload: dict) -> dict:
try:
response = lambda_client.invoke(
FunctionName=function_name,
InvocationType="RequestResponse",
Payload=json.dumps(payload).encode("utf-8"),
)
return json.loads(response["Payload"].read().decode("utf-8"))
except ClientError as e:
return {
"error": True,
"message": str(e),
"fallback_action": "route_to_human_adjuster",
}
This matters because multi-agent systems fail in messy ways: one agent returns malformed JSON, another times out, or a downstream policy service is unavailable. Your orchestration layer should degrade cleanly instead of breaking the whole workflow.
Testing the Integration
Use a local event payload first. If this runs inside AWS Lambda, deploy both functions and invoke the orchestrator function with a sample event.
if __name__ == "__main__":
test_event = {
"text": (
"I need help understanding whether my homeowners policy covers "
"water damage from a burst pipe in my kitchen."
)
}
result = lambda_handler(test_event, None)
print(json.dumps(result, indent=2))
Expected output:
{
"classification": "policy_question",
"downstream_result": {
"answer": "...",
"citations": ["policy_doc_12", "coverage_clause_4"]
}
}
If your classifier returns something else during testing, tighten the system prompt and make sure your downstream Lambda name matches exactly what’s in SECONDARY_AGENT_LAMBDA.
Real-World Use Cases
- •
Claims triage pipeline
- •One Lambda classifies incoming FNOL messages.
- •A second Lambda extracts entities like loss date, address, and incident type.
- •A third Lambda routes high-risk cases to a human adjuster.
- •
Policy servicing assistant
- •The OpenAI agent answers coverage questions.
- •AWS Lambda pulls policy data from internal APIs.
- •Another agent formats responses for email or chat delivery.
- •
Fraud review workflow
- •The first agent flags suspicious phrasing or inconsistent details.
- •A second Lambda enriches with claim history.
- •A review queue gets only the cases that need analyst attention.
If you’re building multi-agent systems for insurance, this pattern is usually enough to get from prototype to production shape: one reasoning layer with OpenAI, one execution layer with AWS Lambda, and explicit handoffs between 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