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.4(1y ago)25544.1k↓14.4%4[4 PRs](https://github.com/mis3085/tiktoken-for-laravel/pulls)MITPHPPHP ^8.1CI 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 1mo ago

READMEChangelog (3)Dependencies (12)Versions (9)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

47

—

FairBetter than 94% of packages

Maintenance68

Regular maintenance activity

Popularity46

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

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

###  Release Activity

Cadence

Every ~173 days

Total

5

Last Release

440d ago

### 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 (26 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (14 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (8 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-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[hirethunk/verbs

An event sourcing package that feels nice.

513162.9k6](/packages/hirethunk-verbs)[worksome/exchange

Check Exchange Rates for any currency in Laravel.

123544.7k](/packages/worksome-exchange)[ralphjsmit/livewire-urls

Get the previous and current url in Livewire.

82270.3k4](/packages/ralphjsmit-livewire-urls)[hydrat/filament-table-layout-toggle

Filament plugin adding a toggle button to tables, allowing user to switch between Grid and Table layouts.

6292.3k1](/packages/hydrat-filament-table-layout-toggle)

PHPackages © 2026

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