How to Integrate LangChain for healthcare with Twilio for RAG
Combining LangChain for healthcare with Twilio gives you a practical path to build patient-facing RAG agents that can answer questions, retrieve clinical or policy documents, and send responses over SMS or WhatsApp. The useful pattern here is simple: LangChain handles retrieval and generation, while Twilio becomes the delivery channel for secure, low-friction communication.
This is a strong fit for appointment reminders, benefits questions, triage-style FAQ bots, and post-visit follow-ups where the answer should come from approved knowledge sources rather than a free-form model response.
Prerequisites
- •Python 3.10+
- •A Twilio account with:
- •
TWILIO_ACCOUNT_SID - •
TWILIO_AUTH_TOKEN - •a Twilio phone number or WhatsApp-enabled sender
- •
- •A LangChain environment set up for healthcare workflows
- •Access to your healthcare knowledge base:
- •PDFs, policy docs, SOPs, or approved clinical content
- •An embeddings provider and vector store:
- •OpenAI, Azure OpenAI, or another supported embedding model
- •FAISS, Chroma, Pinecone, or similar
- •Installed packages:
- •
langchain - •
langchain-community - •
langchain-openai - •
twilio - •
python-dotenv
- •
Integration Steps
- •Install dependencies and load credentials
Start by wiring your environment variables and installing the core libraries. Keep secrets out of source control and load them at runtime.
pip install langchain langchain-community langchain-openai twilio python-dotenv
import os
from dotenv import load_dotenv
load_dotenv()
TWILIO_ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")
TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
TWILIO_FROM_NUMBER = os.getenv("TWILIO_FROM_NUMBER")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
- •Build the healthcare RAG pipeline in LangChain
Use LangChain to load your healthcare documents, split them into chunks, embed them, and create a retriever. In production, this should point only to approved content sources.
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
loader = PyPDFLoader("healthcare_policy.pdf")
documents = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=120)
chunks = splitter.split_documents(documents)
embeddings = OpenAIEmbeddings(api_key=OPENAI_API_KEY)
vectorstore = FAISS.from_documents(chunks, embeddings)
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
- •Create the RAG chain that answers from retrieved context
Use a retrieval chain so the model answers from your indexed healthcare content instead of guessing. This is the core control point for safe responses.
from langchain_openai import ChatOpenAI
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.chains.retrieval import create_retrieval_chain
from langchain_core.prompts import ChatPromptTemplate
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a healthcare assistant. Answer only from the provided context. If the answer is not in context, say you don't know."),
("human", "Question: {input}\n\nContext:\n{context}")
])
document_chain = create_stuff_documents_chain(llm, prompt)
rag_chain = create_retrieval_chain(retriever, document_chain)
- •Connect Twilio to send the RAG answer by SMS
Twilio’s Python SDK gives you a straightforward way to deliver the generated answer back to the user. This is where your agent becomes reachable through a normal phone number.
from twilio.rest import Client
twilio_client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
def send_sms(to_number: str, message: str):
return twilio_client.messages.create(
body=message,
from_=TWILIO_FROM_NUMBER,
to=to_number
)
def handle_patient_question(phone_number: str, question: str):
result = rag_chain.invoke({"input": question})
answer = result["answer"]
message_sid = send_sms(phone_number, answer)
return {"answer": answer, "message_sid": message_sid.sid}
- •Wrap it in an inbound webhook for real-time messaging
If you want Twilio to receive inbound SMS and trigger retrieval automatically, expose a webhook endpoint in FastAPI or Flask. Twilio will POST incoming messages to this route.
from fastapi import FastAPI, Request
from fastapi.responses import Response
app = FastAPI()
@app.post("/twilio/inbound")
async def twilio_inbound(request: Request):
form = await request.form()
from_number = form.get("From")
body = form.get("Body")
result = rag_chain.invoke({"input": body})
reply_text = result["answer"]
xml_response = f"""<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Message>{reply_text}</Message>
</Response>"""
return Response(content=xml_response, media_type="application/xml")
Testing the Integration
Run a direct test first before putting the webhook behind Twilio. This verifies retrieval works and that SMS delivery is wired correctly.
test_question = "What is the policy for after-hours nurse callbacks?"
result = rag_chain.invoke({"input": test_question})
print("Answer:", result["answer"])
sms_result = send_sms("+15551234567", result["answer"])
print("Twilio SID:", sms_result.sid)
Expected output:
Answer: After-hours nurse callbacks are handled within 30 minutes for urgent cases and by the next business day for non-urgent requests.
Twilio SID: SMxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
If you are testing the webhook locally, use ngrok or another tunnel so Twilio can reach your endpoint. Then send an SMS to your Twilio number and confirm that the reply comes from retrieved policy content.
Real-World Use Cases
- •
Patient support bot over SMS
- •Answer medication refill rules, clinic hours, pre-op instructions, or claims questions from approved documents.
- •
Care navigation assistant
- •Retrieve plan-specific guidance and send follow-up instructions after appointments or discharge events.
- •
Internal staff helper
- •Let nurses or front-desk staff text questions like “What’s our prior-auth workflow?” and get grounded answers instantly.
The production pattern here is straightforward: keep healthcare content in a controlled retriever, keep generation constrained to that context, and use Twilio as the last-mile delivery layer. That gives you an agent that is useful without turning into an uncontrolled chatbot.
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