You count SMS segments in code by checking whether every character fits the GSM-7 alphabet, then dividing the length by 160 for a single segment or by 153 for each part of a longer message. If even one character falls outside GSM-7, the whole message switches to UCS-2, where the limits are 70 and 67. That's the entire algorithm, and the rest of this post is the edge cases that make a naive body.length / 160 wrong.
Why bother? Because every segment is billed. If your product lets customers type their own messages, a counter in the composer tells them what a send will cost before they press the button, and it keeps your own support queue free of "why did that cost three texts" tickets. If you'd like the non-code version of the rules first, our character limits post covers it.
The rules the counter has to encode
Four numbers drive everything:
| Encoding | Single message | Each part of a longer message |
|---|---|---|
| GSM-7 | 160 characters | 153 characters |
| UCS-2 | 70 characters | 67 characters |
Longer messages are split into parts, and each part carries a small header so the phone can stitch them back together. That header is why a part holds 153 characters instead of 160, or 67 instead of 70. A message that fits in 160 GSM-7 characters is one segment. A 161-character message is two segments, not 1.01, and it's billed as two.
Two details trip people up. First, the encoding is chosen for the whole message, not per character. One emoji in a 300-character text moves all 300 characters to the 67-per-part rule. Second, a few GSM-7 characters are "extension" characters and cost two slots each: the caret, curly braces, backslash, square brackets, tilde, pipe and the euro sign. A message full of JSON-looking text can be longer than it looks.
A counter in about thirty lines
This JavaScript function takes a message body and returns the encoding, the number of characters it uses, and the segment count. It's a sketch to adapt, not a drop-in library.
const BASIC =
"@£$¥èéùìòÇ\nØø\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./0123456789:;<=>?¡" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà";
const EXTENDED = "^{}\\[~]|€";
function countSegments(body) {
let units = 0;
let gsm = true;
for (const ch of body) {
if (BASIC.includes(ch)) units += 1;
else if (EXTENDED.includes(ch)) units += 2;
else { gsm = false; break; }
}
if (gsm) {
const segments = units <= 160 ? 1 : Math.ceil(units / 153);
return { encoding: "GSM-7", units, segments };
}
const utf16 = body.length; // JS length counts UTF-16 units, which is what UCS-2 counts
const segments = utf16 <= 70 ? 1 : Math.ceil(utf16 / 67);
return { encoding: "UCS-2", units: utf16, segments };
} An empty string returns one segment here, so guard against that in your composer if you'd rather show zero.
The emoji and surrogate pair trap
Notice the comment on the UCS-2 branch. JavaScript's length counts UTF-16 code units, and most emoji take two of them. A single thumbs-up is two units, so it eats two of your 70 slots, not one. That's the behavior you want, because UCS-2 on the wire works the same way. Python's len() counts code points instead, so a Python counter needs to encode to UTF-16 and divide the byte length by two. Go and Java have the same split between runes and UTF-16 units, so pick the one that matches the wire.
If you iterate with for (const ch of body) as above, you're walking code points, which is right for the GSM-7 check, since no emoji is in that table. For the UCS-2 count, use body.length. Mixing the two up is the most common bug in hand-rolled counters.
Normalize before you count
Smart quotes, long dashes and ellipses are the usual accidental triggers. They show up when a customer pastes copy from a document, and each one is outside GSM-7. A message that looks plain can quietly flip to UCS-2 and triple in cost.
Ready swaps curly quotes, long dashes and ellipses for plain ones before sending, so on Ready those never change the count. Your counter should apply the same swaps before it measures, or it will over-report and scare people away from sends that would've cost one segment. A small map handles it: curly single and double quotes to straight ones, long dashes to a hyphen, the ellipsis character to three periods, and the non-breaking space to a normal space.
Accented letters are a separate case. A few are in the GSM-7 table (the e with an acute accent, the u with a grave accent), but many aren't, including most capital accented letters and the vowels used in Spanish like the accented a and i. A Spanish message can look single-segment and still be UCS-2. Our post on bilingual segment costs shows what that does to a budget.
Count what is actually sent
Your composer text isn't always the final text. Unless an account turns it off, Ready adds "Reply STOP to opt out" and the business name to the first text to each contact, and again from time to time. A body that's 150 characters in your editor can pass 160 once the footer is added, and become two segments.
So your preview should add the footer length when it applies, or state clearly that it's left out. The same goes for personalization. If the template says Hi {first_name}, count against a long realistic name, not the placeholder, or run the counter on each rendered message when you're costing a campaign.
Or let Ready count it. For a campaign, POST /campaigns/segment-preview estimates segments across the real audience with merge fields filled in and your footer and signature settings applied, free and with nothing saved (see the campaigns docs). Keep your own counter for a live count as someone types.
Turn segments into a cost estimate
Once you have a segment count, the cost is multiplication. Ready's Standard rate is $0.0075 per segment plus a $0.0045 carrier fee per segment, passed through at cost, which makes $0.012 per segment all-in. That all-in figure doesn't include screening, the per-segment charge for the Scrub lists and Number validation switches when an account turns them on.
Ready's all-in prices per segment include the carrier fee but not screening: Scrub lists and Number validation, the two switches in Settings under Compliance, each add $0.003 per segment when switched on, shown as screening on your bill, and both are off by default. Unless you turn the opt-out footer off, the first text to each contact, and one again from time to time, also gets "Reply STOP to opt out" and your business name if the message does not already have them, so a text close to the segment limit can bill one more segment.
Worked example, with assumptions stated: a promo is 175 characters and includes one emoji, and it goes to 5,000 contacts. The emoji forces UCS-2, so 175 characters is 3 segments (175 divided by 67 is 2.6, rounded up). The send is 5,000 × 3 = 15,000 segments, billed at $0.012 each. Remove the emoji and rewrite the copy to 160 characters, and it's 5,000 segments, a third of the bill.
That's the arithmetic worth showing in your product: segments per message, number of recipients, and the total. Accounts opened before November 12, 2026 keep the prices they signed up with, shown under Billing, so read the rate from the account instead of hard-coding it. See the pricing page for the current rate card.
Test the counter, then trust the bill
A counter earns trust with a short table of known cases. These are worth putting in your test suite:
- 160 plain GSM-7 characters: 1 segment.
- 161 plain GSM-7 characters: 2 segments.
- 159 plain characters plus one caret: 161 units, 2 segments.
- 68 plain characters plus one emoji: 70 units, 1 UCS-2 segment. Add one more plain character and it's 2.
- 71 characters including a capital A with an acute accent: UCS-2, 2 segments.
- A pasted curly apostrophe: 1 segment after normalizing.
Your counter is an estimate for previews and budgets. The bill is the source of truth, and the billed segments come back on each send as segments, so check your estimate against them on real sends before you show the number to customers as a promise. When you're ready to send, the request itself is the same short call whatever the length:
POST https://api.tryready.com/v1/messages
X-API-Key: <key>
Idempotency-Key: <uuid>
{"to": "+12125550199", "body": "Your order #1042 has shipped.", "consent_attested": true} The API reference for messages has the full set of fields.
The practical takeaway
Count characters against the GSM-7 table, treat the extension characters as double, fall back to UCS-2 for the whole message when anything else appears, and count UTF-16 units for UCS-2. Normalize quotes and dashes first, add the footer, and render the personalization before you measure. If you'd like to see how the rest of the send works, the SMS API integration guide is a good next step.