AI agents · WhatsApp · Tool calling · E-commerce
Agentic workflows for WhatsApp commerce
How to design a WhatsApp AI agent for online sellers: webhook, intent routing, tools for catalog and order lookup, and a clean human handoff.
By Ahmed Salah Fouda · · 6 min read
TL;DR
A WhatsApp commerce agent works best as a small set of tools — catalog search, order status, and human handoff — behind one LLM that decides which tool to call. The webhook stays thin, every tool is a normal function you can test, and anything risky (refunds, address changes) goes to a human.
The shape of the system
- WhatsApp Cloud API sends each incoming message to your webhook.
- The agent reads the message plus recent history and chooses a tool.
- Tools are plain functions: search_catalog, get_order_status, handoff_to_human.
- The reply goes back through the WhatsApp messages endpoint.
A thin webhook
from fastapi import FastAPI, Request, Query
from fastapi.responses import PlainTextResponse
import os
app = FastAPI()
VERIFY_TOKEN = os.environ["WA_VERIFY_TOKEN"]
@app.get("/webhook")
def verify(mode: str = Query(alias="hub.mode"),
token: str = Query(alias="hub.verify_token"),
challenge: str = Query(alias="hub.challenge")):
if mode == "subscribe" and token == VERIFY_TOKEN:
return PlainTextResponse(challenge)
return PlainTextResponse("forbidden", status_code=403)
@app.post("/webhook")
async def incoming(req: Request):
body = await req.json()
value = body["entry"][0]["changes"][0]["value"]
for msg in value.get("messages", []):
if msg["type"] == "text":
await handle(msg["from"], msg["text"]["body"])
return {"ok": True}Tools the model can call
TOOLS = [
{"type": "function", "function": {
"name": "get_order_status",
"description": "Look up an order by its number for this customer.",
"parameters": {"type": "object",
"properties": {"order_id": {"type": "string"}},
"required": ["order_id"]}}},
{"type": "function", "function": {
"name": "search_catalog",
"description": "Find products by name, size or color.",
"parameters": {"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"]}}},
{"type": "function", "function": {
"name": "handoff_to_human",
"description": "Use for complaints, refunds, address changes or anything unclear.",
"parameters": {"type": "object",
"properties": {"reason": {"type": "string"}},
"required": ["reason"]}}},
]The agent loop
import json
from openai import OpenAI
client = OpenAI()
async def handle(phone: str, text: str):
messages = [{"role": "system", "content": SYSTEM_PROMPT},
*load_history(phone),
{"role": "user", "content": text}]
for _ in range(4): # hard cap on tool steps
res = client.chat.completions.create(
model="gpt-4o-mini", messages=messages, tools=TOOLS)
msg = res.choices[0].message
if not msg.tool_calls:
return await send_whatsapp(phone, msg.content)
messages.append(msg)
for call in msg.tool_calls:
result = run_tool(call.function.name,
json.loads(call.function.arguments), phone)
messages.append({"role": "tool", "tool_call_id": call.id,
"content": json.dumps(result, ensure_ascii=False)})
await send_whatsapp(phone, "I'll connect you with a team member.")Rules that keep it safe
- Scope every tool to the sender’s phone number — the model never chooses whose order to read.
- Cap the number of tool steps per message.
- Route money and address changes to a human by design, not by prompt.
- Reply in the customer’s language and keep messages short — it’s a chat, not an email.
Need this built for your business?
Start a Project