Skip to main content

tiktoken-cli

Simple wrapper around tiktoken to use it in your favorite language.

Why

If you play with openAI's GPT API, you probably encounter one annoying problem : your prompt is allowed a given amount of tokens, you have no idea how those tokens are counted, and you only know it was too much when the API replies with an error, which is seriously annoying (and slow).

OpenAI published its tiktoken token counting library to solve that, which helps a lot! If you're writing a python program.

tiktoken-cli allows you to write your program in any language. It's a simple wrapper around tiktoken that will read your prompt from STDIN and write the tokens to STDOUT. Now you can write your program in your favorite language and use the CLI for token counting.

It's almost not worth publishing a github repos for so few lines, but I figured that README explanation would be valuable for people wondering how to use openAI's API in their favorite language, the code is merely an executable example.

A note for those of you who may be new to GPT's API : having the count of tokens from your prompt alone is not enough to avoid the exceed context errors. This is because the API wraps your messages with its own content. The exact algorithm to know if you're going to exceed the count is documented in the API doc here (it's in the block "Deep dive : Counting tokens for chat API calls").

Install

tiktoken-cli is a simple script, you can install via pipx.

pipx install tiktoken-cli

Usage

To use tiktoken send your prompt as STDIN, and read the tokens as STDOUT.

Examples:

In shell:

tiktoken --model gpt-4 in.txt out.txt

Replace the file with - for standard input/output:

echo "Hello, world!" | tiktoken --model gpt-4 - out.txt # writes tokens to out.txt
tiktoken --model gpt-4 in.txt - # writes tokens to stdout

You can count tokens easily:

echo "Hello, world!" | tiktoken - | wc -l

Model

tiktoken counts tokens differently based on model. By default, the model used is gpt-3.5-turbo.

You can change the model using the --model option.

For the full list of models available:

tiktoken --help

Metadata

Release files for tiktoken-cli 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tiktoken-cli 1.0.0
File Size Uploaded
tiktoken_cli-1.0.0.tar.gz 2.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tiktoken-cli 1.0.0
File Interpreter ABI Platform
tiktoken_cli-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 5.1 kB

Release files / tiktoken_cli-1.0.0.tar.gz

Download URL tiktoken_cli-1.0.0.tar.gz
Size 2.1 kB
Tags Source
SHA-256 checksum
How to use checksums
0878121b924bb8d43f2f7f22e1019919844487e7a5d419f58aa9e50a8d30bda6
BLAKE2b-256 checksum
How to use checksums
5700895703fa45fe2793d2dc36ffc179298c9f405513d36649da9530cbdf1e2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.25.1

Release files / tiktoken_cli-1.0.0-py3-none-any.whl

Download URL tiktoken_cli-1.0.0-py3-none-any.whl
Size 3.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
330d7ac364cab3fb6c7b3f44900861a9262c3d175c6a98f91d739ecb3d773624
BLAKE2b-256 checksum
How to use checksums
b95708b46c7ac5caddbb2a0fcb7d3ff9e1115be23a039109d9b1424e88a2cef2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.25.1

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page