yethee/tiktoken
This PHP port of OpenAI's tiktoken lets you encode text into tokens for models like gpt-3.5-turbo and any BPE encoding such as p50k_base. Includes vocabulary caching by default and an experimental FFI lib mode for faster encoding of larger texts.
Architecture fit: yethee/tiktoken is a Composer-installable PHP port of OpenAI’s tokenizer, not a Laravel-specific package. It fits a Laravel architecture as a small infrastructure dependency behind a thin application service: EncoderProvider->getForModel() or get() returns an encoder, and encode() returns token IDs for counting, prompt validation, chunking, and cost estimation. TPM should treat it as a tokenization primitive, not a complete token-governance layer.
Integration feasibility: Low initial effort: one-line Composer install, MIT license, no Python/Rust requirement for default native mode, and straightforward API. It can support middleware, form requests, queued jobs, RAG chunking, usage analytics, and budget enforcement. The main integration gap is Laravel ergonomics: there is no service provider, facade, config file, or Laravel cache adapter, so the team should wrap it in a small internal package/service.
Technical risk: Moderate. Community signal is thin (0 dependents, 166 stars), so TPM should not assume broad ecosystem validation. The package is actively released and has changelog/readme maturity, including model support through GPT-5.2. The experimental LibEncoder introduces FFI/native-library operational risk and is only justified if benchmarks show throughput need. Known product limitations matter: GPT-2 encoding and special tokens such as <|endofprompt|> are unsupported. Model-to-encoding mapping can lag new OpenAI releases, so package upgrades become part of the AI feature release process. Pin at least 1.1.1 because it fixes a vocabulary-cache race condition.
Key questions: Which OpenAI models/encodings must be supported at launch? Do any features depend on special tokens or GPT-2? What tokenization volume and latency SLO apply? Is FFI/lib mode acceptable operationally, or should native PHP be the only supported path? Where will vocab cache live across Laravel workers and containers? Who owns package upgrades when OpenAI releases new models? What fallback is acceptable if tokenization fails—approximate count, OpenAI usage data, or request rejection? Is OpenAI SDK tokenization already sufficient for some use cases?
Stack fit: Use the package inside Laravel as a singleton-provided internal service, e.g. TokenizationService, configured through config/tokenization.php for cache path, default model, and optional lib mode. Keep EncoderProvider out of controllers and jobs directly; expose intent-level methods such as countForModel(), assertWithinBudget(), chunkForEmbedding(), and estimateCost(). Use it in middleware/form requests for preflight prompt limits, in jobs for batch chunking, and in analytics for token metering.
Migration path: Start in shadow mode: measure token counts without enforcing limits, compare against OpenAI usage/billing data, and validate model mappings. Then add soft warnings and observability, followed by hard limits/truncation for high-risk paths. Only after accuracy and operational behavior are proven should token counts drive customer-facing billing or tier enforcement. Avoid a big-bang replacement of existing OpenAI SDK usage.
Compatibility: Confirm PHP version and Laravel version compatibility against the package’s composer.json before adoption. Default mode is portable PHP, but the vocab cache must be writable and preferably persistent across deployments. In multi-instance Laravel deployments, pre-warm or share the cache directory; do not rely on ephemeral temp directories for cold-start performance. FFI/lib mode requires additional build, deployment, and platform compatibility work and should be feature-flagged.
Sequencing: 1) Composer spike and token-count validation against known prompts. 2) Build Laravel service provider/config/facade wrapper. 3) Add logging and metrics for token counts and cache behavior. 4) Introduce soft prompt-budget checks in staging. 5) Roll out hard limits/truncation behind feature flags. 6) Add chunking for RAG/embeddings. 7) Benchmark native vs lib mode only if volume/latency requires it. 8) Formalize upgrade cadence for new OpenAI model support.
Maintenance: Low code footprint but non-zero ownership. TPM should assign an owner for package upgrades, model-mapping validation, cache operations, and changelog review. Pin versions in composer.lock, review upstream releases, and regression-test token counts when upgrading. The package’s release history is a positive signal, but 0 dependents means the team cannot rely on a large community to surface issues.
Support: No vendor SLA. Internal documentation should state supported models/encodings, known limitations, fallback behavior, and who to contact when counts look wrong. Support teams need guidance that token counts are model-specific and not equivalent to character counts. If customer-facing limits are enforced, provide clear product messaging and logs for disputed token usage.
Scaling: Native PHP tokenization is suitable for moderate request volumes and batch jobs. High-throughput or large-document workloads should be queued, chunked, and possibly offloaded; experimental lib mode can improve medium/large-text throughput but adds FFI and native-library overhead, especially for small texts. Cache warm-up matters for cold containers. Monitor CPU, latency, cache hit rate, and tokenization failures per endpoint.
Failure modes: Vocabulary download/cache failures, unwritable cache directories, checksum mismatch, stale cache, missing FFI library, unsupported model/encoding, special-token mismatch, and model-mapping gaps. Older versions had a cache race condition; require >=1.1.1. Mitigations include pre-baked vocab cache, health checks, fallback estimators, circuit breakers, alerting, and feature flags around enforcement. For critical paths, fail closed only after product sign-off; otherwise degrade to approximate limits or observability-only mode.
Ramp-up: Engineering ramp-up is small for default mode—hours to days for a wrapper and initial instrumentation. Product/analytics ramp-up is larger because teams must align on token budgets, model mappings, customer messaging, and billing implications. TPM success metrics should include token-count accuracy versus OpenAI usage, reduction in oversized-request 4xx/429 errors, cost per AI feature, tokenization latency, cache hit rate, and percentage of traffic under enforced budget policies.
How can I help you explore Laravel packages today?