Weave 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 For Laravel Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Unit economics and pricing model. Tokens are the meter for nearly every LLM cost line. This package lets you count tokens before the call, which is what turns "we think inference costs ~$0.02 per run" into a priced, defensible number. That directly supports subscription tier design, per-seat vs. per-usage decisions, and margin modeling.
  • Feature scoping against context windows. Every prompt-in, completion-out feature has a hard ceiling. Pre-flight token counts make "can we fit the full document history in the prompt?" a sizing decision rather than a production incident, and tell you when to invest in summarization, retrieval, or a larger-context model tier.
  • RAG and chunking strategy. Token-based chunking (rather than character- or word-based) is reliably better for embedding quality and retrieval hit rate. This is a quality lever you can A/B test cheaply, and it changes both retrieval accuracy and index cost.
  • Truncation and degradation policy. Deciding what gets dropped when a prompt overflows — oldest turns, tool output, retrieved passages — is a product decision about behavior users will notice. Token counting is what makes that policy explicit and testable instead of an invisible bug.
  • Quota, abuse, and fair-use enforcement. Per-tenant token budgets, per-request caps, and pre-call rejection of oversized requests are cheaper than post-hoc billing reconciliation and a much better experience than a surprise invoice or a mid-stream error.
  • Usage metering and internal chargeback. Accurate per-feature and per-customer token attribution feeds dashboards, invoicing, and — for B2B — the usage reports your customers will ask for.
  • Build-vs-buy for a small piece of infrastructure. This is a ~1-file-thick wrapper. Adopting it is a decision not to spend engineer-days on encoding files, model-name mapping, and the cl100k_base/o200k_base drift problem — a small but real maintenance tax.

When to Consider This Package

Adopt when:

  • You're on Laravel and your LLM work is OpenAI-family models (chat completions, embeddings). The encodings are tied to OpenAI's tokenizers; that's the whole value proposition.
  • You need pre-call estimates — budgeting, quotas, truncation, or cost display — not just post-hoc usage from API responses.
  • Your stack is PHP-only and standing up a Python sidecar just to count tokens is disproportionate.
  • Request volume is moderate and latency-tolerant; you care more about correctness and developer time than microseconds per count.
  • You want token counting injectable and testable through the container rather than scattered new calls.

Look elsewhere when:

  • You're multi-vendor (Anthropic, Gemini, Mistral, open-weight models). Tokenizers differ; a single OpenAI-shaped counter will silently misestimate. Use each provider's counting endpoint (e.g., Anthropic's count_tokens, Gemini's countTokens) or a provider-agnostic abstraction.
  • You're high-throughput / latency-sensitive. PHP tiktoken ports are meaningfully slower and heavier than the Rust original; caching encodings helps but won't close the gap at scale.
  • You need byte-exact parity with the provider's billing. Treat any local count as an estimate; reconcile against returned usage for anything customer-facing or financially material.
  • Supply-chain sensitivity. 27 stars, effectively a single maintainer, and a thin wrapper means the abandonment risk is "you vendor 80 lines of code" — low blast radius, but you should be comfortable pinning the version and owning the fallback. Also confirm the release cadence keeps pace with new model/encoding releases.
  • Your framework already gives you this — some OpenAI PHP clients ship tokenizer helpers, and a Laravel wrapper is then redundant indirection.

Adoption gate I'd set: wrap it behind your own narrow interface (TokenCounter::count(string $text, string $model): int) on day one. The wrapper is cheap; the lock-in isn't the point. That way a swap to a provider endpoint or a Rust-backed service is a one-file change, and you can unit-test with a fake counter.

How to Pitch It (Stakeholders)

For executives (one paragraph): We currently have no reliable way to predict what an LLM feature costs before it runs, which means our pricing and margin assumptions are guesses and our context-limit handling is reactive. This package adds pre-call token counting to our Laravel stack in a few hours of work: it lets us price usage-based tiers with real numbers, cap runaway spend per customer, and make retrieval quality decisions on evidence. It's MIT-licensed, a thin wrapper over a known tokenizer, and we'll isolate it behind our own interface so the switching cost stays near zero. The risk is maintainer attention on a small project, not technical lock-in — and the exposure is negligible given the wrapper's size.

For engineering:

  • Laravel-native binding — service container, config, testable; no bespoke glue.
  • Correctness on the piece that's easy to get wrong: encoding selection and model-name → encoding mapping, including o200k_base for newer models.
  • Encourages the right pattern: count before you call, so truncation and quota logic live in our code rather than being discovered in production.
  • Cache/reuse encodings; count at boundaries (request ingress, chunking, prompt assembly), not in hot loops.
  • Caveats to own explicitly: estimates only — reconcile with provider usage for billing; PHP port performance is not the Rust original's; pin the version and keep a fallback path.

For finance/ops: gives us a per-request cost figure we can attach to a customer, a feature, and an environment — the missing input for usage-based pricing and for forecast-vs-actual reviews.

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.
diephp/sequences
users-au/socialite-provider
twidpay/twid-logger
mrnewport/laravel-plaid
mrnewport/laravel-repo
mrnewport/laravel-docsign
mrnewport/laravel-stow
mrnewport/laravel-flow
mrnewport/laravel-priceable
mrnewport/laravel-nestedterms
fredmeng/laravel-apidoc-generator
itstructure/laravel-multilingual-tools
pdustdar/elasticsearch
markupai/php-sdk
rlacerda83/lumem-microservice-shipment
uni-method/symfony-lib
tacticmedia/scim-server-bundle
neika-ai/symfony-bundle
mattleads/agent-publishing-bundle
glavweb/framework-bundle