How to Integrate Anthropic for insurance with Cloudflare Workers for RAG
Why this integration matters
If you’re building insurance agents, the hard part isn’t just generating text. It’s grounding claims, policy, and underwriting answers in approved internal documents without dragging your app through a slow backend hop.
Anthropic gives you strong reasoning and structured output for insurance workflows, while Cloudflare Workers gives you an edge runtime for retrieval, routing, and low-latency document access. Put them together and you get a RAG pipeline that can answer policy questions, summarize claim files, and draft customer responses with less latency and tighter control.
Prerequisites
- •Python 3.10+
- •An Anthropic API key
- •A Cloudflare account
- •A Cloudflare Worker deployed with a retrieval endpoint
- •
pipinstalled - •These Python packages:
- •
anthropic - •
requests - •
python-dotenv
- •
- •A vector store or document index behind your Worker
- •Insurance documents already chunked and indexed
- •Environment variables set locally:
- •
ANTHROPIC_API_KEY - •
CLOUDFLARE_WORKER_URL
- •
Integration Steps
- •Install dependencies and load secrets
Start with a clean Python environment and keep keys out of source control.
import os
from dotenv import load_dotenv
load_dotenv()
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
CLOUDFLARE_WORKER_URL = os.getenv("CLOUDFLARE_WORKER_URL")
if not ANTHROPIC_API_KEY:
raise ValueError("Missing ANTHROPIC_API_KEY")
if not CLOUDFLARE_WORKER_URL:
raise ValueError("Missing CLOUDFLARE_WORKER_URL")
Install the packages:
pip install anthropic requests python-dotenv
- •Call your Cloudflare Worker to retrieve insurance context
Your Worker should expose a simple HTTP endpoint that accepts a query and returns top matching chunks from your indexed policy corpus.
import requests
def retrieve_context(query: str) -> list[dict]:
payload = {
"query": query,
"top_k": 4,
"namespace": "insurance-policies"
}
response = requests.post(
f"{CLOUDFLARE_WORKER_URL}/retrieve",
json=payload,
timeout=15,
)
response.raise_for_status()
data = response.json()
return data["results"]
A typical Worker response should look like this:
{
"results": [
{
"id": "policy_123_chunk_7",
"text": "Accidental damage is covered only when ...",
"source": "policy-handbook.pdf",
"score": 0.91
}
]
}
- •Build the RAG prompt for Anthropic
For insurance use cases, keep the model constrained. Feed it retrieved context, then force it to answer only from that context unless it explicitly says the answer is missing.
from anthropic import Anthropic
client = Anthropic(api_key=ANTHROPIC_API_KEY)
def build_prompt(user_question: str, contexts: list[dict]) -> str:
context_block = "\n\n".join(
f"[Source: {item['source']} | Score: {item['score']}]\n{item['text']}"
for item in contexts
)
return f"""
You are an insurance assistant.
Answer only using the provided context.
If the answer is not in the context, say: "I don't have enough information in the policy documents."
Question:
{user_question}
Context:
{context_block}
""".strip()
- •Send the grounded prompt to Anthropic
Use the Messages API. For production insurance flows, keep temperature low and request concise output.
def ask_anthropic(question: str) -> str:
contexts = retrieve_context(question)
prompt = build_prompt(question, contexts)
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=400,
temperature=0,
system="You are a compliant insurance assistant.",
messages=[
{
"role": "user",
"content": prompt
}
],
)
return message.content[0].text
- •Wrap retrieval + generation into one agent function
This is the piece you’ll call from your API route, queue consumer, or claims workflow.
def answer_insurance_question(question: str) -> dict:
contexts = retrieve_context(question)
response_text = ask_anthropic(question)
return {
"question": question,
"answer": response_text,
"retrieved_sources": [
{
"source": item["source"],
"score": item["score"],
"id": item["id"]
}
for item in contexts
]
}
Testing the Integration
Run a direct smoke test with a policy question that should exist in your corpus.
if __name__ == "__main__":
question = "Does this policy cover water damage from a burst pipe?"
result = answer_insurance_question(question)
print("QUESTION:", result["question"])
print("\nANSWER:", result["answer"])
print("\nSOURCES:")
for source in result["retrieved_sources"]:
print(source)
Expected output should look like this:
QUESTION: Does this policy cover water damage from a burst pipe?
ANSWER: Based on the policy documents, sudden water damage from a burst pipe is covered subject to exclusions related to negligence and maintenance issues.
SOURCES:
{'source': 'homeowners_policy.pdf', 'score': 0.93, 'id': 'policy_44_chunk_2'}
{'source': 'claims_guidelines.pdf', 'score': 0.88, 'id': 'policy_12_chunk_9'}
If you get “I don't have enough information in the policy documents.”, that usually means one of three things:
- •Your Worker retrieval is returning weak chunks
- •Your chunking strategy is too coarse
- •The question needs better metadata filters like product line or jurisdiction
Real-World Use Cases
- •
Claims triage assistant
- •Retrieve claim notes, coverage clauses, and adjuster guidelines.
- •Have Anthropic draft a next-step recommendation or customer-facing summary.
- •
Policy Q&A agent
- •Answer questions about deductibles, exclusions, waiting periods, and endorsements.
- •Ground every response in approved policy text returned by Cloudflare Workers.
- •
Underwriting copilot
- •Pull relevant submission docs, prior loss history, and underwriting rules.
- •Generate a concise risk summary for human review.
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