What Is a 500 Error? Fixing One in an AI-Built App
A 500 error means the server hit a problem it could not handle, and the visitor did nothing wrong. The page tells you almost nothing, so the fix starts in the logs.
A 500 error means the server received a valid request and failed while trying to handle it. It is a problem in your app or its environment, not in the visitor's browser. The MDN reference for the 500 status describes it as a generic response used when the server meets a condition it did not expect. "Generic" is the key word: the browser shows no detail, so the real message lives in your server logs.
What the number means
HTTP status codes are grouped by first digit. 2xx means success, 4xx means the request was a problem (a missing page is 404, a login failure is 401 or 403), and 5xx means the server could not complete a valid request. Within the 5xx family:
Code | Meaning in plain language | Typical cause |
|---|---|---|
500 | Something broke inside the app | An unhandled error in your code |
502 | A server in the middle got a bad reply from your app | App crashed or returned garbage behind a proxy |
503 | Service unavailable right now | Overloaded, restarting or under maintenance |
504 | A server in the middle waited too long | Slow database query or external API |
Knowing which one you have tells you where to look. A 500 points at your code. A 502 or 504 points at the connection between services, or at something slow.
Step 1: Reproduce it
Find the exact request that fails. Which page or action? Does it happen every time or only with certain data, such as a particular user or a long text? A fault that only appears with certain inputs is a clue in itself. Write down the steps, because you will give them to the AI tool later.
Step 2: Find the log line
Open your hosting platform's runtime logs and look for the time of your failed request. You are looking for an error message and a stack trace, which is the list of function calls that led to the crash. The top few lines usually name the file and the line number where it failed.
If your app swallows errors and logs nothing, that is a problem to fix first. Stopping an AI coding agent from swallowing errors explains why it happens and what to ask for instead. Connecting an error monitoring service helps too, as described in giving an AI coding agent your error monitoring data.
Step 3: Isolate the cause
The usual suspects for a 500 in an AI-built app:
A value that is
undefinedornullwhere the code expects data.A database query that fails, because a table or column is missing.
A missing environment variable or API key on the server.
An external service returning an error the code does not handle.
Input the code did not expect, such as an empty field or a very large file.
If it only fails live, read why an AI-built app works locally but fails after deploy, since the first four items there account for many production-only 500s.
Step 4: Ask the AI with the right context
A vague question gets a vague answer. Give the tool a bundle:
My app returns a 500 error when [action].
It works when [condition], fails when [condition].
Stack trace (secrets removed):
[paste the trace]
The code of the function named at the top of the trace:
[paste that function]
What I have already checked: [list].
Explain the most likely cause first, then give the smallest change that fixes it.Asking for the smallest change matters. Without it, tools sometimes rewrite surrounding code, which makes the bug harder to find. For help reading the trace itself, see how to get an AI coding agent to explain a stack trace.
What never to paste
API keys, database passwords and tokens, which often appear in error output.
Customer data, such as names, emails and order details, which logs sometimes include.
Full environment files.
Scan the log text before sharing it and replace anything sensitive with a placeholder. If a secret does leak, treat it as compromised and rotate it.
After the fix
Add a test or at least a manual checklist for the case that failed, so it does not return. Make the error produce a helpful message in your logs instead of a bare crash. And decide what visitors see: a plain page that apologizes and offers a way back is better than a raw server error. If the problem hits real users, what to do when your AI-built app breaks in production sets out a calm order of operations. For the bigger picture of building and running apps this way, return to our guide on how to build an app with AI.
FAQ
What causes a 500 internal server error?
An unhandled problem on the server: a bug in the code, a failed database query, a missing setting or an external service failing. The status code alone does not say which.
Is a 500 error my fault or the website's fault?
It is the website's. Visitors cannot fix it by reloading, although a temporary glitch sometimes clears.
How do I fix a 500 error with AI?
Reproduce it, copy the matching stack trace and the code it points to, remove secrets, and ask for the likely cause and the smallest fix.
What is the difference between a 500 and a 404?
A 404 means the page was not found, which is a request problem. A 500 means the server failed while handling a request that was valid.
How did this land?
About the author

Senior Editor, AI & Product
Cecilia leads the Swarmz editorial desk. She has spent a decade turning complex AI and product topics into writing people actually finish, and she owns the blog's quality bar.


