r/AI_Agents • • 24d ago

Discussion Building a WhatsApp bot using code generated by Claude, but "if/else" logic breaks when users go off-script. How to fix?

Hey everyone,

I’m currently writing code to build my third WhatsApp bot for a client. I don't have a formal coding background. Everything I know comes from YouTube, and I’ve been using Claude to help me write the actual code base.

I’ve hit a major bottleneck: I’ve realized I’m building a highly deterministic, rigid code-based bot rather than a flexible agent. Because of this, I’m spending hours debugging how it interacts with real users.

Currently, my code relies on a strict sequence. If a customer deviates even a little bit from the expected path, the logic breaks and my code defaults to sending an admin takeover notification.

For example, when a client wants to book an appointment, the bot queries the database and lists 4 to 5 available time slots. But if the user asks a pricing question mid-booking, or taps a wrong menu button instead of picking a slot, the sequential code doesn't know how to handle the tangent. It completely loses context.

How do developers handle conversational context and state management in code so the bot can answer random user questions mid-flow, remember what it was doing, and gracefully steer the user back to the main goal (like booking)?

Any architectural advice, libraries, or logic patterns I should ask Claude to implement would be incredibly helpful!

4 Upvotes

18 comments sorted by

1

u/Extra-Ad358 24d ago

Don't script the conversation, script the data you need. Let the LLM classify intent and extract fields (name, date, service) into JSON each turn, and let your code decide what's still missing and when to hand off to a human. Off-script messages stop breaking things because there's no script to fall off.

1

u/Ok-Zombie-9595 24d ago

Yeah, thats the issue. The agent that i build hands over to a human the moment it doesnt understand. because human conversation is not a straight line path for example someone would start a convo with sending multiple response to deliver a single message. For e.g.:

Patient

  1. "hi"
  2. "when is the earliest slot"
  3. "?"

This is also an issue i'm facing. because the agent currently doesnt understand the context of the response and replies to each message.

1

u/London-Boat 24d ago

one thing that helps a lot is keeping the booking state outside the chat, like a small record with the service, date, chosen slot and which step you're on. when a side question comes in, answer it from that record or a faq, then re-send the pending step with the slots again so the user can just tap one. also save the slot list you offered so a wrong button tap can be handled by re-showing it instead of defaulting to an admin handoff. is this for one client with a fixed set of services or does each client have their own?

1

u/Ok-Zombie-9595 24d ago

I already save the times I offered. If they type something off-script, it shows the list again once, then hands the chat to the admin. Do you keep showing the list every time, or is there a point where you hand over?

1

u/London-Boat 24d ago

showing it once then handing over sounds right. the one exception i'd make is if they reply with something that looks like an attempt at a time, like "4ish" or "after lunch", since that's them trying rather than going off script. a second try with the closest slots is worth it there. past that a human sorts it faster than the bot guessing

1

u/_haiderfarooq 24d ago

agree with the others on keeping state outside the chat. the bit that made it click for me: ur code owns the state, the model only classifies. each turn send the message plus the current step and ask for a tiny json back, intent (continue, side_question, restart, human) plus any fields it pulled out. side_question = answer from a faq then resend the pending step with the same slots. store the slots u offered with ids so "the 3pm one" resolves. what model are u calling rn?

1

u/Ok-Zombie-9595 24d ago

Gemini 2.5 Flash through OpenRouter, with GLM 5.3 as a backup if it fails. It already sends back JSON with the intent and the fields, pretty close to what you describe. Your four intents are a lot simpler than mine though.

1

u/_haiderfarooq 24d ago

nice, flash is plenty for that. saw ur other comment about people sending hi then the question then ? as 3 msgs, id debounce it, wait like 5-8 sec after the last message and send them all to the model as one turn, that alone stops a lot of early handovers. how many intents u got rn, and does it hand over on low confidence or on any unknown intent?

1

u/AutoModerator 24d ago

Thank you for your submission, for any questions regarding AI, please check out our wiki at https://www.reddit.com/r/ai_agents/wiki (this is currently in test and we are actively adding to the wiki)

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/stealthagents 21d ago

That's a classic issue with bot development. One way to tackle this is by implementing a state management system that keeps track of the user's context throughout the conversation. You might also consider using a more flexible framework or adding some NLP capabilities to help the bot understand off-script questions and respond accordingly instead of defaulting to admin alerts.

1

u/[deleted] 23d ago

[removed] — view removed comment

1

u/Ok-Zombie-9595 22d ago

yeah i've been getting of answers to look into state machines. how does it work and how can i build it?

0

u/[deleted] 24d ago

[removed] — view removed comment

1

u/Ok-Zombie-9595 24d ago

so what would be your step by step process to build a whatsapp agent for a service based business? i'm not sure how to build it but what i would do is, i would get the client to export all past conversation, get claude to read it and analyse it. Then build based how FAQs are answered, booking logic, matching with therapist availability etc. Not sure if this is the right way to do it?