{"slug": "the-ngo-app-s-ai-worked-locally-then-vercel-gave-me-a-504", "title": "The NGO App's AI Worked Locally. Then Vercel Gave Me a 504.", "summary": "A developer debugging a 504 Gateway Timeout on Vercel discovered that the default 10-second function execution limit was killing AI requests before the model could respond. Enabling Fluid Compute in the Vercel project settings extended the limit to 5 minutes, allowing the AI assistant to complete requests that took over 20 seconds.", "body_md": "I was testing the AI assistant locally, and everything was working.\n\nThen I deployed it.\n\nI opened the production app and asked:\n\n`how start meditation. im a beginner`\n\nInstead of an answer, I got:\n\n```\nUnexpected token 'A', \"An error o\"... is not valid JSON\n```\n\nAt first, I thought I had broken something in the frontend.\n\nI hadn't.\n\nI checked the browser Network tab.\n\nThe request was:\n\n```\nPOST /api/ai/chat\n```\n\nand the response was:\n\n```\n504 Gateway Timeout\n```\n\nSo the JSON error wasn't the actual problem. The frontend was trying to parse an error response as JSON, while the request itself had already timed out.\n\nThat changed my debugging path.\n\nInstead of looking at the JSON parser, I needed to find out **why the server request was timing out**.\n\nThis was the confusing part.\n\nThe same AI request worked on my local machine.\n\nThe local terminal showed:\n\n```\nPOST /api/ai/chat 200 in 21994ms\n```\n\nThe logs showed:\n\n```\nAI context retrieved\nmodel: openai/gpt-5-mini\n```\n\nThe AI completion itself took around **17.76 seconds**.\n\nThe usage was:\n\n```\npromptTokens: 1663\ncompletionTokens: 1500\ntotalTokens: 3163\nfinishReason: \"length\"\n```\n\nSo the complete local request was taking roughly **22 seconds**, but it still succeeded.\n\nThat was an important clue.\n\nThe model wasn't simply broken.\n\nOpenRouter wasn't obviously unreachable.\n\nThe application could make the request and receive a response.\n\nSomething was different in production.\n\nI checked the Vercel production logs.\n\nThis time, the error was much more useful:\n\n```\nFUNCTION_INVOCATION_TIMEOUT\n```\n\nAnd there was a number that immediately stood out:\n\n```\nExecution Duration / Maximum\n10.37s / 10s\n```\n\nThe logs also showed that parts of the request had already completed:\n\n```\nClerk authentication succeeded\nAI context retrieval succeeded\nPOST request to OpenRouter was made\nAI completion did not return before the function was terminated\n```\n\nSo the function wasn't failing immediately.\n\nIt was running out of time while waiting for the AI completion.\n\nThat explained the 504.\n\nI checked the repository first.\n\nThere was no:\n\n```\nvercel.json\n```\n\nThere was no route-level:\n\n```\nmaxDuration\n```\n\nThere was no runtime override.\n\nAnd there wasn't a Next.js configuration setting a 10-second function duration.\n\nSo I hadn't configured this timeout in the application code.\n\nI checked the Vercel project settings next.\n\nThat's where I found it.\n\n**Fluid Compute was disabled.**\n\n**Screenshot: Vercel Functions settings showing Fluid Compute disabled**\n\nThat was the missing piece.\n\nI enabled **Fluid Compute** in the Vercel project settings and redeployed the application.\n\nAfter the redeployment, the execution numbers changed from:\n\n```\n10.37s / 10s\n```\n\nto:\n\n```\n26.31s / 5m\n```\n\nThe AI request could now finish.\n\nThe browser displayed the generated response instead of the 504.\n\nThe production logs also showed the rest of the request completing:\n\n```\nconversation_save_start\nconversation_save_end\nusage_persist_start\nusage_persist_end\nai_chat_request_end\n```\n\nThe application recorded:\n\n```\ntotalElapsedMs: 25384\n```\n\nVercel reported approximately:\n\n```\n26.31s\n```\n\nAnd the API returned:\n\n```\n200\n```\n\n**Screenshot: successful production request / Vercel execution metrics**\n\nSo the first production problem was fixed.\n\nI don't want to turn this into the wrong Vercel lesson.\n\nI'm **not** saying:\n\n```\n\"Vercel cannot run AI functions longer than 10 seconds.\"\n```\n\nThat's not what I observed.\n\nWhat I observed was that **this deployment was running with a 10-second maximum while Fluid Compute was disabled**.\n\nAfter enabling Fluid Compute and redeploying, the same function was allowed to run for up to **5 minutes**, and the request completed.\n\nThe fix wasn't making OpenRouter faster.\n\nIt removed the execution limit that was killing the request before the AI response could return.\n\nThe 504 was gone.\n\nThe AI was working in production.\n\nSo I could have stopped there.\n\nBut the successful request was still taking around **25–26 seconds**.\n\nThe local request was around **22 seconds**.\n\nSo fixing one problem exposed another one.\n\nI don't know the cause yet, and I don't want to guess.\n\nInstead, I added temporary timing logs around different parts of the request:\n\n```\nauthentication\nrate limiting\nquota reservation\ndatabase connection\nconversation lookup/create\nAI context retrieval\nOpenRouter request start\nOpenRouter response headers\nOpenRouter full response\nconversation save\nusage persistence\ntotal request duration\n```\n\nNow I'm measuring each part instead of assuming where the time is going.\n\nThat's the next problem.\n\nThe browser showed a JSON parsing error.\n\nThe actual problem was a 504.\n\nThe Network tab gave me the next clue.\n\nLocally, the request took around 22 seconds and worked.\n\nProduction was terminating it after 10 seconds.\n\nThe application code wasn't the only thing that mattered. The execution environment mattered too.\n\nThere was no 10-second timeout configured in my repository.\n\nThe useful information was in the Vercel runtime logs and project settings.\n\nThe request reached OpenRouter, and the same model worked locally.\n\nThere wasn't enough evidence to blame the provider.\n\nFirst find out **which layer is timing out**.\n\nIn this case, the function itself was being terminated before the AI completion could return.\n\nEnabling Fluid Compute fixed the 504.\n\nIt also made the next problem visible:\n\n**Why is the request still taking ~25 seconds?**\n\nThat's probably the simplest way I can describe this debugging session.\n\nI started with a strange frontend error.\n\nThen I checked the Network tab.\n\nThen the production logs.\n\nThen the repository configuration.\n\nThen the Vercel project settings.\n\nEach step removed another assumption.\n\nEventually, the 504 made sense.\n\nThe actual fix was only one setting.\n\n**Finding that setting was the debugging work.**\n\nThe application is live here if you want to have a look:\n\nThe timeout is fixed.\n\nThe request now completes successfully in production.\n\nBut waiting 25+ seconds for an AI response isn't something I want to leave alone.\n\nSo now I'm trying to find out where those seconds are actually going.\n\nIs it the AI request?\n\nContext retrieval?\n\nDatabase work?\n\nSomething else?\n\nI don't know yet.\n\nThat's what I'm measuring now.\n\n**Part 2 will be about finding that answer.**", "url": "https://wpnews.pro/news/the-ngo-app-s-ai-worked-locally-then-vercel-gave-me-a-504", "canonical_source": "https://dev.to/nishchaldev/the-ngo-apps-ai-worked-locally-then-vercel-gave-me-a-504-3ied", "published_at": "2026-09-09 01:49:08+00:00", "updated_at": "2026-09-09 02:19:20.377235+00:00", "lang": "en", "topics": ["developer-tools", "ai-infrastructure", "ai-products"], "entities": ["Vercel", "OpenRouter", "Clerk", "Next.js", "GPT-5-mini"], "alternates": {"html": "https://wpnews.pro/news/the-ngo-app-s-ai-worked-locally-then-vercel-gave-me-a-504", "markdown": "https://wpnews.pro/news/the-ngo-app-s-ai-worked-locally-then-vercel-gave-me-a-504.md", "text": "https://wpnews.pro/news/the-ngo-app-s-ai-worked-locally-then-vercel-gave-me-a-504.txt", "jsonld": "https://wpnews.pro/news/the-ngo-app-s-ai-worked-locally-then-vercel-gave-me-a-504.jsonld"}}