How to Integrate Anthropic for retail banking with Cloudflare Workers for RAG
Retail banking teams need answers grounded in policy, product docs, and customer context. Anthropic gives you the reasoning layer for safe, policy-aware responses, while Cloudflare Workers gives you the edge runtime to fetch, filter, and route retrieval data close to the user.
For RAG, this combo is useful when you need low-latency access to internal banking knowledge without shipping raw documents into your model prompt. The Worker becomes the retrieval gateway, and Anthropic turns retrieved context into a controlled answer.
Prerequisites
- •Python 3.10+
- •An Anthropic API key
- •A Cloudflare account with:
- •Workers enabled
- •A deployed Worker endpoint for retrieval
- •Optional: Workers KV or D1 if you store embeddings/metadata at the edge
- •
pipinstalled - •A banking knowledge base already chunked and indexed somewhere accessible by your Worker
- •Environment variables set:
- •
ANTHROPIC_API_KEY - •
CLOUDFLARE_WORKER_URL
- •
Install the Python dependencies:
pip install anthropic requests python-dotenv
Integration Steps
- •Set up your environment variables and client objects.
import os
from dotenv import load_dotenv
from anthropic import Anthropic
load_dotenv()
ANTHROPIC_API_KEY = os.environ["ANTHROPIC_API_KEY"]
CLOUDFLARE_WORKER_URL = os.environ["CLOUDFLARE_WORKER_URL"]
client = Anthropic(api_key=ANTHROPIC_API_KEY)
- •Call your Cloudflare Worker to retrieve banking context.
Your Worker should accept a query and return top-k chunks from your RAG index. In production, keep the Worker response small and structured.
import requests
def retrieve_context(query: str) -> dict:
payload = {
"query": query,
"top_k": 4,
"filters": {
"domain": "retail_banking",
"jurisdiction": "US"
}
}
response = requests.post(
CLOUDFLARE_WORKER_URL,
json=payload,
timeout=10,
)
response.raise_for_status()
return response.json()
result = retrieve_context("What is the overdraft fee waiver policy?")
print(result)
A typical response from the Worker should look like this:
{
"chunks": [
{
"doc_id": "policy-102",
"title": "Checking Account Fee Schedule",
"text": "Customers with direct deposit of $500 or more per statement cycle are eligible for one overdraft fee waiver per month."
},
{
"doc_id": "faq-044",
"title": "Overdraft Assistance FAQ",
"text": "Fee waivers are not guaranteed and may be revoked if abuse is detected."
}
]
}
- •Build a prompt that includes retrieved context and banking guardrails.
Do not dump raw documents into the model. Pass only the minimal evidence needed for the answer.
def build_messages(user_question: str, chunks: list[dict]) -> list[dict]:
context_block = "\n\n".join(
f"[{c['doc_id']}] {c['title']}\n{c['text']}"
for c in chunks
)
system_prompt = (
"You are a retail banking assistant. "
"Answer only using the provided context. "
"If the context does not contain the answer, say you don't have enough information. "
"Do not provide legal advice."
)
user_prompt = f"""
Question:
{user_question}
Context:
{context_block}
""".strip()
return [
{"role": "user", "content": user_prompt}
], system_prompt
- •Send the RAG prompt to Anthropic using the Messages API.
This is the core integration point. Use client.messages.create() with a constrained model and explicit instructions.
def answer_with_anthropic(question: str) -> str:
retrieval = retrieve_context(question)
chunks = retrieval.get("chunks", [])
messages, system_prompt = build_messages(question, chunks)
response = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=400,
temperature=0,
system=system_prompt,
messages=messages,
)
return response.content[0].text
answer = answer_with_anthropic("Can I get an overdraft fee waived?")
print(answer)
- •Add a small orchestration layer for production use.
In real systems, your agent should handle timeouts, empty retrievals, and fallback behavior. Keep retries around the Worker call, not around model generation unless you have idempotency controls.
def rag_answer(question: str) -> dict:
try:
retrieval = retrieve_context(question)
chunks = retrieval.get("chunks", [])
if not chunks:
return {
"answer": "I don't have enough information in the bank policy corpus to answer that.",
"sources": []
}
messages, system_prompt = build_messages(question, chunks)
response = client.messages.create(
model="claude-3-5-sonnet-latest",
max_tokens=400,
temperature=0,
system=system_prompt,
messages=messages,
)
return {
"answer": response.content[0].text,
"sources": [c["doc_id"] for c in chunks]
}
except requests.RequestException as e:
return {"answer": f"Retrieval failed: {str(e)}", "sources": []}
print(rag_answer("What documents do I need to open a student checking account?"))
Testing the Integration
Run a direct smoke test against both services:
if __name__ == "__main__":
question = "How many overdraft fee waivers can a customer receive per month?"
result = rag_answer(question)
print("ANSWER:")
print(result["answer"])
print("\nSOURCES:")
print(result["sources"])
Expected output:
ANSWER:
A customer eligible under this policy can receive one overdraft fee waiver per month. The policy states that direct deposit of $500 or more per statement cycle qualifies for eligibility.
SOURCES:
['policy-102', 'faq-044']
If you get an empty source list or a generic answer, check these first:
- •Your Worker is returning chunk text, not just IDs
- •The Worker URL is reachable from your Python runtime
- •The Anthropic prompt includes enough retrieved evidence
- •
temperatureis set to0for policy-style answers
Real-World Use Cases
- •
Policy Q&A assistant for branch staff
- •Staff ask questions about fees, eligibility rules, card replacement timelines, or account opening requirements.
- •The Worker retrieves approved policy snippets; Anthropic generates a concise answer with sources.
- •
Customer support copilot
- •Agents use it during live chats to summarize relevant product disclosures and next-best actions.
- •You keep answers grounded in current bank documentation instead of relying on memory.
- •
Internal compliance reviewer
- •Analysts query procedures like dispute handling or complaint escalation.
- •The RAG layer pulls only approved internal content from Cloudflare-backed storage before Anthropic drafts a response.
The pattern is simple: Cloudflare Workers handles retrieval at the edge, Anthropic handles controlled generation. That separation keeps your banking assistant fast, auditable, and easier to govern.
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