How to set up an AI agent on WhatsApp
Cloud API, 24-hour window, templates, message competition and human handoff: what changes when the channel is WhatsApp.
Setting up an agent on WhatsApp is 30% AI and 70% messaging platform. Agent modeling is the same as any other channel; What changes are the rules of Meta's Cloud API, the real behavior of those who send five audios in a row and the need to return the conversation to a human without breaking the context.
Executive summary
- Official Cloud API avoids banning and releases templates — BSP and unofficial gateways are a business risk.
- The 24-hour window defines what you can send and how much it costs.
- Message debounce is mandatory: the user writes in bursts.
- Human handoff is part of the product, it is not plan B.
1. Choose the access path to WhatsApp
There are three paths: Cloud API directly with Meta, a BSP (official provider) or unofficial libraries that automate WhatsApp Web. The third is tempting because it is free and fast, and is exactly what brings down the company's number. Do not use in serious operation.
Direct Cloud API gives lower cost and full control, requiring you to take care of business verification, webhooks and infrastructure. BSP solves onboarding and support with a markup per conversation. For those who already have a technical team, direct Cloud API is the standard.
2. Webhook: receive without losing messages
Meta delivers events via HTTPS webhook and resends when you don't respond quickly. The handler needs to validate the signature, respond 200 immediately and process in queue. Processing synchronously generates timeout, redelivery and duplicate response to the customer.
Keep the message identifier and discard duplicates: delivery is at least once, not exactly once.
export const POST = async ({ request }) => {
const raw = await request.text();
if (!verificarAssinatura(raw, request.headers.get("x-hub-signature-256"))) {
return new Response("assinatura inválida", { status: 401 });
}
const evento = JSON.parse(raw);
const msg = evento.entry?.[0]?.changes?.[0]?.value?.messages?.[0];
if (msg && !(await jaProcessada(msg.id))) {
await fila.enfileirar({ id: msg.id, de: msg.from, tipo: msg.type, msg });
}
return new Response("ok"); // sempre 200, sempre rápido
};3. Debounce: people write in bursts
On WhatsApp, the user sends "hi", then "I wanted to know", then "what's your plan" in three messages. Responding to each one generates three random responses and triples the cost.
The solution is a grouping window of 3 to 8 seconds per contact: it accumulates, resets the counter with each new message and only then calls the agent with the entire block. Along with this, a typing indicator and a lock per conversation to prevent two simultaneous executions of the same contact.
4. 24-hour window, templates and cost
After a message from the customer, you have 24 hours to respond freely. Outside this window, only previously approved templates. This changes the design of the product: follow-up, reminder and conversation resumption require a registered template and correct category.
Meta is charged per conversation initiated, varying by category — service, utility, marketing and authentication. Add this to the cost of agent tokens and you arrive at the real cost per service, which is the number to compare with the cost of the human agent.
- Templates: submit early, approval takes hours to days and disapproval is common for promotional text.
- Never launch cold list marketing — it’s the quickest path to low quality and restricted numbers.
- Record the time of the contact's last message to know, at any time, whether the window is open.
5. Audio, image and document
In Brazil, audio is the norm. The agent needs to download the API-authenticated media, transcribe it, and treat the text as normal input. Images are usually proof, error prints or product photos — it's worth running a view only when the flow requires it, because it costs more.
Store the media on your infrastructure: Meta URLs expire, and you will need the file in your service history.
6. Human handoff without losing context
Every WhatsApp agent needs an exit door: explicit customer request, sensitive topic (legal, financial, serious complaint), low trust or repeat failure. During handoff, the agent stops responding to that contact, marks the conversation as human and delivers a summary, collected data and history to the agent.
Also define the return path: who returns the conversation to the agent, when and what the agent can already assume as known.
7. Integration with CRM and internal systems
The value is not in the conversation, it is in what it records. Each service must produce identified contact, qualified fields, funnel stage and event in CRM. The same tool rules apply here: scope per tenant on the server, idempotence in writing and auditable log.
This is what transforms the agent from a “nice chatbot” into a channel with a conversion number comparable to that of the sales team.
8. Metrics that matter
Track resolution rate without human, time to first response, handoff rate, cost per resolved conversation and conversion to meeting or sale. "Messages sent" metric says nothing.
Also monitor the quality of the number on the Meta platform: a drop in quality precedes the sending limit, and the sending limit brings down the operation.
Frequently asked questions
+Do I need a new number?
Recommended. A number already used on regular WhatsApp needs to be migrated and loses its history in the app. Number dedicated to Cloud API avoids conflict between manual and automated service.
+Does the customer realize it is AI?
Realize when the agent is shallow. With access to real data and well-done handoff, the experience is usually better than the human queue. Being transparent about being an assistant is good practice and reduces friction.
+What does it cost per month?
Add three lines: Meta conversation rate, model tokens, and infrastructure. In medium volume operations, the cost per resolved conversation is usually well below the cost of the same service provided per person.
+Can you use the same number for the human team?
Yes, with a shared inbox linked to the Cloud API. The agent answers first and the attendant takes over the conversation when the handoff triggers, on the same wire.
Want to discuss this architecture for your product? Get 30 minutes with our technical team, free of charge.
Talk to an engineerRelated solutions

































