Omid Sayfun
Omid SayfunComputer Geek
Home
Notebook
Journey

Online

Github
Linkedin
Hevy
Plyatomic
Notebook
Adding prettier to eslint
April 10, 2025
A try at type-safe groupBy function in TypeScript
April 10, 2025
Upgrading my blog to Next 15
April 05, 2025
tsx doesn’t support decorators
March 26, 2025
Extending Window: types vs interfaces
March 21, 2025
Validating NestJS env vars with zod
February 06, 2025
Using node API for delay
February 06, 2025
Loading env file into Node process
February 06, 2025
React Component Lifecycle
November 28, 2024
Email special headers
November 20, 2024
How CQRS is different than Event Sourcing
August 18, 2024
RabbitMQ exchange vs queue
August 14, 2024
PgVector similarity search distance functions
August 13, 2024
PgVector indexing options for vector similarity search
July 31, 2024
Counting GPT tokens
June 30, 2024
Logging route handler responses in Next.js 14
June 19, 2024
Redirect www subdomain with Cloudflare
June 17, 2024
Logging requests in Express app
June 16, 2024
Move Docker volume to bind mount
June 12, 2024
Using puppeteer executable for GSTS
June 08, 2024
Next.js Hydration Window Issue
May 29, 2024
Using Git rebase without creating chaos in your repo
May 16, 2024
Why EQ is Your Next Career Upgrade
May 13, 2024
Storing Vector in Postgres with Drizzle ORM
March 21, 2024
Implementing RPC Calls with RabbitMQ in TypeScript
March 16, 2024
Optimize webpage load with special tags
March 15, 2024
What the hell is Open Graph?
March 13, 2024
List of useful Chrome args
March 10, 2024
My go-to Next.js ESlint config
March 10, 2024
Add update date field to Postgres
February 27, 2024
Canvas macOS issue
February 20, 2024
Combining RxJS observables - Part 1
February 20, 2024

Counting GPT tokens

June 30, 2024

Large Language Models (LLMs) have become essential tools for software developers. With their increasing usage, it's crucial to understand what tokens are, how to count them, and how to manage their costs effectively.

What is a Token?

A token is a piece of text that the model processes as a single unit. This could be as short as one character or as long as one word, depending on the language and the specific word. For example, the phrase "Hello, world!" might be broken down into the following tokens: "Hello", ",", "world", and "!". In essence, tokens are the building blocks that LLMs use to understand and generate language.

Encodings and Models

Encodings are the methods used to convert text into tokens that the model can understand. Different models use different encoding schemes, and this can affect how text is tokenized. For example, OpenAI models like GPT-4 and GPT-3.5 use the cl100k_base encoding. These encodings determine how efficiently the model processes the text and, ultimately, the cost of using the model. Other LLM providers, such as Hugging Face or Google's BERT, use their own unique tokenization methods, which can vary significantly in how they segment text into tokens.

Let’s Count

We can use tiktoken to count tokens for OpenAI models. This library uses WebAssembly for efficient token counting. To get started, install it using:

npm install tiktoken

Here's a snippet to count tokens in a JSON file containing an array of items:

import { get_encoding } from "tiktoken";
import fs from "fs";
 
const filePath = process.argv[2];
const content =





















Continue Reading

fs.
readFileSync
(filePath,
"utf-8"
);
const parsedContent: any[] = JSON.parse(content);
const tokens: number[] = [];
const enc = get_encoding("cl100k_base");
for (const item of parsedContent) {
const encodedContent = enc.encode(JSON.stringify(item));
tokens.push(encodedContent.length);
}
const total = tokens.reduce((acc, cur) => acc + cur, 0);
const average = total / tokens.length;
const max = Math.max(...tokens);
const min = Math.min(...tokens);
console.log("Total", total.toLocaleString());
console.log("Average", average.toLocaleString());
console.log("Max", max.toLocaleString());
console.log("Min", min.toLocaleString());
console.log("Avg/Max average", ((average + max) / 2).toLocaleString());
enc.free();

Most newer models in the GPT family use cl100k_base encoding. This includes models like GPT-4, GPT-3.5, and even embedding models such as text-embedding-3-small and text-embedding-3-large.

I created this simple calculator for you to count tokens directly in your browser. You can upload files or enter text and check total token count, max token count (if your input is an array of items) and average token count.

Encoding

Token Count

-

Max count (if array)

-

Average Count (if array)

-

File will be parsed in the browser and won't be uploaded to the server. Only JSON is supported for now.

To understand the cost implications, you can check OpenAI's pricing here. Remember that token count can also impact the performance and accuracy of the responses generated by the models.

tl;dr

  • Tokens: Basic units of text processed by LLMs.
  • Encodings: Methods to convert text into tokens, differing across LLM providers.
  • Token Counting: Use tiktoken for efficient counting with OpenAI models.
  • Impact on Cost: Token count affects both cost and performance.
  • 04-11-2025

    A try at type-safe groupBy function in TypeScript

  • 04-10-2025

    Adding prettier to eslint

  • 04-10-2025

    Email special headers

  • 04-09-2025

    Upgrading my blog to Next 15

  • 04-09-2025

    Storing Vector in Postgres with Drizzle ORM