How to Integrate LangGraph for fintech with Kubernetes for RAG
When you combine LangGraph for fintech with Kubernetes, you get a clean way to run regulated, retrieval-heavy agent workflows at scale. The useful pattern here is simple: LangGraph handles the decision flow for RAG, while Kubernetes gives you isolated, repeatable infrastructure for vector stores, retrievers, and agent workers.
For fintech teams, this matters because you usually need more than a single prompt chain. You need branching logic, retries, policy checks, document retrieval, and predictable deployment across environments.
Prerequisites
- •Python 3.10+
- •A running Kubernetes cluster
- •Minikube, kind, EKS, GKE, or AKS
- •
kubectlconfigured against your cluster - •Access to a vector store deployed in Kubernetes
- •Example: pgvector on Postgres, Weaviate, or Qdrant
- •LangGraph installed
- •LangChain ecosystem packages for retrieval
- •A container registry if you plan to deploy the agent to Kubernetes
- •Environment variables set for:
- •
KUBECONFIG - •vector store connection string
- •LLM provider API key
- •
Install the core Python packages:
pip install langgraph langchain langchain-community kubernetes psycopg2-binary
Integration Steps
- •Define your RAG workflow in LangGraph.
For fintech use cases, keep the graph explicit. A typical flow is: receive question, retrieve policy or product docs, grade relevance, generate answer.
from typing import TypedDict, List
from langgraph.graph import StateGraph, START, END
from langchain_core.documents import Document
class RAGState(TypedDict):
question: str
documents: List[Document]
answer: str
def retrieve_docs(state: RAGState) -> RAGState:
# Replace this with your actual retriever call
docs = [
Document(page_content="KYC checks are required before account activation."),
Document(page_content="Wire transfers above threshold require additional review."),
]
return {**state, "documents": docs}
def generate_answer(state: RAGState) -> RAGState:
context = "\n".join(doc.page_content for doc in state["documents"])
answer = f"Based on policy docs:\n{context}\n\nAnswer: manual review required."
return {**state, "answer": answer}
graph = StateGraph(RAGState)
graph.add_node("retrieve_docs", retrieve_docs)
graph.add_node("generate_answer", generate_answer)
graph.add_edge(START, "retrieve_docs")
graph.add_edge("retrieve_docs", "generate_answer")
graph.add_edge("generate_answer", END)
app = graph.compile()
- •Connect your retriever to a service running in Kubernetes.
In practice, your retriever is usually backed by a database or search service exposed inside the cluster. Use the Kubernetes Python client to discover the service endpoint and build the connection string dynamically.
from kubernetes import client, config
def get_service_endpoint(namespace: str, service_name: str) -> str:
config.load_kube_config()
v1 = client.CoreV1Api()
svc = v1.read_namespaced_service(service_name=service_name, namespace=namespace)
# ClusterIP example; adapt for LoadBalancer/Ingress if needed.
ip = svc.spec.cluster_ip
port = svc.spec.ports[0].port
return f"http://{ip}:{port}"
vector_store_url = get_service_endpoint("fintech-ai", "qdrant")
print(vector_store_url)
If you use Postgres with pgvector instead of a dedicated vector DB:
import os
postgres_host = os.getenv("PGVECTOR_HOST", "pgvector.fintech-ai.svc.cluster.local")
postgres_port = os.getenv("PGVECTOR_PORT", "5432")
connection_string = (
f"postgresql+psycopg2://{os.environ['PGVECTOR_USER']}:"
f"{os.environ['PGVECTOR_PASSWORD']}@{postgres_host}:{postgres_port}/ragdb"
)
print(connection_string)
- •Wire the retriever into the LangGraph node.
This is where the two systems actually meet. Your graph node should call the service deployed in Kubernetes and return documents into state.
from langchain_community.vectorstores import PGVector
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
vectorstore = PGVector(
connection_string=connection_string,
embedding_function=embeddings,
collection_name="fintech_policy_docs",
)
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
def retrieve_docs_from_cluster(state: RAGState) -> RAGState:
docs = retriever.invoke(state["question"])
return {**state, "documents": docs}
Replace the earlier retrieval node:
graph = StateGraph(RAGState)
graph.add_node("retrieve_docs", retrieve_docs_from_cluster)
graph.add_node("generate_answer", generate_answer)
graph.add_edge(START, "retrieve_docs")
graph.add_edge("retrieve_docs", "generate_answer")
graph.add_edge("generate_answer", END)
app = graph.compile()
- •Deploy the agent as a Kubernetes workload.
Package the graph into an API worker so it can run as a pod alongside your retrieval services. This keeps scaling and rollout control inside Kubernetes.
from fastapi import FastAPI
from pydantic import BaseModel
api = FastAPI()
class QueryRequest(BaseModel):
question: str
@api.post("/rag")
def rag_endpoint(req: QueryRequest):
result = app.invoke({"question": req.question, "documents": [], "answer": ""})
return {"answer": result["answer"]}
A minimal deployment manifest should point to this container and let Kubernetes manage replicas:
apiVersion: apps/v1
kind: Deployment
metadata:
name: fintech-rag-agent
spec:
replicas: 2
selector:
matchLabels:
app: fintech-rag-agent
template:
metadata:
labels:
app: fintech-rag-agent
spec:
containers:
- name: agent
image: your-registry/fintech-rag-agent:latest
ports:
- containerPort: 8000
- •Add operational controls for fintech workloads.
For regulated environments, don’t stop at “it runs.” Use Kubernetes primitives for secrets and resource isolation so your graph can’t accidentally drift into unsafe behavior.
import os
# Read secrets injected by Kubernetes Secrets or External Secrets Operator.
openai_key = os.environ["OPENAI_API_KEY"]
db_password = os.environ["PGVECTOR_PASSWORD"]
# Optional runtime guardrail example.
ALLOWED_TOPICS = {"kyc", "aml", "wire_transfer", "account_opening"}
def validate_question(state: RAGState) -> RAGState:
q_lower = state["question"].lower()
if not any(topic in q_lower for topic in ALLOWED_TOPICS):
return {**state, "answer": "Question out of scope for this workflow."}
return state
graph = StateGraph(RAGState)
graph.add_node("validate_question", validate_question)
graph.add_node("retrieve_docs", retrieve_docs_from_cluster)
graph.add_node("generate_answer", generate_answer)
graph.add_edge(START, "validate_question")
graph.add_edge("validate_question", "retrieve_docs")
graph.add_edge("retrieve_docs", "generate_answer")
graph.add_edge("generate_answer", END)
app = graph.compile()
Testing the Integration
Run a direct invocation first before putting traffic behind an ingress or service mesh.
test_input = {
"question": "Do wire transfers above $10k require extra review?",
"documents": [],
"answer": "",
}
result = app.invoke(test_input)
print(result["answer"])
Expected output:
Based on policy docs:
KYC checks are required before account activation.
Wire transfers above threshold require additional review.
Answer: manual review required.
If you want a quick Kubernetes-side check after deployment:
kubectl get pods -n fintech-ai
kubectl logs deploy/fintech-rag-agent -n fintech-ai --tail=50
Real-World Use Cases
- •
AML analyst copilot
- •Pulls internal policy docs from a vector store in Kubernetes and answers case-review questions through a LangGraph workflow with approval branches.
- •
Customer support assistant for banking products
- •Retrieves product terms from cluster-hosted search infrastructure and generates compliant answers with fallback escalation paths.
- •
Operations triage bot
- •Reads incident runbooks from a RAG index and routes issues based on severity while Kubernetes handles scaling during incident spikes.
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