How to count tokens with tiktoken
0. Install tiktoken
If needed, install tiktoken with pip:

%pip install --upgrade tiktoken

1. Import tiktoken
import tiktoken

2. Load an encoding
Use tiktoken.get_encoding() to load an encoding by name.

The first time this runs, it will require an internet connection to download. Later runs won't need an internet connection.