How to Integrate Next.js for lending with Vercel AI SDK for RAG
Combining Next.js for lending with Vercel AI SDK gives you a clean path from borrower-facing UI to retrieval-augmented generation in the backend. In practice, that means loan officers and underwriting assistants can ask questions over policy docs, credit memos, KYC files, and product guides without wiring up a separate chat stack from scratch.
The useful pattern here is simple: Next.js for lending handles the workflow surface and lending context, while Vercel AI SDK handles generation, streaming, and tool orchestration for RAG. You end up with an agent system that can answer lending questions with grounded citations instead of free-form guesses.
Prerequisites
- •Python 3.10+
- •A Next.js for lending project already running
- •A Vercel project with the AI SDK enabled
- •API keys configured:
- •
OPENAI_API_KEY - •your Next.js for lending backend token or service credential
- •Vercel project token if you are calling deployment APIs
- •
- •A document store or vector index for RAG
- •
pipinstalled
Install the Python dependencies:
pip install requests pydantic openai
If your lending stack exposes a REST API, keep its base URL handy. In this tutorial I’ll assume your lending app exposes endpoints like /api/loan-context and /api/documents/search.
Integration Steps
1) Pull lending context from Next.js for lending
Start by fetching the current loan application context. This gives the RAG layer structured signals like borrower type, product, stage, and jurisdiction.
import os
import requests
NEXTJS_LENDING_BASE_URL = os.getenv("NEXTJS_LENDING_BASE_URL", "http://localhost:3000")
NEXTJS_LENDING_TOKEN = os.getenv("NEXTJS_LENDING_TOKEN")
def fetch_loan_context(application_id: str) -> dict:
url = f"{NEXTJS_LENDING_BASE_URL}/api/loan-context/{application_id}"
headers = {
"Authorization": f"Bearer {NEXTJS_LENDING_TOKEN}",
"Content-Type": "application/json",
}
response = requests.get(url, headers=headers, timeout=20)
response.raise_for_status()
return response.json()
context = fetch_loan_context("app_12345")
print(context)
Use this context to drive retrieval filters. For example, a commercial mortgage application should not retrieve consumer loan policies.
2) Query the lending knowledge base for grounded documents
Next.js for lending usually sits on top of internal docs: underwriting rules, policy PDFs, rate sheets, exception matrices. Expose a search endpoint that returns chunks and metadata.
import os
import requests
from typing import List
def search_lending_docs(query: str, product_type: str) -> List[dict]:
url = f"{NEXTJS_LENDING_BASE_URL}/api/documents/search"
payload = {
"query": query,
"filters": {
"product_type": product_type,
"source": ["policy", "underwriting_manual", "faq"],
},
"top_k": 5,
}
headers = {
"Authorization": f"Bearer {NEXTJS_LENDING_TOKEN}",
"Content-Type": "application/json",
}
response = requests.post(url, json=payload, headers=headers, timeout=30)
response.raise_for_status()
return response.json()["results"]
docs = search_lending_docs(
query="What income documentation is required for self-employed borrowers?",
product_type="commercial_loan",
)
for doc in docs:
print(doc["title"], doc["score"])
This is the retrieval side of RAG. Keep the returned payload small and structured so the model gets only what it needs.
3) Send retrieved context into Vercel AI SDK-compatible generation flow
Vercel AI SDK is usually used in JavaScript on the frontend or server routes. If your agent orchestrator is Python-based, call your Vercel-backed inference endpoint and pass retrieved chunks as grounded context.
A practical pattern is to expose a server route in your Vercel app that accepts messages, context, and citations, then streams the answer back. From Python, call that route directly.
import os
import requests
VERCEL_AGENT_URL = os.getenv("VERCEL_AGENT_URL", "https://your-app.vercel.app/api/chat")
def generate_rag_answer(question: str, loan_context: dict, retrieved_docs: list) -> str:
payload = {
"messages": [
{"role": "system", "content": "You are a lending assistant. Answer only from provided context."},
{"role": "user", "content": question},
],
"context": loan_context,
"documents": retrieved_docs,
}
response = requests.post(VERCEL_AGENT_URL, json=payload, timeout=60)
response.raise_for_status()
return response.json()["answer"]
answer = generate_rag_answer(
question="Can this borrower use bank statements instead of tax returns?",
loan_context=context,
retrieved_docs=docs,
)
print(answer)
On the Vercel side, this route typically uses AI SDK methods like streamText() or generateText() to produce the final answer. Your Python service just feeds it grounded inputs.
4) Persist traces and citations back into Next.js for lending
For auditability in lending workflows, write the answer plus citations back to your app. That makes it usable by underwriters and compliance reviewers later.
import requests
def save_agent_response(application_id: str, question: str, answer: str, sources: list) -> dict:
url = f"{NEXTJS_LENDING_BASE_URL}/api/agent-notes"
payload = {
"application_id": application_id,
"question": question,
"answer": answer,
"sources": sources,
"channel": "rag_assistant",
}
headers = {
"Authorization": f"Bearer {NEXTJS_LENDING_TOKEN}",
"Content-Type": "application/json",
}
response = requests.post(url, json=payload, headers=headers, timeout=20)
response.raise_for_status()
return response.json()
saved = save_agent_response(
application_id="app_12345",
question="Can this borrower use bank statements instead of tax returns?",
answer=answer,
sources=[{"title": d["title"], "url": d["url"]} for d in docs],
)
print(saved)
This step matters in regulated environments. If you cannot reconstruct why the assistant answered something, you do not have an enterprise-ready workflow.
5) Add a thin orchestration layer for retries and fallback retrieval
In production you need retry logic when search returns weak matches or generation fails. Use a second retrieval pass before surfacing an answer.
def rag_answer_with_fallback(application_id: str, question: str) -> str:
ctx = fetch_loan_context(application_id)
primary_docs = search_lending_docs(question, ctx["product_type"])
if not primary_docs:
primary_docs = search_lending_docs(question + f' jurisdiction:{ctx["jurisdiction"]}', ctx["product_type"])
if not primary_docs:
return "No grounded policy match found. Escalate to underwriting."
return generate_rag_answer(question, ctx, primary_docs)
print(rag_answer_with_fallback("app_12345", "What documents are required for income verification?"))
That gives you deterministic behavior under sparse retrieval conditions instead of letting the model improvise.
Testing the Integration
Use one known policy question and verify that you get a grounded answer plus source metadata.
test_question = "What documents are required for self-employed income verification?"
result = rag_answer_with_fallback("app_12345", test_question)
print(result)
Expected output:
Self-employed borrowers must provide two years of personal tax returns...
Sources:
1. Underwriting Manual v4.2 - Income Documentation Policy
2. Commercial Lending FAQ - Self-Employed Borrowers
If you get an uncited generic answer, fix retrieval first. In RAG systems, bad answers usually mean bad chunking or bad filters before they mean model failure.
Real-World Use Cases
- •Underwriter copilot
- •Ask policy questions inside a loan file and get answers tied to approved manuals and rate sheets.
- •Borrower document assistant
- •Help relationship managers tell applicants exactly which files are missing based on loan type and stage.
- •Compliance review helper
- •Summarize why an exception was granted and attach citations from internal policy plus prior decisions.
The main value here is control. Next.js for lending gives you workflow state and business context; Vercel AI SDK gives you the generation layer needed to turn that context into reliable RAG responses.
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