Writing Health Check Endpoints With LLMs in November 2026: Separate Liveness From Readiness, Keep Checks Cheap, and Never Let an Optional Dependency Take You Down

Ask an LLM to "add a health check" to a web service and you will usually get something like this: a /health route that pings the database, pings the cache, calls a downstream API, and returns 200 if everything answered. It looks responsible. In production it can be the thing that turns a small blip into a full outage.

Health checks are read by machines, not people. A load balancer, Kubernetes, Docker, or an uptime monitor calls the endpoint over and over and then acts on the answer: it stops sending traffic, or it kills and restarts your process. So the real question is not "is everything fine?" but "what should the platform do right now?" This post covers how to get an LLM to write health checks that answer that question correctly, and how to review what it gives you.

Start by telling the model who is calling

The same endpoint means different things to different callers. Before you prompt, write down which of these will hit your service:

  • A container orchestrator such as Kubernetes. It can run a liveness probe (failure means restart the container), a readiness probe (failure means stop routing traffic to this pod, but leave it running), and a startup probe (holds off the other two until the app has finished starting).
  • A load balancer. A cloud load balancer health check usually removes an unhealthy target from rotation and adds it back when it passes again.
  • Docker's HEALTHCHECK. Marks a container healthy or unhealthy. What happens next depends on what is watching that status.
  • An external uptime monitor. Pages a human when the check fails.

Put this list in your prompt. "Write a liveness endpoint and a readiness endpoint for a Node service that runs on Kubernetes behind a Service, and is also checked by an external uptime monitor" gets a much better answer than "add a health check", because the model now knows that a wrong answer leads to restarts and dropped traffic.

A stethoscope lying on a surface

A liveness check listens for one thing only: is this process still alive? Image: Stethoscope 1 by ernstl, CC BY-SA 2.0, via Wikimedia Commons.

Liveness: is this process stuck?

A liveness check should answer one narrow question: is this process so broken that restarting it would help? Restarting fixes a deadlocked event loop, an exhausted thread pool, or a process wedged in a bad state. Restarting does not fix a database that is down. If your liveness check pings the database, a database outage makes every replica fail liveness at the same time, the orchestrator restarts all of them, and they come back to the same dead database and get restarted again. You have added a restart loop on top of the original problem.

So a good liveness handler is almost boring:

// Liveness: only proves the process can still serve a request.
app.get("/livez", (req, res) => {
  res.status(200).type("text/plain").send("ok");
});

If your app has a known way of getting stuck, such as a background worker loop, you can add an internal signal: the worker updates a timestamp each time round, and liveness fails if that timestamp is older than some limit you choose. That is still a check on this process, not on anything outside it.

LLM-generated code often gets this wrong in one specific way: it reuses the same "check everything" function for both liveness and readiness. When you review, look for any network call inside the liveness handler. If there is one, ask why a restart would fix that dependency failing. Usually it would not, and the call should move to readiness or go away.

Readiness: should traffic come here right now?

Readiness is where dependencies belong, but only the ones this instance truly cannot work without. A readiness failure takes the instance out of rotation without killing it, which is the right response to "I cannot serve requests usefully at the moment."

Go through each dependency and sort it into one of two groups:

  • Hard dependencies. Without them, every request fails. For many apps this is the primary database.
  • Soft dependencies. Without them, some features degrade but the core still works. Caches, recommendation services, analytics, email providers, feature flag services with a local default.

Only hard dependencies go in readiness. If a cache outage fails readiness on every instance, the load balancer has nowhere to send traffic, and a cache problem that should have made pages a little slower now makes the whole site unavailable. Tell the model your list explicitly: "The Postgres database is required. Redis is a cache and the app works without it. The recommendations API is optional." Without that, it will usually treat every dependency as required.

Also think about a dependency that every instance shares. If the shared database is down, failing readiness everywhere does remove every instance from rotation. That may be what you want, or you may prefer instances to stay in rotation and return a clear error page. Decide on purpose rather than letting generated code decide for you.

Keep checks cheap and bounded

Probes run often. In Kubernetes the default probe period is 10 seconds per probe per pod, and many teams set it shorter. Multiply by replicas and by every load balancer and monitor that also calls in, and an expensive check becomes real load. Rules to apply when reviewing:

  1. Use the cheapest query that proves the connection works. SELECT 1 on a pooled connection, not a count over a large table.
  2. Put a timeout on every dependency call, shorter than the probe timeout. Kubernetes probes have a timeoutSeconds setting that defaults to 1 second. If your database ping can hang for 30 seconds, the probe times out first and you learn nothing about why. A short internal timeout lets you return a clear failure.
  3. Consider caching the result for a few seconds. If several callers hit readiness in a burst, run the real check once and serve the cached answer to the rest.
  4. Never do work with side effects. No writes, no sending messages, no creating test records. A health check should be safe to call a million times.

