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(2y ago)97.7k↓47.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 today

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 72% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity30

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

748d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/67074314?v=4)[Rahul Dey](/maintainers/rahul900day)[@rahul900day](https://github.com/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

[telnyx/telnyx-php

Official Telnyx PHP SDK — APIs for Voice, SMS, MMS, WhatsApp, Fax, SIP Trunking, Wireless IoT, Call Control, and more. Build global communications on Telnyx's private carrier-grade network.

35789.4k2](/packages/telnyx-telnyx-php)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[anthropic-ai/sdk

Anthropic PHP SDK

163583.3k17](/packages/anthropic-ai-sdk)[openai-php/client

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

5.8k28.0M318](/packages/openai-php-client)[n1ebieski/ksef-php-client

PHP API client that allows you to interact with the API Krajowego Systemu e-Faktur

9067.8k](/packages/n1ebieski-ksef-php-client)[aws/aws-sdk-php

AWS SDK for PHP - Use Amazon Web Services in your PHP project

6.3k543.5M2.6k](/packages/aws-aws-sdk-php)

PHPackages © 2026

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