背景Vercel AI SDK 是 Next.js 应用接 LLM 的标准方式。streamText tool calling 让前端能流式接收 LLM 响应,把 SERP API 作为 tool 暴露给 LLM 是常见需求。安装npminstallai ai-sdk/anthropicTool 定义app/api/chat/route.ts:import{streamText,tool}fromai;import{anthropic}fromai-sdk/anthropic;import{z}fromzod;exportconstmaxDuration30;exportasyncfunctionPOST(req:Request){const{messages}awaitreq.json();constresultstreamText({model:anthropic(claude-sonnet-4-5),messages,tools:{googleSearch:tool({description:搜索 Google 实时结果,返回前 5 条 organic PAA,parameters:z.object({query:z.string().describe(搜索关键词),}),execute:async({query}){constrawaitfetch(https://api.serpbase.dev/google/search,{method:POST,headers:{X-API-Key:process.env.SERPBASE_API_KEY!,Content-Type:application/json,},body:JSON.stringify({q:query,gl:us,hl:en,num:5,}),});if(!r.ok)thrownewError(SERP API${r.status});constdataawaitr.json();constsources(data.organic||[]).map((item:any,i:number)[${i1}]${item.title}\n${item.link}\n${item.snippet||});constpaa(data.people_also_ask||[]).slice(0,3).map((q:any)-${q.question||q}).join(\n);return{sources:sources.join(\n\n),relatedQuestions:paa,requestId:data.request_id,};},}),},maxSteps:3,});returnresult.toDataStreamResponse();}前端调用app/page.tsx:use client; import { useChat } from ai/react; export default function Chat() { const { messages, input, handleInputChange, handleSubmit } useChat({ api: /api/chat, }); return ( div classNameflex flex-col w-full max-w-2xl mx-auto p-4 div classNamespace-y-4 mb-4 {messages.map((m) ( div key{m.id} classNamep-3 rounded bg-gray-100 strong{m.role}:/strong {m.content} /div ))} /div form onSubmit{handleSubmit} classNameflex gap-2 input value{input} onChange{handleInputChange} placeholder问点什么... classNameflex-1 p-2 border rounded / button typesubmit classNamep-2 bg-blue-500 text-white rounded 发送 /button /form /div ); }部署注意process.env.SERPBASE_API_KEY必须在 Vercel Dashboard 配置maxDuration 30给 SERP API LLM 推理足够时间maxSteps: 3防止 agent 无限循环Vercel Edge Runtime 也能跑(把 fetch 改成 Edge 兼容)成本跟其他 framework 一样,大头是 LLM,SERP 部分 0.9 美元 / 月(Starter Boost,3,000 调用)。Vercel AI SDK 相对 LangChain 的优势原生 streaming:不用自己拼 SSE,SDK 处理React Hook 集成:useChat5 行接前端Edge Runtime 兼容:全球部署延迟低TypeScript 一等公民:tool 定义、参数校验都 type-safe如果你的项目是 Next.js / Vercel 部署,这套是最顺的路径。跟 LangChain / LlamaIndex 的对比维度Vercel AI SDKLangChainLlamaIndex适配场景Next.js / 前端后端 agent数据源 RAGStreaming原生自己拼自己拼部署Vercel 优化任意任意学习曲线低中中Tool 调用tool()ToolFunctionToolSERP API 在三家都能接,选 framework 看主战场。注意点API key 必须放 Vercel 环境变量,不要 hardcodemaxSteps: 3防止 agent 循环(每步 1 次 LLM 调用 1 次 tool)SERP 响应塞 LLM context 会增加 token,生产用num: 5别开太大100 次免费试用:serpbase.dev 注册,不用绑卡,跑通 demo 后 deploy 到 Vercel。