A few weeks ago I took over GChat, a multi-tenant WhatsApp AI that answers customers on a business’s own number: FAQs, prices, follow-ups, and a flag when someone is ready to buy. It was already in production, with real businesses depending on it every day.
Inheriting a live system is a different job from building one. You don’t get to start from a clean slate, and the most useful question isn’t “how would I have built this?” It’s “what happens at 2am when this breaks, and would anyone notice?”
This is what I found, what I changed first, and what I deliberately left alone.
What I inherited
Roughly 200K lines of TypeScript on Next.js and PostgreSQL, over 100 SQL migrations, and more than 100 test scripts. Most of the product’s surface area landed in the first few weeks of its history: the AI pipeline, a sales board, promotions, a service and price catalogue, human takeover, and a setup copilot (MIRA) that configures the whole system by chat.
It also ships with a spec and an implementation plan for most larger features. That is rarer than it should be, and it made the first week much easier. I could read why something was built, not just how.
What it got right
Before changing anything, I wrote down what was already good, because that’s the part you don’t want to break.
Bursts become one reply. Customers send five short messages, not one long one. Each chat has a debounce timer (6 seconds by default) that resets on every new message, so the AI answers the whole burst once. A per-conversation lock makes sure a second run queues behind the first and re-reads the database, so nothing that arrived mid-run is missed.
Layers between the model and the customer. The model’s output never goes straight to WhatsApp:
- A sanitizer strips tool calls that leak into the text channel, like raw JSON or a stray tool name at the end of a sentence. If the cleaned reply still smells like internal syntax, it’s withheld and flagged for a human instead of sent.
- A second, tiny model call acts as a judge: is every sentence written to the customer, or is the model talking to itself (“but wait, we must check the persona…”)? A failed verdict withholds the reply. An infrastructure error lets the reply through, because one provider hiccup shouldn’t silence every business at once. Fail closed on content, fail open on infrastructure.
- Prices come only from the business’s catalogue. Anything unlisted is deferred and flagged, never guessed.
Redaction on a copy. Before text reaches the model, sensitive values are masked in an in-memory copy. The stored messages and the transcript staff see are never touched. Cards are Luhn-checked, Malaysian IC numbers have their own pattern, and the placeholders are typed. National IDs, card numbers and secrets are masked by default; phone numbers and emails are an opt-in per business.
The failures I went after first
None of these are about the AI. They’re about the plumbing that decides whether a message arrives, shows up on time, and can be trusted.
1. Messages that never arrive
Webhooks are best-effort. A disconnect or a transient ingest error can drop an inbound message with no record: it’s on the customer’s phone, but never in the inbox.
The fix is a reconciliation sweep that runs every five minutes, re-pulls recent messages for chats that look behind, and inserts whatever the webhook missed. Inserts are idempotent on the business and message ID, so running it twice is harmless. One rule matters more than the rest: recovered messages are stored as visibility-only. They appear in the right place with their real timestamp, but they never trigger an AI reply. A customer who wrote an hour ago shouldn’t get a bot answer out of nowhere. Staff handle those. The sweep also skips a tick if the previous one is still running, so a slow upstream doesn’t pile load onto itself.
2. Messages that arrive, but late
This one came from a support ticket. For a few days, some numbers were one to three days behind. Every message was eventually arriving, so every health check was green, and nobody knew until customers complained.
The existing monitor looked at the newest message time. A backlog trickling in looks perfectly healthy by that measure. So I added a delivery-delay monitor that compares WhatsApp’s own send time against when GChat stored the message:
- Late: enough recent messages arrived, and the median one was ten minutes old or more. Median, not max, so one straggler doesn’t trigger it.
- Silent: nothing sent in the last three hours has arrived, at a time that number normally gets several, judged against the same window on each of the previous seven days. It’s self-calibrating, so quiet numbers and closed hours never trip it.
An episode has to last two checks before anyone is emailed, and the alert goes out once per episode. If three or more numbers on the same server are affected, it’s labelled a server problem rather than three separate ones. GChat can’t fix the cause, but it can make it visible in minutes instead of days.
3. An inbox that polled
The inbox refreshed by polling every few seconds, which is wasteful and still feels slow. I replaced it with live updates: the database announces every chat or message change with NOTIFY, one LISTEN connection per process fans it out, and each open inbox tab holds a server-sent-events stream. The browser reconnects automatically when a stream closes.
Two small changes made it feel right. A staff reply now appears immediately as “sending…” and flips to “sent”, instead of appearing a poll later. And messages show WhatsApp’s send time, not the moment they reached GChat, which matters a lot once a delay has made those two numbers differ.
4. Background jobs that added up
A system like this runs a lot of sweeps: backups, media retries, partner webhook delivery, reconciliation. Each is reasonable alone. Together they were competing for the same database.
I lightened the backup sweep (its interval is now a superadmin setting, 3 minutes by default), gave failed media downloads a retry back-off instead of hammering, and pruned old partner delivery rows. Partner webhooks are now drained per webhook in bounded batches, and a stuck pending backlog expires rather than growing forever. The pattern is simple: every background job needs a bound on how much it does per run and how long it keeps trying.
5. A model with an expiry date
The provider is retiring an older model family on a fixed date. Removing those models from the allowlist wasn’t enough. The model resolver passes a stored override through unvalidated, so any business still pointing at a retired model would have got a 404 on every reply after the cutoff, silently.
So the change shipped with a migration that clears every stored reference, and the price rows stay so past usage is still costed correctly. If your product depends on someone else’s model catalogue, treat each retirement notice like a schema migration.
What I’m keeping on purpose
GChat runs as a single long-running Node process. The debounce timers, the per-conversation locks and the send pacers are all in memory, so there must be exactly one replica. That’s a real limitation, and the first instinct is to rip it out.
I’m not. For this product at this scale, one process is simple to reason about, and the failure mode is covered: on boot, a recovery sweep re-processes anything missed in the last hour, and migrations apply before the server starts serving (with a background retry if the database isn’t up yet during a redeploy). The honest rule is to write down the trigger for revisiting it, a second process or a deployment that can’t tolerate a restart, and not rewrite before then.
The AI also has to know when to stop. This is the part I’d least want to change without a good reason, because it’s what lets a business trust a bot with its customers:
- The model has a
flag_for_reviewtool for when it’s unsure, the customer is unhappy, or something needs a person. Flagged chats land in a review queue, and a business can choose to pause the AI on that chat automatically when it happens. The pause is written to the activity log with its reason. - Classification options can mute the AI. Tag a chat as spam and the AI goes quiet on it, no matter what the customer sends next.
- Each business has its own AI reply window (9:00 to 21:00 in its own timezone by default), so a bot doesn’t answer at 3am unless the owner wants it to.
- Staff can pause the AI on any chat and take over, and their replies skip the AI pacing entirely, because a human typing is already the safe path.
Every one of those is a small switch. Together they’re the difference between an assistant that handles the routine and one that has to be supervised.
What taking over taught me
- Read the incidents before the code. The audit log and support tickets told me which failures were real. The code only told me which ones were possible.
- Make it visible before you make it better. The delay monitor changed nothing about how fast messages arrive. It changed how fast we find out they’re late, and that’s what stopped customers being the alarm.
- ”Healthy” needs a definition. Green checks measured whether something was running, not whether it was working. The monitors worth adding measure the outcome.
- Bound every loop. Every sweep, retry and queue needs a limit on work per run and a point where it gives up.
- Keep the boring, working parts. The debounce, the lock and the layered output checks were solid. Most of my value was in the seams between them, not in rewrites.
There’s more to do, and the next round is about the inbox experience for teams handling hundreds of chats a day. If you’re running WhatsApp for customers, you can see what GChat does at gchat.my.