How to Integrate LangChain for lending with Twilio for RAG
Combining LangChain for lending with Twilio gives you a clean pattern for borrower-facing RAG agents: retrieve policy, product, and underwriting context from your lending knowledge base, then deliver answers over SMS or WhatsApp. That matters when your ops team needs to answer loan-status questions, document requirements, or rate explanations without forcing borrowers into a portal.
Prerequisites
- •Python 3.10+
- •A LangChain for lending project with:
- •embeddings configured
- •a vector store populated with lending docs
- •a retriever exposed in code
- •A Twilio account with:
- •Account SID
- •Auth Token
- •a verified phone number or WhatsApp sender
- •Installed packages:
- •
langchain - •
twilio - •
python-dotenv
- •
- •Environment variables set:
- •
TWILIO_ACCOUNT_SID - •
TWILIO_AUTH_TOKEN - •
TWILIO_FROM_NUMBER - •
TWILIO_WHATSAPP_FROMif using WhatsApp
- •
- •A webhook endpoint if you want inbound SMS handling
Integration Steps
- •Set up the lending retriever in LangChain.
Your RAG layer should expose a retriever that can pull relevant loan policy snippets, product FAQs, or servicing rules. Keep this separate from Twilio so the messaging layer only handles transport.
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vectorstore = Chroma(
persist_directory="./lending_kb",
embedding_function=embeddings,
)
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
- •Build the RAG chain that answers lending questions.
Use LangChain’s retrieval chain pattern so the agent grounds responses in your lending corpus. For production, keep the prompt strict: no unsupported financial advice, no invented policy details.
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.chains.retrieval import create_retrieval_chain
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
prompt = ChatPromptTemplate.from_messages([
("system", "You are a lending support assistant. Answer only from retrieved context."),
("human", "{input}")
])
document_chain = create_stuff_documents_chain(llm, prompt)
rag_chain = create_retrieval_chain(retriever, document_chain)
response = rag_chain.invoke({"input": "What documents are required for an unsecured personal loan?"})
print(response["answer"])
- •Send the RAG answer through Twilio SMS.
Twilio’s Python SDK is straightforward: instantiate Client, then call messages.create(). This is where the borrower gets the answer after LangChain generates it.
import os
from twilio.rest import Client
twilio_client = Client(
os.environ["TWILIO_ACCOUNT_SID"],
os.environ["TWILIO_AUTH_TOKEN"],
)
answer_text = response["answer"]
message = twilio_client.messages.create(
body=answer_text,
from_=os.environ["TWILIO_FROM_NUMBER"],
to="+15551234567",
)
print(message.sid)
- •Wire inbound SMS into the retrieval flow.
For real usage, borrowers text a question to Twilio, Twilio posts to your webhook, and your app returns a TwiML response. The key method here is MessagingResponse, which lets you reply immediately while still keeping the architecture simple.
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
app = Flask(__name__)
@app.post("/sms")
def sms_webhook():
user_text = request.form.get("Body", "").strip()
result = rag_chain.invoke({"input": user_text})
answer = result["answer"]
resp = MessagingResponse()
resp.message(answer)
return str(resp)
if __name__ == "__main__":
app.run(port=8000)
- •Add guardrails for lending compliance.
Don’t let the model freewheel on rates, approvals, or eligibility. In lending, your assistant should cite policy excerpts and escalate uncertain cases to a human queue.
def safe_answer(question: str) -> str:
result = rag_chain.invoke({"input": question})
answer = result["answer"]
blocked_phrases = [
"guaranteed approval",
"you will be approved",
"final decision"
]
if any(p in answer.lower() for p in blocked_phrases):
return "I can help with general loan guidance, but final decisions require manual review."
return answer
Testing the Integration
Run a direct test before exposing the webhook. This verifies both retrieval and message delivery paths without waiting on an inbound SMS event.
test_question = "How long does underwriting take for a small business term loan?"
result = rag_chain.invoke({"input": test_question})
print("RAG ANSWER:", result["answer"])
sent_message = twilio_client.messages.create(
body=result["answer"],
from_=os.environ["TWILIO_FROM_NUMBER"],
to="+15551234567",
)
print("TWILIO SID:", sent_message.sid)
Expected output:
RAG ANSWER: Underwriting typically takes 2–5 business days after all required documents are received.
TWILIO SID: SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Real-World Use Cases
- •
Borrower support by SMS
- •Answer common questions about required documents, repayment schedules, or status updates without logging into a portal.
- •
Loan officer assist
- •Let internal staff text product questions and get grounded answers from policy docs during calls with applicants.
- •
Collections and servicing workflows
- •Trigger reminder messages that include accurate payment instructions pulled from servicing knowledge bases.
If you want this production-ready, add logging around every retrieval query and every Twilio message SID. That gives you traceability across the full path: question asked, context retrieved, answer sent.
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