# `504: GATEWAY_TIMEOUT — Code: FUNCTION_INVOCATION_TIMEOUT`

**Where it appears:** Preview or Production site on Vercel

**What it means:** A server action or API route took too long to finish, so Vercel stopped waiting and closed the request.

## Likely causes

- An AI call is waiting on a slow or unresponsive provider instead of failing fast to the backup or streaming.
- A database query or loop is running one query per item instead of fetching in parallel.

## Message for your agent

```text
My deployed site returned `504 FUNCTION_INVOCATION_TIMEOUT`. Read the Vercel runtime logs (`npx vercel logs`), find what timed out, and make sure AI calls use `maxRetries: 0` on Tokentide with the Gateway fallback.
```

**Prevention:** Set `maxRetries: 0` on the main AI client so slow refusals reach the backup immediately, and cap `max_completion_tokens`.

---
Source: [Agentic Makers](https://agenticmakers.app/en/help/errors/504-gateway-timeout-function-invocation-timeout) · License: CC BY 4.0
