PHPackages                             rahul900day/tiktoken-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rahul900day/tiktoken-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rahul900day/tiktoken-php
========================

1.0.0(1y ago)76.8k↓44.7%MITPHPPHP ^8.1

Since Jun 16Pushed 1y ago1 watchersCompare

[ Source](https://github.com/RahulDey12/tiktoken-php)[ Packagist](https://packagist.org/packages/rahul900day/tiktoken-php)[ RSS](/packages/rahul900day-tiktoken-php/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (17)Versions (2)Used By (0)

⏳ Tiktoken for PHP
==================

[](#-tiktoken-for-php)

Most compatible PHP implementation of OpenAI's original [Tiktoken](https://github.com/openai/tiktoken).

Get Started
-----------

[](#get-started)

> Requires [PHP 8.1+](https://php.net/releases/)

Install Tiktoken via [Composer](https://getcomposer.org/) package manager.

```
composer require rahul900day/tiktoken-php
```

Supported Models
----------------

[](#supported-models)

ModelSupportedGPT-3✅GPT-3.5 &amp; 4✅GPT-4o❌Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

```
use Rahul900day\Tiktoken\Tiktoken;

$encoder = Tiktoken::getEncodingForModel('gpt-4');
$encoder->encode("hello world aaaaaaaaaaaa");
$encoder->decode([9906, 4435]);
```

### Special Tokens

[](#special-tokens)

```
use Rahul900day\Tiktoken\Tiktoken;

$encoder = Tiktoken::getEncodingForModel('gpt-4');
$encoder->encode('', allowedSpecial: 'all');
```

### Caching

[](#caching)

Tiktoken always cache the server's responses when downloading them.

By default it uses the system's default temporary directory to cache a response but you can still overwrite the cache location by setting `TIKTOKEN_CACHE_DIR` environment variable.

### Registering Custom Encoding

[](#registering-custom-encoding)

```
use Rahul900day\Tiktoken\Encodings\OpenAiPublic\Cl100KBaseEncoding;

class Cl100KIm extends Cl100KBaseEncoding
{
    protected function getName(): string
    {
        return 'cl100k_im';
    }

    protected function getSpecialTokens(): array
    {
        return [
            ...parent::getSpecialTokens(),
            "" => 100264,
            "" => 100265,
        ];
    }
}

use Rahul900day\Tiktoken\Registry;
use Rahul900day\Tiktoken\Tiktoken;

Registry::registerCustomEncoding('cl100k_im', new Cl100KIm);
$encoding = Tiktoken::getEncoding('cl100k_im');

// Expect: 100264
$encoding->encode("", allowedSpecial: 'all');
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Rahul Dey](https://github.com/RahulDey12)
- [All Contributors](https://github.com/RahulDey12/tiktoken-php/graphs/contributors)

License
-------

[](#license)

This package is released under the [MIT License](https://github.com/RahulDey12/tiktoken-php/blob/main/LICENSE.md).

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

701d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/aaa15ae1ce7dae65de8cd5ffb0c2e2f0429fdc2b6308004827f20a7973f735d3?d=identicon)[rahul900day](/maintainers/rahul900day)

---

Top Contributors

[![RahulDey12](https://avatars.githubusercontent.com/u/26340814?v=4)](https://github.com/RahulDey12 "RahulDey12 (43 commits)")

---

Tags

bpe-tokenizerspecial-tokenstiktokentiktoken-php

###  Code Quality

TestsPest

Static AnalysisPHPStan, Rector

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/rahul900day-tiktoken-php/health.svg)

```
[![Health](https://phpackages.com/badges/rahul900day-tiktoken-php/health.svg)](https://phpackages.com/packages/rahul900day-tiktoken-php)
```

###  Alternatives

[openai-php/client

OpenAI PHP is a supercharged PHP API client that allows you to interact with the Open AI API

5.8k22.6M232](/packages/openai-php-client)[anthropic-ai/sdk

Anthropic PHP SDK

129134.7k5](/packages/anthropic-ai-sdk)[opensearch-project/opensearch-php

PHP Client for OpenSearch

15224.3M65](/packages/opensearch-project-opensearch-php)[getbrevo/brevo-php

Official Brevo provided RESTFul API V3 php library

963.1M35](/packages/getbrevo-brevo-php)[swisnl/json-api-client

A PHP package for mapping remote JSON:API resources to Eloquent like models and collections.

211473.2k12](/packages/swisnl-json-api-client)[open-telemetry/sdk

SDK for OpenTelemetry PHP.

2322.9M248](/packages/open-telemetry-sdk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
