TwilioJesse SumrakTutorial
How to build an AI agent that checks order status and processes refunds
Summary
Build an AI agent that handles order status and refunds by defining four custom tools—get order, check eligibility, initiate refund, update ticket—using Twilio Agent Connect’s function tool decorator to call existing APIs. The agent verifies identity with Twilio Verify’s one-time passcode before any refund, enforced in both prompt and code to prevent prompt injection bypass. Refund decisions are grounded in the company’s return policy from Enterprise Knowledge, while hard rules like return windows are enforced in eligibility tool code to avoid hallucinations. High-value or repeat requests escalate to a human in Twilio Flex via Agent Connect’s handoff tool, which packages conversation ID and reason with an AI-generated summary. The same agent works across voice, SMS, WhatsApp, RCS, and chat through a single handler, with ConversationRelay providing median latency under 0.5 second and Conversation Memory recording outcomes for future context.
Approach / What changed
Define four custom function tools (get order status, check refund eligibility, initiate refund, update ticket) via Twilio Agent Connect’s @function_tool decorator. Add Twilio Verify one-time passcode before refund. Load return policy into Enterprise Knowledge base for grounding. Enforce hard rules in tool code. Set auto-approval thresholds and escalate high-value/repeat requests via Agent Connect handoff to Twilio Flex. Use a single message handler for all channels with ConversationRelay for voice.
Takeaways
The agent must verify customer identity via one-time passcode (Twilio Verify) before processing refunds, enforced both in prompt and in refund tool code to prevent prompt injection.
Policy grounding alone is insufficient; hard rules like return windows must be enforced directly in eligibility tool code to prevent AI hallucinations from issuing incorrect refunds.
Start deployment with read-only order status before enabling refunds, and set conservative auto-approval thresholds with weekly review of escalated cases to build trust.
FAQ
How do you stop an AI agent from issuing fraudulent refunds?
Use Twilio Verify to send a one-time passcode to the customer's phone or email on file before any refund. Pair this with eligibility rules enforced in code and approval limits that escalate high-value or repeat requests to a human.
Can the same AI agent handle order questions over phone and SMS?
Yes. Twilio Agent Connect routes all channels through a single message handler, so one agent with one set of tools covers voice, SMS, WhatsApp, RCS, and chat. ConversationRelay handles speech for phone calls with sub-second latency.
What happens when an AI refund agent can't resolve a request?
Agent Connect's built-in handoff tool transfers the conversation to a human in Flex with the conversation ID and escalation reason. With Conversation Intelligence, the human agent receives an AI-generated summary, and the customer stays on the same call or chat.
Source and freshness
- Source
- twilio.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.