Token counting and management for LLMs. Count tokens, truncate text, manage budgets. OpenAI, Anthropic, Llama support.
npx ai-tokenizer count "Hello, world!"- Accurate token counting - Uses tiktoken for precise counts
- Smart truncation - Truncate from end, start, or middle
- Chunking - Split text by tokens, sentences, or paragraphs
- Budget management - Allocate tokens across system/context/response
- Statistics - Analyze compression ratios and token usage
- Multi-model - GPT-4, Claude, Llama, Mistral support
# Use directly with npx (no install needed)
npx ai-tokenizer count "your text here"
# Or install globally
npm install -g ai-tokenizer
# Or add to your project
npm install ai-tokenizer# Count tokens in text
npx ai-tokenizer count "Hello, how are you?"
# Count tokens in a file
npx ai-tokenizer count --file ./document.txt
# Specify model
npx ai-tokenizer count "Hello" --model gpt-3.5-turbo
# Count message tokens (JSON format)
npx ai-tokenizer count --messages '[{"role":"user","content":"Hi"}]'# Truncate to 100 tokens
npx ai-tokenizer truncate "long text..." --tokens 100
# Truncate from start
npx ai-tokenizer truncate "long text..." --tokens 100 --strategy start
# Truncate from middle
npx ai-tokenizer truncate "long text..." --tokens 100 --strategy middle
# Custom ellipsis
npx ai-tokenizer truncate "long text..." --tokens 100 --ellipsis " [...]"# Split into 1000-token chunks
npx ai-tokenizer chunk --file ./large-doc.txt --tokens 1000
# With overlap
npx ai-tokenizer chunk --file ./doc.txt --tokens 1000 --overlap 100
# Save chunks to files
npx ai-tokenizer chunk --file ./doc.txt --tokens 1000 --output ./chunks/part# Get token statistics
npx ai-tokenizer analyze "Your text here"
# Analyze a file
npx ai-tokenizer analyze --file ./document.txt# Compare token counts across models
npx ai-tokenizer compare "Your text" --models gpt-4,claude-3-sonnet,llama-2-70b# Show all supported models and context windows
npx ai-tokenizer modelsimport {
countTokens,
countMessageTokens,
truncateToTokens,
chunkText,
analyzeText,
BudgetManager,
getContextWindow,
} from 'ai-tokenizer';
// Count tokens
const tokens = countTokens("Hello, world!", "gpt-4");
console.log(tokens); // 4
// Count message tokens
const messageTokens = countMessageTokens([
{ role: "system", content: "You are helpful." },
{ role: "user", content: "Hi!" },
], "gpt-4");
// Truncate text
const truncated = truncateToTokens("very long text...", {
maxTokens: 100,
strategy: "end",
ellipsis: "...",
});
// Chunk text
const chunks = chunkText(longDocument, {
maxTokens: 1000,
overlap: 100,
});
// Budget management
const budget = new BudgetManager(8000, "gpt-4");
budget.addSystemPrompt("You are a helpful assistant.");
budget.addContext(relevantDocs);
console.log(budget.getRemainingContext()); // tokens left for more context
console.log(budget.getMaxResponseTokens()); // tokens reserved for response
// Analyze text
const stats = analyzeText("Your text here");
console.log(stats.totalTokens);
console.log(stats.compressionRatio);
// Get context window
const contextWindow = getContextWindow("gpt-4-turbo"); // 128000| Model | Context Window |
|---|---|
| gpt-4-turbo | 128,000 |
| gpt-4 | 8,192 |
| gpt-4-32k | 32,768 |
| gpt-3.5-turbo | 16,385 |
| claude-3-opus | 200,000 |
| claude-3-sonnet | 200,000 |
| claude-3-haiku | 200,000 |
| gemini-1.5-pro | 1,000,000 |
| mistral-large | 32,000 |
| llama-2-70b | 4,096 |
Count tokens in text string.
Count tokens in chat messages array (includes overhead).
Truncate text to fit within token limit.
Split text into chunks of specified token size.
Split text by sentences, respecting token limit.
Split text by paragraphs, respecting token limit.
Get token statistics for text.
Class for managing token budgets across system/context/response.
Get context window size for a model.
Check if text fits in model's context window.
One of 110+ free developer tools from LXGIC Studios. No paywalls, no sign-ups.
Find more:
- GitHub: https://github.com/lxgicstudios
- Twitter: https://x.com/lxgicstudios
- Website: https://lxgicstudios.com
- npm: https://www.npmjs.com/~lxgicstudios
MIT. Free forever.