Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Tiktoken Laravel Package

yethee/tiktoken

PHP port of OpenAI tiktoken for fast tokenization. Get encoders by model or encoding, encode text to token IDs, with built-in vocabulary caching (configurable cache dir). Optional experimental FFI mode using tiktoken-rs for better performance on larger inputs.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Strong Fit: The package is a direct port of OpenAI’s tiktoken, making it ideal for Laravel applications leveraging GPT-3.5/4/5/5.x models, embeddings, or token-aware features (e.g., cost optimization, prompt validation, RAG chunking).
  • Laravel Synergy: Aligns with Laravel’s dependency injection, caching (via EncoderProvider), and composer ecosystem. Can integrate with Laravel’s middleware, queues, or logging for token tracking.
  • Tokenization-Centric Use Cases:
    • Cost Control: Enforce token budgets (e.g., reject prompts >8k tokens for GPT-4).
    • Input Validation: Pre-check token counts to avoid API 429/404 errors.
    • Prompt Optimization: Truncate/compress prompts dynamically (e.g., for multi-turn chat).
    • RAG Pipelines: Chunk documents into token-sized embeddings.
    • Analytics: Log token usage for billing or observability.

Integration Feasibility

  • Low Friction: Single composer require + minimal boilerplate (e.g., EncoderProvider setup).
  • Caching: Built-in vocabulary caching (default: sys_get_temp_dir()) reduces I/O overhead. Configurable via TIKTOKEN_CACHE_DIR or EncoderProvider::setVocabCache().
  • Model Support: Covers GPT-3.5/4/4.1/4.5/5/5.1/5.2, embeddings (text-embedding-3-*), and o1/o3 models. No GPT-2 or special tokens (e.g., <|endofprompt|>).
  • Experimental Features:
    • Lib Mode (FFI): Optional Rust-backed tiktoken-rs for high throughput (requires LD_LIBRARY_PATH setup). Not recommended for production due to stability risks and build complexity.
    • Chunked Encoding: encodeInChunks() is unimplemented (TODO in v1.0.0).

Technical Risk

Risk Area Severity Mitigation
Unsupported Models Medium Avoid GPT-2 or models requiring special tokens. Use OpenAI SDK as fallback.
Lib Mode Instability High Skip unless benchmarked for your workload. Prefer native encoder for stability.
Cache Race Conditions Low Fixed in v1.1.1. Monitor cache performance in production.
Performance Bottlenecks Medium Benchmark with composer bench. Use LibEncoder only for large batches.
BC Breaks Low API is stable since v1.0.0. Cache dir is now required (no null support).
Dependency Bloat None Zero external dependencies (pure PHP).

Key Questions for TPM

  1. Model Requirements:
    • Are you using GPT-2 or models needing special tokens? If yes, this package is not a fit.
    • Do you need GPT-5.x support? Confirmed in v1.1.0+.
  2. Performance Needs:
    • What’s your tokenization volume (e.g., 1k/sec vs. 100k/sec)? Lib mode may help only for the latter.
    • Are you processing large documents (e.g., >100k tokens)? Consider chunked encoding (currently unimplemented).
  3. Integration Points:
    • Where will tokenization logic live? (e.g., middleware, service layer, queue jobs).
    • Do you need token counts in logs? Integrate with Laravel’s Log or Sentry.
  4. Cost Optimization:
    • Will you enforce hard token limits (e.g., reject >8k tokens for GPT-4)?
    • Do you need dynamic prompt compression (e.g., replace rare words with subword tokens)?
  5. Future-Proofing:
    • Are you planning to support new models (e.g., GPT-6)? This package updates frequently (last release: 2026-03-10).
    • Do you need custom tokenization rules? This package is not extensible for proprietary logic.
  6. Operational Overhead:
    • Who will monitor cache performance? Default temp dir may fill up under high load.
    • Will you use LibEncoder? If yes, Rust/FFI expertise is required for debugging.

Integration Approach

Stack Fit

  • Laravel Native: Designed for Laravel’s service containers, caching, and composer workflows.
    • Service Provider: Register EncoderProvider as a singleton for global access.
    • Middleware: Validate token counts before API calls (e.g., ValidatePromptTokens).
    • Queues: Offload tokenization for batch jobs (e.g., document processing).
    • Logging: Log token counts with Log::info('Tokens used:', $tokenCount).
  • Compatibility:
    • PHP 8.1+: Confirmed by CI/CD (GitHub Actions).
    • No Framework Lock-in: Pure PHP; works in non-Laravel apps.
    • OpenAI SDK: Can coexist with guzzlehttp/ringphp or openai-php/client.

Migration Path

Step Action Complexity Dependencies
1. Install composer require yethee/tiktoken Low Composer
2. Configure Cache Set TIKTOKEN_CACHE_DIR or call EncoderProvider::setVocabCache() Low Filesystem
3. Basic Usage Inject EncoderProvider into services and use getForModel('gpt-4') Low Laravel DI
4. Middleware Add ValidatePromptTokens to API routes Medium Laravel Middleware
5. Logging Log token counts with Log::info() or Sentry Low Laravel Logging
6. Queue Jobs Offload tokenization for batch processing (e.g., document chunking) Medium Laravel Queues
7. Lib Mode (Opt.) Build Rust lib and configure TIKTOKEN_LIB_PATH High Rust, FFI, CI/CD

Compatibility

  • Pros:
    • Zero Dependencies: Pure PHP; no Python/Rust runtime required (unless using Lib mode).
    • Caching: Leverages Laravel’s cache drivers (e.g., Redis, file) via EncoderProvider.
    • Model Agnostic: Supports all OpenAI models except GPT-2/special tokens.
    • Thread-Safe: Fixed race conditions in v1.1.1.
  • Cons:
    • Lib Mode: Requires Rust/FFI setup; not recommended for production.
    • No GPT-2: Blocking for fine-tuned models relying on custom tokens.
    • Chunked Encoding: Unimplemented (TODO in v1.0.0).

Sequencing

  1. Phase 1: Core Integration (2–3 days)
    • Install package, configure caching, and implement basic tokenization in services.
    • Goal: Replace manual token counting with EncoderProvider.
  2. Phase 2: Validation Layer (1–2 days)
    • Add middleware to validate token counts before API calls.
    • Goal: Reduce API errors by 30–50%.
  3. Phase 3: Optimization (3–5 days)
    • Integrate with queues for batch processing (e.g., document chunking).
    • Add logging for token analytics.
    • Goal: Enable cost optimization and observability.
  4. Phase 4: Advanced (Optional) (1–2 weeks)
    • Experiment with LibEncoder for high-throughput use cases (requires Rust expertise).
    • Implement custom prompt compression logic if needed.
    • Goal: Handle edge cases (e.g., >100k tokens).

Operational Impact

Maintenance

  • Pros:
    • Minimal Overhead: MIT-licensed, actively maintained (last release: 2026-03-10).
    • No External Services: Self-contained; no API keys or dependencies.
    • Cache Management: Automatic vocabulary caching reduces I/O. Monitor disk usage if caching large vocabularies.
  • Cons:
    • Lib Mode: Experimental; requires Rust/FFI expertise to debug.
    • Model Updates: New models (e.g., GPT-6) may require package updates.
    • GPT-2 Gap: No support for custom tokens or GPT-2 variants.

Support

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codraw/entity-migrator
codraw/doctrine-extra
codraw/aws-tool-kit
codraw/validator
codraw/workflow
codraw/open-api
codraw/cron-job
codraw/process
codraw/log
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony