How Specializing the Vocabulary of SLMs Can Cut Inference Cost by 77% in Real Applications
Researchers at Dharma-AI have developed a method to reduce the inference cost of specialized language models by up to 77%—without changing the base model or training data. This breakthrough demonstrates that efficiency in AI systems can be achieved not just through model scaling or data augmentation, but through a more granular optimization of the model’s internal structure—specifically, its vocabulary.
Background: The Hidden Cost of Tokenization
Language models process text into tokens using a tokenizer. This tool splits text into pieces based on rules like sub-word segmentation. General-purpose models like Qwen or Gemma are trained on vast, diverse datasets—ranging from code to contracts. This broad training ensures versatility.
When a model is fine-tuned for a specific task—like grading Portuguese essays—the original tokenizer remains unchanged. This mismatch leads to inefficiency.
Two problems arise:
- Fragmentation: The tokenizer splits common phrases—like JSON response structures—into multiple tokens. Each piece must be processed during inference.
- Excess: The model maintains a full vocabulary of tens of thousands of entries. Many tokens are never used, yet the model still computes their probabilities.
Why Input and Output Cost Differ
Inference has two phases: prefill and decode. These phases have different cost drivers.
During prefill, the model reads the entire input once. Each weight is reused. This phase is compute-limited.
During decode, the model generates output one token at a time. Each token requires a full network pass. This phase is memory-bound due to the KV cache growing with each response.
Generating a token costs about 7.4 times more than reading input. In the Portuguese essay grader, this ratio is key.
Think of a shipping company charging per box. Incoming boxes are cheaper. Outgoing boxes have higher fees due to packing and delivery. Reducing output boxes saves more. In AI, cutting generated tokens—especially long, repeated ones—has a much greater impact.
How VocabX Reduces Cost: A Shift from Frequency to Cost
Traditional methods—like Fast Vocabulary Transfer or in-place BPE—prioritize token frequency. They remove rare tokens, reducing total count.
But a long, repeated response structure may cost far more to generate than a common input token. A token that appears only in output may save more than a frequent input one.
VocabX changes the question: instead of asking how many tokens are saved, it asks how much cost is saved. Each token is weighted by position—input, prompt, or response. A response token saves 7.4 times more than an input token.
This cost-weighted approach targets the most expensive spans—those in output and repeated—first. The goal is not fewer tokens, but lower total inference cost.

Two-Layer Adaptation: Sub-Word and Macro Tokens
VocabX works at two levels:
- Sub-word layer: Merges common word parts into single sub-tokens. For example, ‘evaluation’ may be combined if it appears consistently in a context.
- Macro layer: Turns repeated spans—like full JSON envelopes—into single tokens, called macro tokens.
Both layers use the same cost criterion. This ensures high-priority spans—especially in output—are compressed first.
Five Miners Identify Repeating Patterns
To find repeated spans, VocabX uses five specialized tools:
- Long words: Finds terms like ‘excellent’ or ‘inadequate’ in response structures.
- Whole lines: Detects headers or footers in essays or reports.
- Expressions: Finds common phrases like ‘the student wrote’.
- Complete sentences: Identifies full sentences reused in similar contexts.
- Structured content: Targets formal formats like JSON or XML tags.
On average, the structured content miner contributed the most. In code, it alone accounted for 17.5 points of reduction. Without all five, gains drop by 55.6 points—nearly a third of total savings.
Estimating Gains Beyond the Sample
Traditional methods use a small sample to select tokens. Only 16.6% of all tokens appear in a 400-document sample.
Some tokens are rare in the sample but common in the full corpus. If discarded, they lose value.
VocabX uses statistical modeling to estimate gains for unseen tokens. It assumes rare but frequent tokens still have cost-saving potential.
This change raised token reduction on a contracts corpus from 44.7% to 52.8%. It ensures the final vocabulary reflects real-world usage.
Results in the Portuguese Essay Grader
The team trained seven versions of the same essay grader using Qwen3-4B. All used the same data and training recipe. Only the tokenizer varied.
All ran 500 requests on an RTX 5090 with vLLM. Cost was measured from actual runtime.
Key results:
- Quality improved from 0.703 to 0.810—measured by grading accuracy and off-topic screening.
An early version fused grade and JSON into one macro token. This reduced token count but dropped quality to 0.689.
Why? The model made one decision covering both structure and evaluation. This created a non-reversible, less interpretable choice.
When the grade had its own token, the model preserved separate decisions. This improved accuracy and raised the bench score to 0.810. Cost dropped to 77.0%—up from 73.3% in the fused version.
This shows: compression must not sacrifice decision-making. The goal is a vocabulary that preserves the tokens that matter.
Testing Across Domains and Models
To test generalizability, VocabX was compared to three reference methods across five domains:
- Contracts
- Medical papers
- Scientific papers
- News
- Python code
Three model families were tested: Qwen3, Llama-3.1, and Gemma-3. All used the same data and vocabulary limits.
Results:
- On average, VocabX achieved 10.2 points more cost reduction on Qwen3, 7.5 on Llama-3.1, and 13.6 on Gemma-3.
- On contracts, it achieved 62.6% token reduction with 113,426 entries—vs. 61.8% for the best in-place BPE setup using 207,803 entries.
This shows VocabX works across domains—legal, medical, scientific, technical—and model families.
What VocabX Actually Does
VocabX does not improve model intelligence. It does not retrain the model or change its knowledge.
It reduces inference cost by:
- Stopping the model from generating unnecessary text—like redundant response structures.
- Stopping the model from computing probabilities for unused tokens—like rare or irrelevant entries.
With the same base model and training, inference cost dropped 77% in real use. This proves small, targeted changes to tokenization can yield major savings.
Limitations and Considerations
VocabX works best in domains with repetitive, structured outputs. It may perform less well in open-ended tasks—like creative writing or free-form dialogue—where patterns are less predictable.
Results are based on specific test data and hardware. Performance may vary with different models, inputs, or environments. The method assumes a stable, well-defined domain.
The full method, including all experiments and discarded approaches, will be published in a peer-reviewed paper under review.
What to Watch Next
As AI systems grow more specialized, vocabulary optimization will become a key efficiency factor. Future work may explore:
- Dynamic vocabularies: Systems that adapt in real time based on user input.
- Integration into live pipelines: Embedding VocabX directly into real-time AI services.
- Use in high-stakes domains: Deploying specialized models in legal, medical, and technical writing.
For more details, see the original research at Dharma-AI’s VocabX blog post.
Specialization usually begins with data and weights. But it can also start one layer earlier—within the vocabulary itself. This suggests future AI systems may be designed with smarter, domain-tailored tokenizers.
Sources & further reading
Featured image: original publisher image. Via original publisher. Image source

