How to Integrate Anthropic for healthcare with Cloudflare Workers for AI agents
Why this integration matters
If you’re building AI agents for healthcare, the hard part is not just generating a response. It’s controlling where data flows, enforcing policy at the edge, and keeping latency low enough for real workflows like triage, prior auth, and patient support.
Anthropic gives you the model layer for clinical reasoning and structured outputs. Cloudflare Workers gives you a global execution layer to validate requests, route traffic, redact sensitive fields, and orchestrate agent calls close to the user.
Prerequisites
- •Python 3.10+
- •An Anthropic API key with access to the healthcare-capable model or your approved Anthropic deployment
- •A Cloudflare account with Workers enabled
- •
wranglerinstalled and authenticated - •A Cloudflare Worker route already created
- •Environment variables set:
- •
ANTHROPIC_API_KEY - •
CLOUDFLARE_ACCOUNT_ID - •
CLOUDFLARE_API_TOKEN
- •
- •Python packages:
- •
anthropic - •
requests - •
python-dotenv
- •
Install the dependencies:
pip install anthropic requests python-dotenv
Integration Steps
1) Set up your Anthropic client in Python
Start by isolating model access behind a small wrapper. In healthcare workflows, you want one place to enforce prompt structure, logging, and redaction before anything leaves your system.
import os
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
def ask_clinical_agent(question: str) -> str:
response = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=400,
temperature=0,
messages=[
{
"role": "user",
"content": f"""
You are a healthcare support agent.
Answer only using the provided context and avoid diagnosis unless explicitly asked.
Question: {question}
"""
}
],
)
return response.content[0].text
This is the core model call. In production, keep the prompt narrow and deterministic so your Worker can enforce policy before forwarding requests.
2) Create a Cloudflare Worker as the control plane
Use Workers to receive requests from your app or agent gateway. The Worker can validate headers, strip PHI you do not want to expose, and forward only approved payloads to your Python service or directly to downstream APIs.
A minimal Worker using the Fetch API looks like this:
export default {
async fetch(request, env, ctx) {
const body = await request.json();
if (!body.question || typeof body.question !== "string") {
return new Response(JSON.stringify({ error: "Missing question" }), {
status: 400,
headers: { "content-type": "application/json" },
});
}
const upstream = await fetch("https://your-python-service.example.com/agent", {
method: "POST",
headers: {
"content-type": "application/json",
"x-worker-auth": env.WORKER_AUTH_TOKEN,
},
body: JSON.stringify({
question: body.question,
patient_context: body.patient_context ?? {},
}),
});
return new Response(upstream.body, {
status: upstream.status,
headers: { "content-type": "application/json" },
});
},
};
This Worker is the policy gate. Keep it thin and deterministic; do not bury business logic here.
3) Expose a Python endpoint that Anthropic can be called through
If your Worker forwards traffic to Python, use FastAPI or Flask on the backend. This keeps sensitive orchestration in a controlled service while Workers handle edge routing.
from fastapi import FastAPI, Header, HTTPException
from pydantic import BaseModel
import os
app = FastAPI()
class AgentRequest(BaseModel):
question: str
patient_context: dict = {}
@app.post("/agent")
def agent(req: AgentRequest, x_worker_auth: str | None = Header(default=None)):
if x_worker_auth != os.environ["WORKER_AUTH_TOKEN"]:
raise HTTPException(status_code=401, detail="Unauthorized")
prompt = f"""
Clinical support request:
Question: {req.question}
Patient context:
{req.patient_context}
"""
answer = ask_clinical_agent(prompt)
return {"answer": answer}
The important part here is that the Worker authenticates into your backend using a shared token or mTLS. That keeps public traffic out of your model path.
4) Call Cloudflare Workers from your Python agent system
If your AI agent runtime lives in Python, treat the Worker as an internal API. This pattern is useful when multiple tools need routing decisions before any model call happens.
import os
import requests
WORKER_URL = os.environ["CLOUDFLARE_WORKER_URL"]
def route_through_worker(question: str, patient_context: dict):
resp = requests.post(
WORKER_URL,
json={
"question": question,
"patient_context": patient_context,
},
timeout=20,
)
resp.raise_for_status()
return resp.json()
result = route_through_worker(
"What are common side effects of metformin?",
{"age": 54, "conditions": ["type_2_diabetes"]},
)
print(result["answer"])
This gives you one entry point for agent traffic. You can later add caching, rate limits, geo-based routing, or audit headers without changing every caller.
5) Add structured output for downstream healthcare workflows
For healthcare systems, free-form text is usually not enough. You want machine-readable output so your agent can hand off to triage systems, CRM tools, or human review queues.
from anthropic import Anthropic
import os
import json
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
def structured_triage(question: str):
msg = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=300,
temperature=0,
messages=[{"role": "user", "content": question}],
system="""
Return valid JSON with keys:
- urgency_level
- summary
- recommended_next_step
Do not include markdown.
""",
)
return json.loads(msg.content[0].text)
Use this when the Worker has already validated input and you need strict output for workflow automation.
Testing the Integration
Run a local test against your Python service or deployed Worker endpoint.
import os
import requests
payload = {
"question": "Explain whether mild nausea after starting metformin is common.",
"patient_context": {"age": 61, "medications": ["metformin"]},
}
resp = requests.post(os.environ["CLOUDFLARE_WORKER_URL"], json=payload)
resp.raise_for_status()
data = resp.json()
print(data["answer"])
Expected output:
Mild nausea can occur when starting metformin and often improves after a few days...
If you get a non-200 response:
- •Check that the Worker route is active
- •Verify
WORKER_AUTH_TOKENmatches on both sides - •Confirm
ANTHROPIC_API_KEYis present in the backend environment - •Inspect Worker logs with
wrangler tail
Real-World Use Cases
- •
Patient intake agents
Use Workers to validate form submissions and Anthropic to summarize symptoms into structured intake notes for nurses. - •
Prior authorization assistants
Route insurance questions through Workers for policy checks, then use Anthropic to draft payer-ready summaries from clinical notes. - •
Care navigation bots
Combine edge routing with model reasoning to answer benefits questions, recommend next steps, and escalate high-risk cases to humans.
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