PHPackages                             guanhui07/tiktoken - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. guanhui07/tiktoken

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

guanhui07/tiktoken
==================

PHP version of tiktoken

1.0.2(1y ago)16MITPHPPHP ^7.2

Since Aug 23Pushed 1y agoCompare

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

READMEChangelogDependencies (1)Versions (3)Used By (0)

tiktoken-php
============

[](#tiktoken-php)

[![Packagist Version](https://camo.githubusercontent.com/5147925659d825e37776022435ad1bd5843c5df25b43571fdd2740e760dce064/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7965746865652f74696b746f6b656e)](https://camo.githubusercontent.com/5147925659d825e37776022435ad1bd5843c5df25b43571fdd2740e760dce064/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7965746865652f74696b746f6b656e)[![Build status](https://camo.githubusercontent.com/4cab2c0c6ec906945697771dcbb10103959d6a013621566137e05e2983f5c8a8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7965746865652f74696b746f6b656e2d7068702f63692e796d6c3f6272616e63683d6d6173746572)](https://camo.githubusercontent.com/4cab2c0c6ec906945697771dcbb10103959d6a013621566137e05e2983f5c8a8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7965746865652f74696b746f6b656e2d7068702f63692e796d6c3f6272616e63683d6d6173746572)[![codecov](https://camo.githubusercontent.com/ac603ff8b19d938a20392cc4f30669bada69f488de48ae15015a93d585b655f7/68747470733a2f2f636f6465636f762e696f2f6769746875622f7965746865652f74696b746f6b656e2d7068702f67726170682f62616467652e737667)](https://codecov.io/github/yethee/tiktoken-php)[![License](https://camo.githubusercontent.com/ff379c0e29a40323abd0e16072d33f494461020c112a2209a258341dd3ffd5e1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7965746865652f74696b746f6b656e2d706870)](https://camo.githubusercontent.com/ff379c0e29a40323abd0e16072d33f494461020c112a2209a258341dd3ffd5e1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7965746865652f74696b746f6b656e2d706870)

This is a port of the [tiktoken](https://github.com/openai/tiktoken).

Installation
------------

[](#installation)

```
$ composer require guanhui07/tiktoken
```

Usage
-----

[](#usage)

```
use Yethee\Tiktoken\EncoderProvider;

$provider = new EncoderProvider();

$encoder = $provider->getForModel('gpt-3.5-turbo-0301');
$tokens = $encoder->encode('Hello world!');
print_r($tokens);
// OUT: [9906, 1917, 0]

$encoder = $provider->get('p50k_base');
$tokens = $encoder->encode('Hello world!');
print_r($tokens);
// OUT: [15496, 995, 0]
```

Cache
-----

[](#cache)

The encoder uses an external vocabularies, so caching is used by default to avoid performance issues.

By default, the [directory for temporary files](https://www.php.net/manual/en/function.sys-get-temp-dir.php) is used. You can override the directory for cache via environment variable `TIKTOKEN_CACHE_DIR`or use `EncoderProvider::setVocabCache()`:

```
use Yethee\Tiktoken\EncoderProvider;

$encProvider = new EncoderProvider();
$encProvider->setVocabCache('/path/to/cache');

// Using the provider
```

### Disable cache

[](#disable-cache)

You can disable the cache, if there are reasons for this, in one of the following ways:

- Set an empty string for the environment variable `TIKTOKEN_CACHE_DIR`.
- Programmatically:

```
use Yethee\Tiktoken\EncoderProvider;

$encProvider = new EncoderProvider();
$encProvider->setVocabCache(null); // disable the cache
```

Limitations
-----------

[](#limitations)

- Encoding for GPT-2 is not supported.
- Special tokens (like ``) are not supported.

License
-------

[](#license)

[MIT](./LICENSE)

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 Bus Factor1

Top contributor holds 81.4% 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

Every ~0 days

Total

2

Last Release

624d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3abde3fccf025ce86670101d7cfa0641d2415c9daad93e5f0c9573a2b23ddbda?d=identicon)[guanhui07](/maintainers/guanhui07)

---

Top Contributors

[![yethee](https://avatars.githubusercontent.com/u/559488?v=4)](https://github.com/yethee "yethee (35 commits)")[![guanhui07](https://avatars.githubusercontent.com/u/5820457?v=4)](https://github.com/guanhui07 "guanhui07 (7 commits)")[![snapshotpl](https://avatars.githubusercontent.com/u/312655?v=4)](https://github.com/snapshotpl "snapshotpl (1 commits)")

---

Tags

chatgpttiktokentiktoken-phpencodedecodeopenaitokenizerbpetiktoken

### Embed Badge

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

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

###  Alternatives

[yethee/tiktoken

PHP version of tiktoken

1583.1M15](/packages/yethee-tiktoken)[gioni06/gpt3-tokenizer

PHP package for Byte Pair Encoding (BPE) used by GPT-3.

85537.5k8](/packages/gioni06-gpt3-tokenizer)[rajentrivedi/tokenizer-x

TokenizerX calculates required tokens for given prompt

91214.0k3](/packages/rajentrivedi-tokenizer-x)[kherge/json

Encodes, decodes, and validates JSON data.

61226.6k6](/packages/kherge-json)[devium/toml

A PHP encoder/decoder for TOML compatible with specification 1.0.0

3968.9k12](/packages/devium-toml)[danny50610/bpe-tokeniser

PHP port for openai/tiktoken (most)

10422.0k1](/packages/danny50610-bpe-tokeniser)

PHPackages © 2026

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