Wow!Full-featured sandbox for developers — free forever!Register Now
LangChain WhatsApp Tutorial

How to Build a WhatsApp AI Agent with LangChain and Whapi.Cloud: A Python Tutorial

Updated on June 1, 2026

This Whapi.Cloud tutorial shows Python developers how to build a WhatsApp AI agent with LangChain and LangGraph on a single hosted webhook. You wire the receive, reason and reply loop, then give each contact its own memory by setting the LangGraph thread_id to the sender's phone number. Replies go back as free-form text through the Whapi.Cloud API, with no local tunnel and no Meta business verification to set up first. It is written for backend developers comfortable with FastAPI and pip.

Build a WhatsApp AI agent with LangChain, LangGraph and Whapi.Cloud in Python
About the Author
Jason Mitchell
Jason Mitchell
Product Owner at Whapi.Cloud
Building WhatsApp integrations since 2019. Always happy to connect — whether you want to discuss an API use case, share feedback, or just talk shop. Find me on LinkedIn.
Frequently Asked Questions

LangChain WhatsApp Agent Questions

No. ngrok is only needed when a tunnel must expose a local machine. With Whapi.Cloud you connect a number by QR code and set a hosted webhook URL in the channel settings, so inbound messages POST straight to your deployed FastAPI app. The tunnel step that most tutorials require disappears entirely.
On the official Business API, messages outside a 24-hour window must be pre-approved templates, and Meta bills per delivered template. With Whapi.Cloud the agent sends a free-form text reply through <code>POST /messages/text</code>, so there is no template approval step and no per-template fee to manage.
Deploy the FastAPI app to a public host, set your token and model key as environment variables, and replace MemorySaver with a Postgres checkpointer. The phone-as-thread_id logic stays the same, but conversation state now survives restarts and deploys instead of resetting in memory.
Attach a checkpointer to the agent and pass <code>thread_id</code> equal to the sender's phone number on every invoke call. LangGraph then stores and restores a separate conversation per thread, so each contact keeps their own context. Without a checkpointer, all users share one state and conversations bleed together.
The webhook handler is blocking on the LLM call before returning. When the response is slow, the delivery times out and the message is re-sent, so the agent answers twice. Return HTTP 200 immediately and run the agent in a background task, then send the reply with a separate API call.
You decorated a class method with <code>@tool</code>, so LangChain reads <code>self</code> as a required tool argument and the agent build fails. Define each tool as a module-level function and pass any shared state through a closure or a global client instead of <code>self</code>.

See What Our Clients Built
with Whapi.Cloud

"Cart reminders with a 5% follow-up coupon lifted our recovery rate from 4% to 11%. Customers reply directly in WhatsApp — our team closes the sale right there."

Abandoned Cart Recovery

Hans M., Germany
"Managing 40+ segment groups became trivial — auto welcome messages, pinned updates, inactive member cleanup. Lead gen from WhatsApp groups grew 3x in two months."

Automated Group Management at Scale

Carlos S., Brazil
"Guests receive door codes, WiFi credentials, and a local guide automatically on arrival. Checkout is confirmed via a photo on WhatsApp. Front desk load dropped 40% in the first month."

Contactless Hotel Operations

Ana M., Romania
"Our deals channel has 12,000 subscribers. Whapi.Cloud scrapes competitors, filters duplicates, and auto-posts the top 5 daily. Channel growth tripled after switching to automated posting."

Automated Deal Channel Publishing

Katrin S., Germany
"We verified 93,000 active WhatsApp numbers from 180,000 contacts in 48 hours. Campaign open rates improved significantly by stopping spend on inactive numbers."

Large-Scale Audience Filtering

Sergio N., Spain
"Patients book appointments and check lab results on WhatsApp. The bot handles 200+ daily queries without staff. Appointment no-shows dropped 30% after automated 24h reminders."

Healthcare Bot — Scheduling & Results

Dr. Fernanda O., Brazil
"Post-purchase WhatsApp messages with a tailored discount at day 14. Birthday coupons see 45% redemption — far above our email rate. Repeat purchases via WhatsApp: 18% of total revenue."

WhatsApp Retention Campaigns

Lukas W., Germany
"Customers get a WhatsApp tracking link the moment their parcel ships. Support tickets dropped 35% in 3 months — mostly 'where is my order?' queries simply disappeared."

Automated Shipping Notifications

Matei P., Romania
Inhouse Developed & Managed

What is Whapi.Cloud?

Whapi.Cloud is an intuitive API that connects your business with WhatsApp -- directly and without complexity. Build support bots, schedule appointments, send notifications, manage groups and channels, automate order confirmations, and track everything with webhooks. Focus on growing your business while the API handles the messaging layer.

Our service provides full control and management of WhatsApp groups, communities and channels.

Add dynamics and new features: media, buttons, reactions, stories, orders and products. All of these are available to you for customer interaction.

Our care team will respond quickly and help you with any questions you may have!
Account screenshot
Account screenshot