PHPackages                             mis3085/tiktoken-for-laravel - 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. mis3085/tiktoken-for-laravel

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

mis3085/tiktoken-for-laravel
============================

This is tiktoken-php (yethee/tiktoken) wrapper for Laravel

0.1.7(2mo ago)26563.3k↓65.5%6[1 PRs](https://github.com/mis3085/tiktoken-for-laravel/pulls)MITPHPPHP ^8.1|^8.2|^8.3CI passing

Since Apr 12Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/mis3085/tiktoken-for-laravel)[ Packagist](https://packagist.org/packages/mis3085/tiktoken-for-laravel)[ Docs](https://github.com/mis3085/tiktoken-for-laravel)[ RSS](/packages/mis3085-tiktoken-for-laravel/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (6)Dependencies (24)Versions (15)Used By (0)

This is tiktoken-php (yethee/tiktoken) wrapper for Laravel
==========================================================

[](#this-is-tiktoken-php-yetheetiktoken-wrapper-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e7195a02243b72d9fe816c4eec9f383451b519e7e349e5270e43d911bd6f367e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6973333038352f74696b746f6b656e2d666f722d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mis3085/tiktoken-for-laravel)[![GitHub Tests Action Status](https://camo.githubusercontent.com/5c1eaee7211fd76a8fc00562cacd0e71242006689638ed0a67222f799eb7efc9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d6973333038352f74696b746f6b656e2d666f722d6c61726176656c2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/mis3085/tiktoken-for-laravel/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/4b0c2f9a844b2ce7989dc2fdbb1f9080c99b14288569f8880f8de1934eabfca5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d6973333038352f74696b746f6b656e2d666f722d6c61726176656c2f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/mis3085/tiktoken-for-laravel/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/45e4834a2cf3650871f00d648e250d89a1630aa2956ecb1e85590846df202750/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6973333038352f74696b746f6b656e2d666f722d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mis3085/tiktoken-for-laravel)

Use the "tiktoken-php" package to encode a string to tokens, decode tokens to a string or calculate token usage for OpenAI models in Laravel.

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

[](#installation)

You can install the package via composer:

```
composer require mis3085/tiktoken-for-laravel
```

You can publish the config file with:

```
php artisan vendor:publish --tag="tiktoken-for-laravel-config"
```

This is the contents of the published config file:

```
return [
    // Cache folder for vocab files
    'cache_dir' => storage_path('framework/cache/tiktoken'),

    /**
     * The default encoder
     * cl100k_base: gpt-4, gpt-3.5-turbo, text-embedding-ada-002
     * p50k_base: Codex models, text-davinci-002, text-davinci-003
     * r50k_base: text-davinci-001
     */
    'default_encoder' => 'cl100k_base',
];
```

Usage
-----

[](#usage)

```
use Mis3085\Tiktoken\Facades\Tiktoken;
// or
use Tiktoken;

// Use the default encoder: cl100k_base
Tiktoken::encode('this is a test');
// [ 576, 374, 264, 1296 ]

Tiktoken::encode('測試');
// [ 35086, 105, 50520, 99 ]

// Count tokens
Tiktoken::count('測試');
// 4

// Truncate a string to the specified length of tokens
Tiktoken::limit('this is a test', 2);
// this is
Tiktoken::limit('測試', 2);
// 測
Tiktoken::limit('測試', 1);
// EMPTY STRING

// Decode
Tiktoken::decode([ 35086, 105, 50520, 99 ]);
// 測試

// Change encoder in runtime
Tiktoken::setEncoder('p50k_base');
Tiktoken::encode('this is a test');
// [ 5661, 318, 257, 1332 ]

Tiktoken::setEncoder('p50k_base')->encode('測試');
// [ 162, 116, 105, 164, 102, 99 ]

Tiktoken::setEncoderForModel('text-davinci-003')->encode('測試');
// [ 162, 116, 105, 164, 102, 99 ]
```

Testing
-------

[](#testing)

```
composer test
```

Credits
-------

[](#credits)

- [yethee/tiktoken-php](https://github.com/yethee/tiktoken-php)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

55

—

FairBetter than 97% of packages

Maintenance86

Actively maintained with recent releases

Popularity47

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 58.3% 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 ~157 days

Recently: every ~189 days

Total

8

Last Release

75d ago

PHP version history (2 changes)0.1.0PHP ^8.1

0.1.6PHP ^8.1|^8.2|^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/901cd3d3c892f269122329d69542994a208cf30b6345c0338293287ae4acb4f6?d=identicon)[mis3085](/maintainers/mis3085)

---

Top Contributors

[![mis3085](https://avatars.githubusercontent.com/u/17059877?v=4)](https://github.com/mis3085 "mis3085 (42 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (19 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (9 commits)")[![bbrala](https://avatars.githubusercontent.com/u/3294970?v=4)](https://github.com/bbrala "bbrala (1 commits)")[![Rakib01](https://avatars.githubusercontent.com/u/33548111?v=4)](https://github.com/Rakib01 "Rakib01 (1 commits)")

---

Tags

laraveltiktokentiktoken-for-laravel

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/mis3085-tiktoken-for-laravel/health.svg)

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

###  Alternatives

[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.8M47](/packages/spatie-laravel-pdf)[codewithdennis/filament-select-tree

The multi-level select field enables you to make single selections from a predefined list of options that are organized into multiple levels or depths.

329530.5k29](/packages/codewithdennis-filament-select-tree)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3914.6k](/packages/rawilk-profile-filament-plugin)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

124603.0k](/packages/worksome-exchange)[tarfin-labs/event-machine

Event-driven state machines for Laravel with event sourcing, type-safe context, and full audit trail.

199.4k](/packages/tarfin-labs-event-machine)

PHPackages © 2026

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