How to Integrate FastAPI for banking with LangChain for startups
FastAPI is the right edge for banking workflows when you need typed request handling, auth boundaries, and predictable latency. LangChain fits on the agent side when you want an LLM to interpret intent, call tools, and orchestrate multi-step actions like balance lookup, transaction categorization, or payment initiation.
Together, they let a startup build an AI assistant that can answer customer questions, query banking services, and trigger controlled operations without turning your app into a pile of ad hoc prompts.
Prerequisites
- •Python 3.10+
- •A FastAPI service exposing banking endpoints
- •A LangChain-compatible LLM provider key
- •
pip install fastapi uvicorn httpx langchain langchain-openai pydantic - •Basic knowledge of:
- •FastAPI route handlers
- •Pydantic models
- •LangChain tools and agents
- •A local
.envor secret manager for:- •
OPENAI_API_KEY - •banking API base URL
- •banking API auth token
- •
Integration Steps
- •
Expose banking operations through FastAPI
Your banking service should expose narrow, typed endpoints. Keep them explicit so the agent can only call approved operations.
from fastapi import FastAPI, Header, HTTPException from pydantic import BaseModel app = FastAPI(title="Banking API") class TransferRequest(BaseModel): from_account: str to_account: str amount: float currency: str = "USD" @app.get("/accounts/{account_id}/balance") async def get_balance(account_id: str, authorization: str = Header(...)): if authorization != "Bearer demo-token": raise HTTPException(status_code=401, detail="Unauthorized") return {"account_id": account_id, "balance": 1250.75, "currency": "USD"} @app.post("/transfers") async def create_transfer(payload: TransferRequest, authorization: str = Header(...)): if authorization != "Bearer demo-token": raise HTTPException(status_code=401, detail="Unauthorized") return { "status": "submitted", "transfer_id": "trf_12345", "from_account": payload.from_account, "to_account": payload.to_account, "amount": payload.amount, "currency": payload.currency, } - •
Wrap FastAPI endpoints as LangChain tools
Use
httpxinside LangChain tools so the agent can call your banking API over HTTP. This keeps the agent isolated from internal implementation details.import os import httpx from langchain_core.tools import tool BANKING_API_BASE = os.getenv("BANKING_API_BASE", "http://localhost:8000") BANKING_TOKEN = os.getenv("BANKING_TOKEN", "demo-token") @tool def get_account_balance(account_id: str) -> str: """Fetch an account balance from the banking API.""" response = httpx.get( f"{BANKING_API_BASE}/accounts/{account_id}/balance", headers={"Authorization": f"Bearer {BANKING_TOKEN}"}, timeout=10.0, ) response.raise_for_status() return response.json().__str__() @tool def initiate_transfer(from_account: str, to_account: str, amount: float) -> str: """Create a bank transfer via the banking API.""" response = httpx.post( f"{BANKING_API_BASE}/transfers", json={ "from_account": from_account, "to_account": to_account, "amount": amount, "currency": "USD", }, headers={"Authorization": f"Bearer {BANKING_TOKEN}"}, timeout=10.0, ) response.raise_for_status() return response.json().__str__() - •
Build a LangChain agent that uses those tools
Use a tool-calling agent so the model decides when to query balances or initiate transfers. For production systems, keep the prompt narrow and require structured outputs where possible.
import os from langchain_openai import ChatOpenAI from langchain.agents import create_tool_calling_agent, AgentExecutor from langchain_core.prompts import ChatPromptTemplate llm = ChatOpenAI( model="gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"], temperature=0, ) prompt = ChatPromptTemplate.from_messages([ ("system", "You are a banking assistant. " "Only use approved tools. " "Never invent balances or transfer results."), ("human", "{input}"), ("placeholder", "{agent_scratchpad}"), ]) tools = [get_account_balance, initiate_transfer] agent = create_tool_calling_agent(llm=llm, tools=tools, prompt=prompt) executor = AgentExecutor(agent=agent, tools=tools, verbose=True) - •
Expose the agent through FastAPI
This gives your startup one HTTP entry point for chat while keeping all financial actions behind your internal policy layer.
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI(title="AI Banking Assistant") class ChatRequest(BaseModel): message: str @app.post("/chat") async def chat(req: ChatRequest): result = await executor.ainvoke({"input": req.message}) return {"answer": result["output"]} - •
Run both services and wire them together
Start the banking API first, then the AI assistant service.
uvicorn banking_api:app --reload --port 8000 uvicorn ai_assistant:app --reload --port 9000
Testing the Integration
Use a simple request against the AI endpoint and verify it routes through LangChain to FastAPI.
import httpx
response = httpx.post(
"http://localhost:9000/chat",
json={"message": "What is the balance for account abc123?"},
timeout=20.0,
)
print(response.status_code)
print(response.json())
Expected output:
{
"answer": "{\"account_id\": \"abc123\", \"balance\": 1250.75, \"currency\": \"USD\"}"
}
If you test a transfer request like "Move 100 USD from checking1 to savings1", you should see a submitted transfer response instead of a guessed answer.
Real-World Use Cases
- •
Customer support agent
- •Answer balance questions.
- •Explain recent transactions.
- •Escalate suspicious activity to a human operator.
- •
Internal ops assistant
- •Let finance teams query account status in natural language.
- •Trigger controlled payout or transfer workflows.
- •Generate audit-friendly summaries of actions taken.
- •
Startup onboarding concierge
- •Guide new users through funding accounts.
- •Verify account status before enabling premium features.
- •Surface next steps based on user intent and bank state.
The pattern that matters here is separation of concerns.
FastAPI owns policy enforcement and typed financial operations. LangChain owns reasoning and orchestration. That gives you an AI agent system that is usable for startups and still sane enough to ship in regulated environments.
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