#Count tokens
1 messages · Page 1 of 1 (latest)
Hi @sweet geyser. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
I'm sorry, but the knowledge sources provided do not contain information on how to count tokens in a TypeScript environment. The sources mainly discuss token counting in Python using OpenAI's tiktoken library.
For Python, you can use the tiktoken library as follows:
import tiktoken
def num_tokens_from_string(string: str, encoding_name: str) -> int:
"""Returns the number of tokens in a text string."""
encoding = tiktoken.get_encoding(encoding_name)
num_tokens = len(encoding.encode(string))
return num_tokens
num_tokens_from_string("tiktoken is great!", "cl100k_base")
(Source: OpenAI Cookbook (https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken#3-turn-text-into-tokens-with-encodingencode))