Precise token counter for GPT, Claude, and Gemini models (source of truth from software kernel)
Precise token counting using official tokenizers as source of truth from software kernel.
Provides accurate token counts for LLM models (GPT, Claude, Gemini) without approximation. Does not perform validation or judgment—returns raw numbers only (Unix philosophy).
deno run --allow-net <url>/stoptimizer.ts "hello world"
# Output: 2 2 2 2 2 2 2 (space-separated: gpt-5 gpt-5.2 gpt-5-mini gpt-4o gpt-4 gpt-3.5 claude)
deno run --allow-net <url>/stoptimizer.ts --json "test"
# Output: {"gpt-5":1,"gpt-5.2":1,"gpt-5-mini":1,"gpt-4o":1,"gpt-4":1,"gpt-3.5":1,"claude-3.5-sonnet":1}
deno run --allow-net <url>/stoptimizer.ts --model gpt-4o "text"
# Output: 1
For files too large for command-line arguments, use stdin:
# Using dash (Unix tradition)
cat KERNEL.md | deno run --allow-net <url>/stoptimizer.ts -
# Using --stdin flag (modern clarity)
deno run --allow-net <url>/stoptimizer.ts --stdin < large-file.txt
# Combined with other options
cat prompt.txt | deno run --allow-net <url>/stoptimizer.ts - --json
cat README.md | deno run --allow-net <url>/stoptimizer.ts --stdin --model gpt-4o
Encoding Note: GPT-5 family and GPT-4o use o200k_base (~2.3 MB vocabulary), while GPT-4 and GPT-3.5 use cl100k_base (~1.1 MB vocabulary). Token counts will differ between encodings for the same text.
Tool provides counts only. Compose validation logic in shell scripts:
#!/bin/bash
# Example: STOP validation with stdin (for large prompts)
OLD=$(cat prompt-v1.txt | deno run --allow-net <url>/stoptimizer.ts - --model gpt-4o)
NEW=$(cat prompt-v2.txt | deno run --allow-net <url>/stoptimizer.ts - --model gpt-4o)
if [ "$OLD" -eq "$NEW" ]; then
CHARS_OLD=$(wc -c < prompt-v1.txt)
CHARS_NEW=$(wc -c < prompt-v2.txt)
GAIN=$((CHARS_NEW - CHARS_OLD))
echo "âś… STOP COMPLIANT: Both $OLD tokens, gained $GAIN chars"
exit 0
else
echo "❌ STOP VIOLATION: $OLD vs $NEW tokens"
exit 1
fi
# .github/workflows/stop-validation.yml
name: STOP Protocol Validation
on: [pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
- name: Validate prompt changes
run: |
TOOL_URL="https://raw.githubusercontent.com/ShipFail/promptware/main/os/skills/stoptimizer/stoptimizer.ts"
# Check each optimization
deno run --allow-net $TOOL_URL --model gpt-4o "args" > /tmp/old
deno run --allow-net $TOOL_URL --model gpt-4o "arguments" > /tmp/new
[ "$(cat /tmp/old)" -eq "$(cat /tmp/new)" ] || exit 1
~/.cache/deno/)0 - Success (token count returned)2 - Error (network failure, invalid model, missing argument)$ deno run --allow-net stoptimizer.ts "The quick brown fox"
5 5 5 5
$ deno run --allow-net stoptimizer.ts --json "params"
{"gpt-4o":1,"gpt-4":1,"gpt-3.5":1,"claude-3.5-sonnet":1}
$ deno run --allow-net stoptimizer.ts --json "parameters"
{"gpt-4o":1,"gpt-4":1,"gpt-3.5":1,"claude-3.5-sonnet":1}
# Both tokenize to 1 token - STOP compliant!
import { Tiktoken, getGPTVocab } from "./deps.ts";
const vocab = getGPTVocab("cl100k_base");
const tokenizer = new Tiktoken(
vocab.bpe_ranks,
vocab.special_tokens,
vocab.pat_str
);
const tokens = tokenizer.encode("hello world");
console.log(`Token count: ${tokens.length}`);
tokenizer.free();