Tokenizer
Calculate DeepSeek model token usage offline — no API calls needed, powered by Transformers.js.
@deepseek-kit/tokenizer is a standalone package for offline token counting. It bundles the DeepSeek tokenizer vocabulary and uses Transformers.js to encode text locally — no API key, no network requests, no API costs. This is useful for estimating prompt costs, validating context length limits, and monitoring token usage before sending requests.
Installation
This package depends on @huggingface/transformers (Transformers.js). The tokenizer vocabulary files are bundled with the package, so no additional downloads are required at runtime.
Basic Usage
Counting Tokens
Use countTokens() to get the token count of a text:
This is the quickest way to estimate how many tokens a piece of text will consume when sent to the DeepSeek API.
Use Cases
Estimating Prompt Cost
Before sending a request, estimate the token count to calculate the approximate cost:
Validating Context Length
DeepSeek models have a maximum context length. Check before sending long texts:
Monitoring Multi-turn Conversations
Track token accumulation across conversation turns:
API Reference
countTokens()
Counts the number of tokens in a text.
Returns a Promise<number> — the total token count.

