使用Vercel生成式UI调用Gemini API出现429配额超限问题求助
问题描述
使用Vercel生成式UI搭配AI SDK,基于Next 14 + TypeScript开发,采用Google Gemini的gemini-1.5-pro-latest模型。本地运行完全正常,但部署到Vercel后返回429资源耗尽错误:
responseBody: '{
"error": {
"code": 429,
"message": "Resource has been exhausted (e.g. check quota).",
"status": "RESOURCE_EXHAUSTED"
}
}'
核心服务端调用函数代码如下:
export async function continueConversation( input: string ): Promise<ClientMessage> { "use server"; const history = getMutableAIState(); const result = await streamUI({ model: google("models/gemini-1.5-pro-latest"), system: ` You are a general purpose assistant, you can help the user with a variety of tasks. You can tell jokes, give place and song recommendations, and much more. You are a professional, don't use emote. `, messages: [...history.get(), { role: "user", content: input }], text: ({ content, done }) => { if (done) { history.done((messages: ServerMessage[]) => [ ...messages, { role: "assistant", content }, ]); } return ( <article className="markdown-container"> <Markdown remarkPlugins={[remarkGfm]}>{content}</Markdown> </article> ); }, tools: { getJoke: { description: "A tool when the user wants a joke. The joke should make the user laugh.", parameters: z.object({ category: z.string().optional().describe("the category of the joke"), }), generate: async function* ({ category }) { yield <LoaderCircle />; const joke = await generateObject({ model: google("models/gemini-1.5-pro-latest"), schema: jokeSchema, prompt: "Generate a joke that will make the user laugh. The joke should be in the category of " + category + ". If no category is provided, ask the user for a category.", }); return <JokeComponent joke={joke.object} />; }, }, getPlaces: { description: "A tool when the user wants place recommendations based on the location and type.", parameters: z.object({ location: z.string().describe("the user's location"), type: z.string().optional().describe("the type of place"), }), generate: async function* ({ location, type }) { yield <LoaderCircle className="loader-circle" />; const places = await generateObject({ model: google("models/gemini-1.5-pro-latest"), schema: placeSchema, prompt: "Generate an array of places to visit in " + location + " with the type of " + (type || "any type") + ". The array should contain at least 5 places.", }); if (places && places.object && Array.isArray(places.object)) { return <PlaceComponent place={places.object} />; } else { return <p>Something went wrong, please try again later.</p>; } }, }, getSongs: { description: "A tool when the user wants song recommendations based on the genre.", parameters: z.object({ genre: z.string().optional().describe("the genre of the song"), singer: z.string().optional().describe("the singer of the song"), }), generate: async function* ({ genre, singer }) { yield <LoaderCircle />; const songs = await generateObject({ model: google("models/gemini-1.5-pro-latest"), schema: songSchema, prompt: "Generate songs recommendation in the genre of " + (genre || "any genres") + "or by the singer " + (singer || "any singer") + ". Return an array of 3 songs.", }); if (songs && songs.object && Array.isArray(songs.object)) { return <SongComponent song={songs.object} />; } else { return <p>Something went wrong, please try again later.</p>; } }, }, }, }); return { id: nanoid(), role: "assistant", display: result.value, }; }
已尝试更换多个新账号的API Key,问题依旧,但本地环境可正常运行,需要解决建议。
解决建议
- 验证Vercel环境变量配置:确认Vercel项目中Google Gemini的API Key环境变量已正确设置,变量名与代码中读取的一致。可在服务端函数中添加日志验证变量是否存在(注意不要泄露密钥)。
- 检查Gemini模型配额:登录Google AI Studio查看
gemini-1.5-pro-latest的配额使用情况,确认请求数、令牌数是否达上限。新账号的1.5 Pro模型配额较低,可临时切换到gemini-1.5-flash-latest模型测试,排查是否为配额问题。 - 优化API调用逻辑:当前工具函数(getJoke/getPlaces/getSongs)每次调用都会额外发起一次Gemini请求,单用户交互可能触发多次模型调用,加速配额消耗。可将工具功能整合到主prompt中,或添加请求节流机制,限制单位时间内的请求次数。
- 排查Vercel自身限流:Vercel的边缘函数/Serverless函数有请求频率限制,若部署后短时间内请求量较大,可能触发Vercel层面的限流。查看Vercel项目日志确认错误来源,必要时切换函数部署类型(如从Edge改为Serverless)或联系Vercel调整限额。
- 确认网络连通性:Vercel部署节点可能存在网络限制,导致无法正常访问Gemini API。可在服务端函数中添加网络诊断代码(如请求公开API),验证节点能否正常连接Google AI服务。
内容的提问来源于stack exchange,提问作者salm00n
相关产品推荐
相关产品推荐

