How to Integrate LangChain for retail banking with Stripe for production AI
Combining LangChain for retail banking with Stripe gives you a clean path from customer intent to payment execution. In practice, this lets you build AI agents that can answer banking questions, create payment intents, collect card payments, and reconcile transactions without stitching together brittle one-off scripts.
Prerequisites
- •Python 3.10+
- •A Stripe account with API keys from the Dashboard
- •A LangChain-compatible retail banking setup
- •Access to your bank’s customer/account data source or sandbox API
- •Installed packages:
- •
langchain - •
langchain-openai - •
stripe - •
python-dotenv
- •
- •Environment variables configured:
- •
OPENAI_API_KEY - •
STRIPE_API_KEY
- •
Install the dependencies:
pip install langchain langchain-openai stripe python-dotenv
Integration Steps
- •
Initialize Stripe and your LangChain model
Keep secrets in environment variables and initialize both SDKs early. For production systems, do this in a bootstrap module, not inside request handlers.
import os
from dotenv import load_dotenv
import stripe
from langchain_openai import ChatOpenAI
load_dotenv()
stripe.api_key = os.environ["STRIPE_API_KEY"]
llm = ChatOpenAI(
model="gpt-4o-mini",
temperature=0,
)
- •
Define banking context and a payment workflow
Your agent needs enough banking context to decide whether to trigger a payment. In retail banking, that usually means checking balance, verifying customer intent, and then creating a Stripe PaymentIntent.
from typing import Dict
def get_customer_account_snapshot(customer_id: str) -> Dict:
# Replace with your core banking / ledger lookup
return {
"customer_id": customer_id,
"available_balance": 1250.75,
"currency": "usd",
"status": "active",
}
def can_proceed_with_payment(snapshot: Dict, amount_cents: int) -> bool:
return snapshot["status"] == "active" and snapshot["available_balance"] * 100 >= amount_cents
- •
Create a Stripe PaymentIntent from the agent decision
This is the actual bridge between LangChain reasoning and Stripe execution. The agent decides, your code enforces policy, and Stripe handles payment orchestration.
import stripe
def create_payment_intent(amount_cents: int, currency: str, customer_email: str):
return stripe.PaymentIntent.create(
amount=amount_cents,
currency=currency,
receipt_email=customer_email,
automatic_payment_methods={"enabled": True},
metadata={
"source": "langchain_retail_banking_agent",
"workflow": "bill_payment",
},
)
customer_id = "cust_001"
customer_email = "alex@example.com"
amount_cents = 4999
snapshot = get_customer_account_snapshot(customer_id)
if can_proceed_with_payment(snapshot, amount_cents):
intent = create_payment_intent(
amount_cents=amount_cents,
currency=snapshot["currency"],
customer_email=customer_email,
)
print(intent["id"], intent["client_secret"])
else:
print("Insufficient funds or inactive account")
- •
Wrap the flow as a LangChain tool
In production AI agents, don’t let the model call Stripe directly. Expose a controlled tool that validates inputs before creating payments.
from langchain_core.tools import tool
@tool
def initiate_bill_payment(customer_id: str, amount_cents: int, customer_email: str) -> str:
"""
Initiate a bill payment after validating account status and balance.
"""
snapshot = get_customer_account_snapshot(customer_id)
if not can_proceed_with_payment(snapshot, amount_cents):
return "Payment rejected: insufficient funds or inactive account"
intent = create_payment_intent(
amount_cents=amount_cents,
currency=snapshot["currency"],
customer_email=customer_email,
)
return f"PaymentIntent created: {intent['id']}"
print(initiate_bill_payment.invoke({
"customer_id": "cust_001",
"amount_cents": 4999,
"customer_email": "alex@example.com",
}))
- •
Add an agent layer for natural language requests
This is where LangChain earns its keep. The user asks for a payment in plain English, the agent extracts intent, and your tool handles execution.
from langchain_core.prompts import ChatPromptTemplate
from langchain.agents import create_tool_calling_agent, AgentExecutor
prompt = ChatPromptTemplate.from_messages([
("system", "You are a retail banking assistant. Only use approved tools for payments."),
("human", "{input}"),
])
tools = [initiate_bill_payment]
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
result = executor.invoke({
"input": "Pay my electricity bill of $49.99 from customer account cust_001 using alex@example.com"
})
print(result["output"])
Testing the Integration
Run a direct smoke test against Stripe’s test mode first. Use test API keys and verify that your tool returns a PaymentIntent ID.
test_result = initiate_bill_payment.invoke({
"customer_id": "cust_001",
"amount_cents": 4999,
"customer_email": "alex@example.com",
})
print(test_result)
Expected output:
PaymentIntent created: pi_3Qxxxxxxxxxxxxxxxxxxxx
If you want to validate the Stripe object directly:
intent = stripe.PaymentIntent.retrieve("pi_3Qxxxxxxxxxxxxxxxxxxxx")
print(intent.status)
Expected output:
requires_payment_method
Real-World Use Cases
- •Bill pay assistant
- •A banking agent answers “Can I pay my utility bill?” and creates a Stripe PaymentIntent after checking available balance.
- •Card-linked repayment workflows
- •Let customers repay overdrafts or installment loans through Stripe while LangChain handles eligibility checks and conversational UX.
- •Dispute and collections automation
- •Build an agent that explains balances, offers payment options, and initiates settlement flows through approved payment rails.
The main pattern is simple: let LangChain handle reasoning and conversation, but keep Stripe calls behind deterministic tools with policy checks. That gives you an agent that is useful in production without turning your payment flow into an uncontrolled prompt experiment.
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