PHPackages                             fourkitchens/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. fourkitchens/tiktoken

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

fourkitchens/tiktoken
=====================

PHP version of tiktoken

00PHP

Since Jun 12Pushed 1y agoCompare

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

READMEChangelogDependenciesVersions (1)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 yethee/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

14

—

LowBetter than 2% of packages

Maintenance27

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity18

Early-stage or recently created project

 Bus Factor1

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

### Community

Maintainers

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

---

Top Contributors

[![yethee](https://avatars.githubusercontent.com/u/559488?v=4)](https://github.com/yethee "yethee (31 commits)")[![nJim](https://avatars.githubusercontent.com/u/9594124?v=4)](https://github.com/nJim "nJim (2 commits)")[![snapshotpl](https://avatars.githubusercontent.com/u/312655?v=4)](https://github.com/snapshotpl "snapshotpl (1 commits)")

### Embed Badge

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

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

###  Alternatives

[webignition/website-rss-feed-finder

Find the RSS feed from a given website base URL

1311.9k](/packages/webignition-website-rss-feed-finder)[dotink/jin

Jsonified Ini Notation

1410.2k2](/packages/dotink-jin)

PHPackages © 2026

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