How to Connect Twilio to an AI Agent (2026)
Twilio's Messages create has no idempotency key, so a retried send is a second, separately billed text. Here's how Swytchcode stores the SID from every attempt and blocks a second send in policy before it ever reaches Twilio.
Key takeaways
- -Twilio's Messages create has no idempotency key. A retry is a second message with its own SID, and its own charge.
- -The SID in the response is the receipt. A timeout is not proof the first text failed to send; look up the SID before you send again.
- -num_segments is how many segments that one body was split into. A longer prompt output can quietly double the cost of every send, before any retry.
- -Swytchcode blocks a second send in policy whenever the recipient and body match a message that already has a stored SID, rather than relying on Twilio to dedupe the call.
An HTTP timeout doesn't mean a message failed to send. It just means the agent didn't hear back in time. Retry on that assumption with Twilio, and a customer can get the same one-time code twice, at two different prices, from a single click of the agent's retry logic.
The model sends a one-time code. The HTTP call times out before the response comes back. The agent posts the same body again. The customer gets two texts, ten seconds apart, with two different codes. They type the first one. Meanwhile the Twilio account now has two separate messages on record, and each one is a charge. The loop never felt expensive from inside the prompt. The bill is per message created.
The general problem is that sending a text isn't a call that can tell a retry apart from a brand-new message, so a timeout plus a retry becomes a second real text, sent and billed. Swytchcode's fix is to make a resend provably impossible once a message has already gone out, by checking what already happened before the agent is ever allowed to send again.
The problem: sending a text isn't a call Twilio can take back
Twilio's send call creates a new message every time it's accepted, with no way to mark a request as a repeat of an earlier one. A second send with the same recipient and the same text isn't recognized as the same request. It's simply a second message, billed again in full. A timeout doesn't tell you whether the first text actually went out, so treating it as a clean failure and sending again is a guess, not a fact.
The cost risk isn't only duplicate sends, either. A text gets split into billable segments based on its length and character set, so an agent that "adds a little context" to a one-time code can quietly push a one-segment text into two segments, doubling the unit cost of every single send before any retry even happens.
How Swytchcode solves it
Swytchcode downloads the Twilio method catalog and lets you enable exactly one send method, not a second "retry send" that's really just the same call with a new body attached. An agent on that setup has one way to send a text, not two ways that happen to look different from inside the prompt.
There's no .env file in this project. Swytchcode keeps the account credentials in its own local store and reads them when a call runs, so they stay in that store rather than anywhere in code. A live command is a request that actually reached Twilio; dry runs don't send and don't create a billable message.
Setting up Twilio with Swytchcode
The Twilio catalog page is the method list:
swy search twilio
swy list methodsEnable one send method, and don't add a second one just to handle retries. That second method is the same POST with a new body attached, and it's exactly the mistake this post is about.
Policies: a retry the agent can't talk its way around
Store four fields from the create response, right next to the attempt itself: the message SID, the recipient and a hash of the body so a duplicate is actually recognizable, the status, and the segment count. If a later attempt has the same recipient and the same body, and a SID already exists for it, stop there. The customer may still be reading the first text, and a second code arriving on top of it ruins the experience even when the second delivery itself fails.
That check belongs in policy, not in the prompt: block a second send whenever the arguments match a body you already allowed and the first call already returned a SID. Approval on Slack or Telegram, available on Business and Enterprise, is the right hold for a broadcast going out to many numbers at once. It expires after 48 hours if nobody answers, and a late approval never sends it. Free and Pro plans can only allow or deny, so on those plans keep the bulk method off entirely rather than relying on an approval step that isn't available yet.
A permissions failure on the destination country is not a cue to try a different sender number in a loop, either. Read the error, fix the permission, and send once. The habit that actually holds up is simple: write the SID down every time, treat a missing response as unknown rather than failed, and send again only once you actually know the first create didn't go through, and even then, send it exactly once.
Twilio's API sends a text by creating a new message resource, and it does that every single time it's asked, with no concept of "this is the same request as before." The risk for an agent isn't that Twilio is unreliable, it's that a timeout looks identical to a failure, and a retry built on that guess turns into a second real text at a second real charge. Swytchcode solves that by allowing exactly one send method, storing the SID and body hash from every attempt, and blocking a second send in policy whenever that same message has already gone out.
Related reading
- Human approval for a tool call. The hold to put on a send that reaches many numbers.
- API compliance for agent integrations. What to tell a reviewer about message content and retention.
- API governance tools for AI agents. Why the send method is the surface worth limiting.
- Best API observability tools for agent traffic. For when the local count of SIDs stops being enough.
FAQ
Does Twilio dedupe two identical SMS creates?
No. The Message create documented for POST .../Messages.json takes no idempotency key at all. Two POSTs produce two Message resources and two SIDs, and you should assume both can be delivered and both can be billed.
I timed out. Did the text send?
You don't know yet. Look up whether a SID was stored. If the create returned a SID before the client gave up waiting, the message exists. If you have no SID at all, check Twilio's message list for that recipient and body before you POST again.
Why did one text cost more than the last one?
Check num_segments in the response. A longer body, or one that no longer fits in a single GSM segment, costs more than one segment's worth. The price for that destination is on Twilio's pricing page and depends on the country. A second create multiplies the whole message, segments included, on top of that.
What does the Twilio create call actually require?
The only required field is To, in E.164 form, or a channel address like whatsapp:+15552229999. You also send a From number on the account, or a messaging service, plus a Body. The response is a Message resource, with a SID that starts with SM for SMS, a status, and num_segments.
How do I check delivery status without risking a duplicate send?
Pass a StatusCallback URL on create. Twilio posts there whenever the status changes, and for most SMS and MMS messages a delivered or undelivered callback is how you actually learn the carrier result. Queued or sent already means the message exists. Wait for the callback, and don't create a replacement just because it hasn't arrived yet.
Can a dry run produce a SID?
No. swy exec with --dry-run never sends the request. It doesn't create a Twilio Message and it doesn't count as a live command. Only a create that Twilio actually accepted returns a SID.
Can I build an AI agent on top of Twilio with Swytchcode?
Yes. swy search twilio finds the provider, swy list methods shows the real method IDs, and swy add method turns on the one send method a task needs. The agent calls exec; it never holds the account credentials itself.
Can Swytchcode stop an agent from sending a duplicate SMS, even if it's asked to?
Yes. A policy rule can block a second send whenever the recipient and body match a message that already has a stored SID, regardless of what the prompt asks for. For broadcasts to many numbers, approval in Slack or Telegram can hold the send until a person signs off, and it fails closed after 48 hours if nobody does.
If an agent retries a Twilio call after a timeout, will Swytchcode let it duplicate a message?
Not if the SID from the first attempt was stored before the retry fires. Twilio itself has no idempotency key on this call (that part is on the caller, not the provider), so the protection here comes from Swytchcode's policy checking the stored SID and body hash before allowing a second send, not from Twilio recognizing the repeat on its own.
Use Swytchcode so this doesn't happen to you
Use Swytchcode so a timed-out Twilio request never turns into a second SMS and a second bill because the agent retried with a brand new POST.
Swytchcode is an execution kernel that sits between your AI agents and the APIs they call. It resolves the right method, injects auth, checks policy, retries safely with idempotency, and logs every call locally.
Start with the MCP quickstart, or browse the docs for the full reference. Free is 100 live commands a month forever; see pricing for Pro and Business.
More content
How to Stop an AI Agent From Booking the Same Meeting Twice
A scheduling agent that checks availability and then books the slot has a gap between those two steps — and a retry can fall straight into it. Here's how Swytchcode closes that gap.
How to Stop an AI Agent From Creating Duplicate Records
A retried "create" call doesn't update an existing contact, ticket, or row — it makes a second one. Here's why that happens across CRMs, databases, and ticketing tools, and how Swytchcode prevents it.
How to Stop an AI Agent From Sending the Same Email Twice
Most email APIs have no idempotency key to catch a repeat send, which makes a duplicate email a harder problem than a duplicate charge. Here's how Swytchcode handles the gap.
