Is there a way to count tokens?

What’s the best way to truncate text? Thanks!