fix(0.6.1): direction- and country-aware Twilio telephony billing#97
Open
nicolotognoni wants to merge 1 commit into
Open
fix(0.6.1): direction- and country-aware Twilio telephony billing#97nicolotognoni wants to merge 1 commit into
nicolotognoni wants to merge 1 commit into
Conversation
Default twilio telephony billing was a flat $0.0085/min — the US inbound local rate. Correct for the 99% case of an agent receiving calls on a US local number, but under-estimated outbound by 9-40x for international mobile destinations (US → IT mobile is $0.3473/min, ~40x the default). On a mixed-traffic dashboard the cost.telephony rollup could swing total margin reporting by 10x or more. Add a per-country, per-direction, per-line-type matrix sourced from Twilio's public pricing pages (verified 2026-05-12), plus a stateless E.164 → ISO-2 parser (no new deps, tiny hand-rolled country code map). calculateTelephonyCost / calculate_telephony_cost gain optional direction, destCountry, destType arguments. CallMetricsAccumulator gains a setTelephonyContext / set_telephony_context setter the carrier handler can call once direction and remote number are known. Backward compatibility is exact: omitting all new arguments keeps the legacy code path billing at pricing.twilio.price as before. Sources (all verified 2026-05-12, US-account perspective): - https://www.twilio.com/en-us/voice/pricing/us - https://www.twilio.com/en-us/voice/pricing/{it,gb,de,fr,es,nl,br,mx,in,jp,au,ca} Operators with negotiated Twilio rates can override the entire matrix via pricing.twilio_outbound_matrix. Parity: Python ↔ TypeScript matrices, country codes, and API shape are identical. 65 Python + 57 TS pricing tests cover the matrix, parser, override surface, and backward-compat fallback. Full suites green (1858 Py + 1533 TS).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
twiliotelephony billing was a flat$0.0085/min(US inbound local) — under-estimates outbound by 9-40x for international mobile destinations (e.g. US → IT mobile is$0.3473/min, ~40x the default).pricing.twilio.priceas before.Implementation
libraries/typescript/src/services/telephony-pricing-matrix.tslibraries/python/getpatter/services/telephony_pricing_matrix.pycalculateTelephonyCost/calculate_telephony_costgain optionaldirection,destCountry/dest_country,destType/dest_typeparameters. Detection oftwilioprovider + presence ofdirection+destCountryswitches from flat rate to matrix lookup.CallMetricsAccumulatorgainssetTelephonyContext/set_telephony_contextso the carrier handler (Twilio/Telnyx adapter) populates the context once direction and remote E.164 number are known.destTypedefaults tomobile(conservative — international mobile rates universally exceed landline rates; under-billing on a margin dashboard is worse than over-billing).pricing.twilio_outbound_matrix(free-form dict, no SDK fork needed).pricing.ts/.py,metrics.ts/.py,index.ts/__init__.py,tests/pricing.test.ts,tests/test_pricing.py,CHANGELOG.md, plus the two new matrix modules.Pricing matrix (Twilio US-account, USD/min, verified 2026-05-12)
Sources:
https://www.twilio.com/en-us/voice/pricing/usand per-destination pages athttps://www.twilio.com/en-us/voice/pricing/<iso2>.Breaking change?
No. All new parameters are optional with safe defaults; existing callers bill identically to before. Public surface adds a new context object and a new metrics setter — both opt-in. Parity holds across Python and TypeScript: identical matrix, identical country-code map, identical API shape (
snake_case↔camelCase).Test plan
pytest tests/test_pricing.py -v— 65 pass (17 new: matrix lookup, parser, override surface, backward-compat fallback)npx vitest run tests/pricing.test.ts— 57 pass (17 new mirror Python)pytest tests/ -m "not soak" -q— 1858 pass, 7 skipped (pre-existing)npx vitest run— 1533 pass / 85 filesnpm run lint— clean (tsc --noEmit)npm run build— clean (tsup esm + cjs + types, 320 KB d.ts)Docs updates
CHANGELOG.md—## Unreleasedentry under### Changedwith source URL and override syntax.