PHPackages                             guttedgarden/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. guttedgarden/tiktoken

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

guttedgarden/tiktoken
=====================

PHP 7.4 version of tiktoken

1.2.1(1y ago)419.7k—1.4%1MITPHPPHP &gt;=7.4

Since Oct 13Pushed 1y agoCompare

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

READMEChangelog (4)DependenciesVersions (5)Used By (0)

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

[](#tiktoken-php)

[Yethee's tiktoken library](https://github.com/yethee/tiktoken-php) port for PHP 7.4 without the Symfony package

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

[](#installation)

```
$ composer require guttedgarden/tiktoken
```

Usage
-----

[](#usage)

```
use guttedgarden\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 guttedgarden\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 guttedgarden\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

36

—

LowBetter than 82% of packages

Maintenance47

Moderate activity, may be stable

Popularity31

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.9% 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 ~183 days

Total

4

Last Release

396d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7af2611ad7a5dcdee44645e7e21ae61249cb7709c66d289d2e7262813ed83f01?d=identicon)[guttedgarden](/maintainers/guttedgarden)

---

Top Contributors

[![yethee](https://avatars.githubusercontent.com/u/559488?v=4)](https://github.com/yethee "yethee (10 commits)")[![guttedgarden](https://avatars.githubusercontent.com/u/61158568?v=4)](https://github.com/guttedgarden "guttedgarden (1 commits)")

---

Tags

encodedecodeopenaitokenizerbpetiktoken

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/guttedgarden-tiktoken/health.svg)](https://phpackages.com/packages/guttedgarden-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.9k13](/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)
