A Baileys bot that answers "got your message" takes thirty lines. A WhatsApp AI agent that people do not notice is an agent takes much more, and most of the extra work is not the model call. This guide walks through the layers, using what we learned building WACrew on Baileys.
New to the library itself? Start with Baileys: how the WhatsApp Web library works, which covers auth state, events, JIDs and a minimal bot.
1. Store everything, not just the last message
An LLM needs history. Persist every messages.upsert event (text, sender, timestamp, quoted message, media path) to your own store. Filter type === "notify" for triggering replies, but still store history-sync messages for context. WACrew writes one JSONL file per chat plus a readable log, which turns out to be easy for agents to read too.
sock.ev.on("messages.upsert", async ({ type, messages }) => {
for (const m of messages) await store.append(m.key.remoteJid!, normalize(m));
if (type !== "notify") return;
for (const m of messages) if (!m.key.fromMe) queue.touch(m.key.remoteJid!);
});
2. Decide who to answer first
Do not reply inside the event handler. Put chats in a queue ordered by how long the other person has waited, and collapse back-to-back messages into one block. People send three messages in a row; answering each one separately is the first tell of a bot.
3. Turn media into text
Download voice notes and transcribe them (Whisper works well for Portuguese, Spanish and English). Images and screenshots can go straight to a multimodal model. Without this, the agent answers around the most important part of the conversation. See voice notes in MCP servers.
4. Write like the owner of the number
Measure how the owner writes in that chat: typical length, punctuation, greetings, emoji. Give the model those measurements and the last messages, not a generic persona prompt. Then review the draft in code before sending: block colon-led intros, em dashes, apologies repeated across messages, the same opening as last time, a greeting that does not match the time of day. WACrew's generic rules are published as botsmell.
5. Send like a person
await sock.readMessages([lastKey]);
await sock.sendPresenceUpdate("composing", jid);
await sleep(typingTimeFor(text));
await sock.sendMessage(jid, { text });
await sock.sendPresenceUpdate("paused", jid);
Add a delay before starting that depends on the conversation (seconds in a live chat, longer in a cold one), split long answers into bubbles, and cancel if a new message arrives or the owner replies from the phone. Return a ticket so the caller can check or cancel delivery.
6. Keep a human in the loop
Default every chat to a confirm mode where the draft is shown to the owner first. Turn on autonomous mode per chat, never globally. If someone asks whether they are talking to an AI, do not lie: stop and hand the chat back.
7. Expose it over MCP
Once the store, queue, transcription and delivery exist, an MCP server is a thin layer: tools to list chats, read, search, transcribe, prepare a reply, send with a ticket and cancel. That lets Claude Code, Codex or Cursor drive the same agent. WACrew's 36 tools are listed in the tools reference.
Or skip the build
All of the above is open source in WACrew (Apache 2.0). You can use it as is, or read src/whatsapp/ to see Baileys isolated behind ports and adapters.
FAQ
Which Baileys package should I install? Follow the current instructions in the Baileys README; the package and version naming has changed over time.
Can I run the LLM locally? Yes. The agent loop does not care where the model runs, as long as latency fits the human-paced delivery.
Will my number get banned? It can, with any unofficial client. Answering people who wrote to you, at a human pace, is the lowest-risk use. Bulk and cold messages are what get flagged.



