Blog
EngineeringFor engineers

Designing a chatbot when every message costs money

From 1 October 2026, WhatsApp bills us for replies past a monthly allowance. How that changed the way our bot talks, when it chooses to say nothing, and the tests that stop a stray message creeping back in.

Lodge Internet · 14 November 2026 · 6 min read

On 1 October 2026, WhatsApp started billing us for every delivered reply past the first 1,000 each month. A single reply is cheap. But a bot that answers every "ok", every "thanks" and every repeated tap with a fresh menu is spending money to repeat itself.

Our engineering standards have a rule for it, P9: bot messages cost money, and a new one has to justify itself. Fold text into an existing message or list before adding a new one. This post covers what that rule turned into in bot/.

Count first

Before the change, nothing in the bot counted what it sent. You can't manage a budget you can't see, so that came first.

Every message WhatsApp accepts is counted twice, once against the day and once against the month, both in Lagos time so the day rolls over at midnight where our customers are. Once the month's count passes a threshold set below the allowance, every send from then on logs a warning. That way the allowance running out shows up in the logs before it shows up on the bill.

Counting happens in the background, after the send:

# bot/app/whatsapp/client.py, after WhatsApp accepts a message
task = asyncio.create_task(_count_sent(settings.monthly_message_warn))
_background.add(task)
task.add_done_callback(_background.discard)
return True

The first version waited for the count. That put Redis round-trips over the internet between one message and the next one the customer was waiting for. Bookkeeping should never be what a customer waits on, so there's a test for that too.

Fold, don't add

Most of the savings came from merging messages that used to travel separately.

  • Signing in. A correct sign-in code gets "You're in" at the top of the menu message, not as a message of its own.
  • The dashboard. _show_dashboard takes an intro that goes at the top of the same message: a greeting, a fresh balance, a notice. The wallet balance rides on it too, and is left out if the wallet can't be read.
  • A payment landing. The code and a hint for getting back to the menu go in one message. The dashboard used to follow as a second one, but by then the customer already has what they came for.
  • Signing up. The note that signing up means agreeing to our terms rides on the message that asks for your email.
  • A sold-out plan. "That plan just sold out" and the updated plan list arrive together.
  • No "please wait". Asking for a sign-in code sends the question for the code and nothing before it.

None of these needed new screens. They needed every reply to be built as one string, with the extras appended, before anything is sent.

Know when to say nothing

The harder part is silence. People repeat themselves when they're waiting, especially when money is involved, and every repeat used to get a reply.

Text we can't act on. A signed-in customer who types something the bot doesn't understand gets the dashboard once. If they keep typing within a quiet window, the bot stays silent, because the dashboard is already in the chat. A tap on a button is different: it is always answered, because a tap is a deliberate choice rather than chatter. WhatsApp keeps old messages tappable forever, so taps from days ago arrive too, and they get answered.

"I've paid", again. On the payment screen, a customer tapping "I've paid" while the transfer is still on its way gets one "not yet" per quiet window. The check itself still runs every time. Only the reply is skipped. And the moment the payment actually lands, it is announced, whatever the window says:

# Still pending. Asked again inside the quiet window, say nothing: the
# answer hasn't changed, and the backend announces the payment itself.
if session.channel == WHATSAPP:
    if session.last_paid_reply_at and now - session.last_paid_reply_at < quiet:
        return

"We're offline." When the service is paused for maintenance, the bot tells each person once a day, then stops answering until it's back. A customer waiting out a pause tends to keep messaging, and every one of those replies used to be billed.

Announcing a payment twice. A transfer can be confirmed by the backend's push and by the customer's own check at almost the same moment. A delivery claim means only one of them announces it.

There's one deliberate exception to all of the silence. If Redis can't be reached, the once-a-day notices are sent anyway. One extra message beats a customer hearing nothing at all.

A daily allowance on balance checks, on one app only

Checking how much data is left on a code is the most repeated thing people do, and a balance barely moves within a day. Each check also costs a round trip to the hostel's Wi-Fi controller and a few billed messages.

So the backend gives each account a small daily allowance of balance results, on WhatsApp. It counts against the account, not the phone, so a second phone doesn't reset it. Only a check that returns a balance uses it up. A mistyped code, a code that isn't yours, or our own lookup failing costs you nothing.

When the allowance is used up, the bot says so once that day, and the message carries your last balance so it still tells you something. After that, further taps get no reply. The test for it reads: "Even an instant, cached answer is a billed message; some people tap over and over."

The same bot also runs on Telegram, where messages cost us nothing. There, the allowance doesn't apply, and none of the quiet rules do either. The bot tells the backend which app a request came from, and the backend sets the limit accordingly.

That gives us something to offer instead of a "no". On WhatsApp, a few messages that are going out anyway carry a line inviting you to Telegram for unlimited balance checks: the dashboard, the payment confirmation, and the daily-limit reply. telegram_invite adds nothing on Telegram itself, and its docstring states the rule plainly: the invitation must never cost a message of its own.

Tests that count messages

All of this is easy to undo by accident. Someone adds a friendly "Checking..." before a slow call, and the bill goes up with no failing test anywhere.

bot/tests/test_message_budget.py exists to make that fail. It replaces the WhatsApp client with one that records every message of every kind, runs a flow, and asserts how many came out:

await handler_mod._handle_login_code(session, _text("123456"), client, backend, store)

assert [kind for kind, _ in client.sent] == ["list"]
assert "You're in, Ada" in client.sent[0][1]

There are 19 tests in that file today. They pin the counts after the trim of 21 October 2026: one message for a balance, one for a sign-in, one for a resend, none for a silent token refresh. They also pin the silences: repeated unknown text gets one reply, a tap is always answered, a payment that lands is always announced, and a Redis outage never silences a notice or breaks the counter.

The trade-offs

Silence can look like a broken bot. A customer who types into a quiet window hears nothing, and may reasonably wonder whether we're still there. We lean on two things to soften that: the menu they need is already in the chat, and any tap gets an answer straight away. Messages that leave someone without a live list end with the one typed word that works from anywhere, startover.

Every quiet window is a guess about how often people repeat themselves. The windows are settings, so we can change them without a deploy, but they are still guesses.

The two apps now behave differently on purpose. That's more to test and more to explain, and it's the price of not making Telegram customers live with a WhatsApp constraint.

What's still open

We count messages WhatsApp accepted. Meta bills messages delivered. The two are close, but our count is an estimate of the bill, not the bill.

A new flow is only covered if someone adds it to the message-budget tests. Nothing yet fails when a flow has no count pinned at all.

For the customer's side of the quiet rules, see Why our bot sometimes goes quiet.

Last checked against the product on 14 November 2026.

More in Engineering