How to Integrate Next.js for banking with Vercel AI SDK for AI agents
If you’re building AI agents for banking, you need two things: a trusted system of record and a controlled way to let the model act on it. Next.js for banking gives you the application surface and backend workflows, while Vercel AI SDK gives you the agent runtime to route prompts, tools, and structured outputs.
The useful pattern here is not “LLM talks directly to bank APIs.” It’s “LLM calls a bounded agent service, which then calls your Next.js banking endpoints with policy checks, audit logging, and deterministic responses.”
Prerequisites
- •Python 3.11+
- •A Next.js for banking app with API routes or server actions exposed for:
- •account lookup
- •transaction history
- •payment initiation
- •Vercel AI SDK installed in your Next.js project:
- •
ai - •
@ai-sdk/openaior another provider
- •
- •A Python client to call your Next.js backend
- •Environment variables configured:
- •
NEXTJS_BANKING_BASE_URL - •
NEXTJS_BANKING_API_KEY - •
OPENAI_API_KEYor provider equivalent
- •
- •Basic auth and authorization already enforced on the banking side
Integration Steps
- •Expose a stable banking API from Next.js
Your agent should never scrape pages. Put bank operations behind explicit API endpoints.
import requests
BASE_URL = "https://banking.example.com"
API_KEY = "your-nextjs-banking-api-key"
def get_account_balance(account_id: str) -> dict:
url = f"{BASE_URL}/api/accounts/{account_id}/balance"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
}
response = requests.get(url, headers=headers, timeout=15)
response.raise_for_status()
return response.json()
def list_transactions(account_id: str, limit: int = 10) -> dict:
url = f"{BASE_URL}/api/accounts/{account_id}/transactions"
params = {"limit": limit}
headers = {"Authorization": f"Bearer {API_KEY}"}
response = requests.get(url, headers=headers, params=params, timeout=15)
response.raise_for_status()
return response.json()
The important part is that these endpoints are narrow and predictable. Your agent should ask for only what it needs: balance, transactions, beneficiary lookup, payment initiation.
- •Wrap Vercel AI SDK behind an agent endpoint
Use Vercel AI SDK in a Next.js route to turn user intent into tool calls. The Python side will call this endpoint as the agent orchestrator.
import requests
AGENT_URL = "https://banking.example.com/api/agent"
def run_agent(prompt: str, session_id: str) -> dict:
payload = {
"messages": [
{"role": "user", "content": prompt}
],
"sessionId": session_id,
}
response = requests.post(AGENT_URL, json=payload, timeout=30)
response.raise_for_status()
return response.json()
On the Next.js side, this endpoint typically uses Vercel AI SDK primitives like generateText, streamText, and tool. For example, your route can expose a tool named getBalance that maps to your internal banking service.
- •Call banking tools from the agent flow
This is the control point. The model decides which tool to use; your backend decides how it executes safely.
from typing import Any
def ask_agent_for_balance(account_id: str) -> Any:
prompt = (
f"Check the current balance for account {account_id}. "
"Use the banking tools available to you."
)
result = run_agent(prompt=prompt, session_id=f"acct-{account_id}")
return result
if __name__ == "__main__":
output = ask_agent_for_balance("acc_12345")
print(output)
In practice, your Next.js route will use Vercel AI SDK tool definitions like:
- •
generateText({ model, tools }) - •
streamText({ model, tools }) - •
tool({ description, parameters, execute })
That gives you structured tool invocation instead of free-form guessing.
- •Add a transaction initiation path with explicit confirmation
Banking agents should not auto-send money because a user asked vaguely. Require a confirmation step before calling any payment endpoint.
import requests
def initiate_payment(from_account: str, to_account: str, amount: float) -> dict:
url = f"{BASE_URL}/api/payments/initiate"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
"Idempotency-Key": f"{from_account}-{to_account}-{amount}",
}
payload = {
"fromAccount": from_account,
"toAccount": to_account,
"amount": amount,
"currency": "USD",
"requiresUserConfirmation": True,
}
response = requests.post(url, headers=headers, json=payload, timeout=20)
response.raise_for_status()
return response.json()
Use this pattern:
- •agent drafts payment intent
- •backend returns a pending action
- •user confirms in UI
- •Next.js endpoint executes transfer
That keeps Vercel AI SDK in orchestration mode instead of execution mode for high-risk actions.
- •Return structured responses back to Python
Don’t parse prose if you can avoid it. Make the agent return JSON so downstream systems can log or render it cleanly.
import json
def summarize_agent_response(raw_response: dict) -> dict:
content = raw_response.get("content", raw_response)
if isinstance(content, str):
return json.loads(content)
return content
agent_result = ask_agent_for_balance("acc_12345")
structured = summarize_agent_response(agent_result)
print(structured["accountId"])
print(structured["balance"])
A good contract looks like this:
| Field | Type | Purpose |
|---|---|---|
accountId | string | Target account |
balance | number | Current available balance |
currency | string | ISO currency code |
source | string | Which tool produced it |
timestamp | string | Audit trail |
Testing the Integration
Run an end-to-end check from Python against your Next.js-backed agent endpoint.
def test_integration():
result = run_agent(
prompt="Get the balance for account acc_12345 and return JSON only.",
session_id="test-session-001",
)
print("Raw result:", result)
if __name__ == "__main__":
test_integration()
Expected output:
Raw result: {
"accountId": "acc_12345",
"balance": 18420.55,
"currency": "USD",
"source": "getBalance",
"timestamp": "2026-04-21T10:15:00Z"
}
If you get plain text instead of JSON, tighten the system prompt in your Vercel AI SDK route and force structured output at the tool boundary.
Real-World Use Cases
- •
Balance and cash-flow assistants
- •Let users ask natural-language questions like “Can I afford payroll next Friday?”
- •The agent pulls balances and recent transactions through Next.js APIs.
- •
Payment ops copilots
- •Draft payment instructions from chat.
- •Require human confirmation before executing transfers through protected endpoints.
- •
Customer service triage
- •Route “Where is my card payment?” queries to transaction lookup tools.
- •Return structured answers that support both chat UI and CRM logging.
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