Here is a readiness handler that follows those rules:

let cached = { ok: false, at: 0 };

async function checkDb() {
  // Bounded: fail fast instead of hanging the probe.
  return Promise.race([
    pool.query("SELECT 1").then(() => true),
    new Promise((resolve) => setTimeout(() => resolve(false), 500)),
  ]).catch(() => false);
}

app.get("/readyz", async (req, res) => {
  const now = Date.now();
  if (now - cached.at > 2000) {
    cached = { ok: await checkDb(), at: now };
  }
  if (cached.ok && !shuttingDown) {
    return res.status(200).type("text/plain").send("ready");
  }
  res.status(503).type("text/plain").send("not ready");
});

Notice that Redis and the optional API do not appear at all. Their health belongs in metrics and alerts, where a human can look at them, not in a signal that controls traffic.

A vital signs monitor showing readings

Watch soft dependencies on dashboards and alerts. Only hard ones should decide where traffic goes. Image: Vital signs monitor display by Petty Officer 1st Class James Stenberg, U.S. Navy, Public domain, via Wikimedia Commons.

Handle startup and shutdown on purpose

Two moments cause most health check surprises, and generated code rarely handles either unless you ask.

Slow startup

If your app takes 40 seconds to load a model, warm a cache, or run migrations, a liveness probe that starts checking right away can kill it before it ever becomes ready, over and over. In Kubernetes, a startup probe solves this: liveness and readiness checks do not run until the startup probe succeeds. Tell the model how long startup really takes, measured, not guessed, and ask it to size the startup probe's failureThreshold times periodSeconds to comfortably cover that.

Graceful shutdown

When a pod is told to stop, it should stop receiving new traffic before it stops serving existing requests. A common pattern is: on SIGTERM, set a shuttingDown flag so readiness starts returning 503, keep serving in-flight requests for a short drain period, then close the server and exit. The readiness handler above already checks that flag. Ask the model to write the SIGTERM handler too, and check that it does not call process.exit() immediately.

Do not leak information

LLMs like to make health endpoints helpful by returning detailed JSON: version numbers, hostnames, dependency connection strings, error messages from the database driver. If the endpoint is reachable from the internet, that is free reconnaissance for an attacker. Keep the public response to a status code and a word. If you want a detailed diagnostics page, put it on a separate route that is only reachable internally or behind authentication, and review it for secrets the same way you would review a log line.

Test the failure paths, not just the happy one

A health check that has only ever returned 200 is untested. Before you trust it:

  • Stop the database in a staging environment and confirm readiness returns 503 within the timeout you set, while liveness keeps returning 200 and nothing restarts.
  • Stop the cache and confirm both endpoints stay 200 and the app still serves pages.
  • Make the database slow, for example with a proxy that adds delay, and confirm the readiness handler returns a failure quickly instead of hanging.
  • Send SIGTERM during a load test and confirm readiness flips to 503 first and in-flight requests finish.
  • Watch a cold start and confirm the startup window is long enough that nothing gets killed while loading.

You can ask the model to write these as automated tests too. For unit tests, have it inject the database check as a function so you can swap in one that fails or hangs. That makes "what happens when Postgres is down" a test you run on every commit instead of a surprise during an incident.

A short review checklist

When an LLM hands you health check code, go through this list:

  1. Are liveness and readiness separate endpoints with separate logic?
  2. Does liveness avoid every network call to another service?
  3. Does readiness include only hard dependencies, and did you decide that list yourself?
  4. Does every dependency call have a timeout shorter than the probe timeout?
  5. Is the check cheap enough to run every few seconds on every instance?
  6. Does it have zero side effects?
  7. Is startup covered by a startup probe or an initial delay sized to real measurements?
  8. Does readiness fail during graceful shutdown?
  9. Does the public response avoid versions, hostnames, and error details?
  10. Have you watched it fail in staging at least once?

Health checks are small, which is exactly why they get generated and merged without much thought. A few minutes spent telling the model who is calling, which dependencies really matter, and what should happen on failure is the difference between a check that protects your service and one that restarts it at the worst possible moment.

Comments

Popular posts from this blog

Grok Bot - a step closer to AGI

Tencent Hy4 Preview: Open 770B MoE Built for Real Work

Structured Outputs and Constrained Decoding for Reliable LLM APIs in Production (September 2026)