mis3085/tiktoken-for-laravel
TikToken for Laravel brings OpenAI's tiktoken tokenizer to your app, letting you count, encode, and decode tokens for GPT models through a simple facade. Estimate API costs, trim prompts to fit context limits, and integrate token counts into validation, logging, or usage tracking.
Architecture fit
yethee/tiktoken (a PHP port of OpenAI's BPE tokenizer). It adds no persistence, no queueing, and no business logic — it is a pure utility library. That means architectural risk is low and the blast radius is contained: it either returns a count or it throws.TokenCounter interface so the facade does not leak into domain code and so you can swap implementations (or a sidecar) later.Integration feasibility
composer require plus package auto-discovery; expected to be a sub-hour integration for a competent Laravel developer. Config publish for encoding/cache settings. Low friction.cl100k_base, o200k_base, …) that are typically downloaded on first use. Confirm: where are they written, are they cached across deploys, and can they be pre-baked into the image? For containerized, autoscaled, or air-gapped environments an implicit network fetch at runtime is a deployment blocker, not a detail.Technical risk
count($text) will undercount real usage. Validate the wrapper's output against the usage object returned by the API before you build any billing or quota feature on it.o200k_base; older use cl100k_base. If the wrapper's model→encoding map is stale, it may silently fall back to a default encoding and return quietly wrong numbers. Verify current-model coverage and whether mismatches throw or silently degrade — silent is worse.Key questions
encode/decode (token arrays, useful for truncation and chunking) or only counts?Stack fit
Migration path
strlen()/4 or similar heuristics: those numbers feed dashboards, quotas, and truncation logic. Migrate by running the new counter in shadow mode alongside the heuristic for a sprint, log the delta, and reconcile against actual API usage before switching any quota enforcement. Expect the heuristic to have been undercounting.Compatibility
Sequencing
TokenCounter contract; no direct facade usage in domain code.Maintenance
Support
Scaling
Failure modes
Ramp-up
How can I help you explore Laravel packages today?