How to Integrate Anthropic for insurance with Cloudflare Workers for production AI
Why this integration matters
If you are building insurance AI agents, you need two things: a model that can reason over policy language and an edge runtime that can sit close to users, APIs, and internal systems. Anthropic gives you the reasoning layer; Cloudflare Workers gives you the low-latency execution layer for routing, auth, caching, and orchestration.
This combo is useful when you want to handle claims triage, policy Q&A, document classification, or broker support without pushing every request through a central app server. You keep the agent logic at the edge and call Anthropic only when you need model inference.
Prerequisites
- •Python 3.10+
- •An Anthropic API key
- •A Cloudflare account
- •A Cloudflare Workers project already created
- •
wranglerinstalled and authenticated - •
httpxinstalled for outbound calls from Python - •
anthropicPython SDK installed
Install the Python dependencies:
pip install anthropic httpx
Set your environment variables:
export ANTHROPIC_API_KEY="your_anthropic_key"
export CLOUDFLARE_ACCOUNT_ID="your_account_id"
export CLOUDFLARE_API_TOKEN="your_cloudflare_api_token"
export WORKER_URL="https://your-worker.your-subdomain.workers.dev"
Integration Steps
1) Call Anthropic from Python for insurance-specific reasoning
Start by wrapping your insurance prompt in a structured function. Keep the prompt narrow so the model returns something your Worker can consume deterministically.
import os
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
def classify_insurance_request(text: str) -> str:
response = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=300,
temperature=0,
messages=[
{
"role": "user",
"content": f"""
You are an insurance operations assistant.
Classify this request into one of:
- claims
- underwriting
- policy_servicing
- billing
- fraud_review
Return only the label.
Request:
{text}
"""
}
],
)
return response.content[0].text.strip()
print(classify_insurance_request("Customer wants to know if hail damage is covered under their auto policy."))
This gives you a stable classifier for routing requests inside your agent system.
2) Expose a Cloudflare Worker endpoint for orchestration
Your Worker should accept incoming traffic from your app, enforce auth, and forward the payload to your internal logic or directly to Anthropic. In production, I prefer keeping the Worker as the thin orchestration layer.
Example Worker in Python using Cloudflare Workers’ Python runtime:
import json
from js import Response, fetch
async def on_fetch(request):
data = await request.json()
# Forward to your backend or another service if needed
# Example: call an internal endpoint that runs Anthropic classification
resp = await fetch("https://api.example.com/insurance/classify", {
"method": "POST",
"headers": {"Content-Type": "application/json"},
"body": json.dumps(data),
})
result = await resp.text()
return Response.new(result, headers={"Content-Type": "application/json"})
If you are using standard Wrangler deployment with JS Workers instead of Python Workers, keep the same pattern: validate input at the edge, then call your model service or backend API.
3) Call your Worker from Python and pass insurance payloads
Now connect your application layer to the Worker. This is where your agent system starts to look like production code instead of a notebook demo.
import os
import httpx
WORKER_URL = os.environ["WORKER_URL"]
def route_insurance_case(case_payload: dict) -> dict:
with httpx.Client(timeout=30) as client:
response = client.post(
f"{WORKER_URL}/route",
json=case_payload,
headers={
"Content-Type": "application/json",
"X-API-Key": os.environ["CLOUDFLARE_API_TOKEN"],
},
)
response.raise_for_status()
return response.json()
payload = {
"customer_id": "CUST-10291",
"text": "Is water damage from a burst pipe covered under my homeowners policy?",
}
print(route_insurance_case(payload))
In practice, this Worker can normalize input, apply rate limits, inspect headers, and decide whether to invoke Anthropic directly or send work downstream.
4) Combine both sides in one production flow
The cleanest pattern is: Python service receives request, calls Cloudflare Worker for edge validation/routing, then calls Anthropic for reasoning. That keeps operational concerns separate from model concerns.
import os
import httpx
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
WORKER_URL = os.environ["WORKER_URL"]
def handle_claim_question(question: str) -> dict:
with httpx.Client(timeout=20) as http_client:
route_resp = http_client.post(
f"{WORKER_URL}/validate",
json={"text": question},
headers={"X-API-Key": os.environ["CLOUDFLARE_API_TOKEN"]},
)
route_resp.raise_for_status()
route_data = route_resp.json()
if not route_data["allowed"]:
return {"status": "blocked", "reason": route_data["reason"]}
anthropic_resp = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=400,
temperature=0,
messages=[
{
"role": "user",
"content": f"""
Answer as an insurance assistant.
Use concise language.
Question: {question}
"""
}
],
)
return {
"status": "ok",
"classification": route_data.get("classification"),
"answer": anthropic_resp.content[0].text.strip(),
}
This is the pattern I use for regulated workflows: edge validation first, model inference second.
5) Add observability and deploy safely
For production AI in insurance, log request IDs, classification labels, latency, and refusal reasons. Do not log raw PII unless you have a clear retention policy.
Use Wrangler to deploy the Worker:
wrangler deploy
Then test it with real traffic patterns:
- •high-frequency quote questions
- •claim status checks
- •document intake requests
Keep retries outside the model path when possible. If Anthropic times out, return a controlled fallback from your Worker rather than failing open.
Testing the Integration
Use a simple end-to-end test that hits your Worker and verifies that it returns structured output from the model path.
import os
import httpx
def test_worker_integration():
payload = {
"text": "My roof was damaged during a storm. Is this likely covered?"
}
with httpx.Client(timeout=30) as client:
response = client.post(
f"{os.environ['WORKER_URL']}/route",
json=payload,
headers={"X-API-Key": os.environ["CLOUDFLARE_API_TOKEN"]},
)
assert response.status_code == 200
data = response.json()
assert "classification" in data or "answer" in data
if __name__ == "__main__":
test_worker_integration()
print("Integration test passed")
Expected output:
Integration test passed
If everything is wired correctly, you should also see a JSON response similar to:
{
"classification": "claims",
"answer": "Storm-related roof damage may be covered depending on peril language and exclusions..."
}
Real-World Use Cases
- •
Claims intake triage
- •Use Cloudflare Workers to authenticate requests and throttle abuse.
- •Use Anthropic to classify claim type, detect missing information, and draft next-step questions.
- •
Policy servicing assistant
- •Let Workers sit in front of customer portals and broker tools.
- •Use Anthropic to answer coverage questions from policy text and summarize endorsements.
- •
Fraud review pre-screening
- •Route suspicious submissions through Workers for enrichment and logging.
- •Use Anthropic to flag inconsistent narratives before sending cases to human reviewers.
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