Count UTF-8 bytes in any Unicode string using the browser TextEncoder API. Unlike character count, byte length reflects how much space text occupies on the wire—critical when agents assemble HTTP bodies, S3 uploads, or log payloads with hard size caps.
When to use it
- Verify a prompt fits an API request size limit
- Compare storage footprint of JSON vs plain text exports
- Debug encoding issues where character count and byte count diverge
How it works
Each character is encoded as UTF-8 bytes; ASCII uses one byte per character while many emoji and accented letters use two to four bytes.
Limitations
Returns zero for an empty string. Does not estimate tokens—pair with the token estimator when budgeting LLM context.
Example
The character é encodes as two UTF-8 bytes while hello uses five bytes total.