› 01 · Tokens › Capstone · Token-Aware Toolkit
🛠️ Capstone Project #charan #capstone

Token-Aware Toolkit

Four small tools that turn "words ≠ tokens" into something you can measure: count tokens and cost, compare τ across text types, check a prompt against a context window, and size an embedding matrix in memory.

Estimate vs exact. This page runs in the browser, so tools 1 and 3 use a rough GPT-style token estimate, not the real tokenizer. Tool 2 uses real GPT-4 (cl100k_base) counts measured with tiktoken. Every tool has the exact Python version underneath it — run that for production numbers.
🔬 Project Tokens · Module 01 4 tools tiktoken · Python
🔢 Token Counter

Paste any text. Get word count, estimated token count, τ = tokens ÷ words, and the API cost at a price per million tokens.

0
Words
0
Tokens (est.)
—
τ tok/word
$0
Cost / request
$0
Cost × requests
🐍 Python CLI with real tiktoken
"""token_counter.py  —  python token_counter.py "your text" --price 30 --requests 1000"""
import argparse
import tiktoken

parser = argparse.ArgumentParser()
parser.add_argument("text")
parser.add_argument("--model", default="gpt-4")
parser.add_argument("--price", type=float, default=30.0, help="USD per 1M tokens")
parser.add_argument("--requests", type=int, default=1)
args = parser.parse_args()

enc = tiktoken.encoding_for_model(args.model)
tokens = len(enc.encode(args.text))
words = len(args.text.split())
tau = tokens / words if words else 0.0
cost = tokens / 1_000_000 * args.price

print(f"Words:        {words}")
print(f"Tokens:       {tokens}")
print(f"τ:            {tau:.2f}")
print(f"Cost/request: ${cost:.6f}")
print(f"Cost x {args.requests}: ${cost * args.requests:.4f}")
📊 τ Ratio Visualizer

Ten samples run through the real GPT-4 tokenizer (cl100k_base). Switch the metric to see why whitespace-based τ breaks down for scripts without spaces.

🐍 Python script that produced these numbers
"""tau_visualizer.py  —  pip install tiktoken matplotlib"""
import tiktoken
import matplotlib.pyplot as plt

SAMPLES = {
    "English prose": "The quick brown fox jumps over the lazy dog while the sun sets slowly behind the hills.",
    "Technical jargon": "Hyperparameter optimization of transformer architectures requires backpropagation through heterogeneous embeddings.",
    "Python code": "def add(a, b):\n    return a + b\n\nprint(add(2, 3))",
    "JSON": '{"id": 42, "name": "Ada", "tags": ["ai", "ml"], "active": true}',
    "Spanish": "El rápido zorro marrón salta sobre el perro perezoso mientras el sol se pone detrás de las colinas.",
    "German": "Die Donaudampfschifffahrtsgesellschaft veröffentlichte gestern ihre Jahresbilanz.",
    "Chinese": "今天天气很好,我们一起去公园散步吧。",
    "Hindi": "आज मौसम बहुत अच्छा है, चलो पार्क में टहलने चलते हैं।",
    "Emoji": "😀🎉🚀🔥👍🌍💡🙌",
    "Numbers": "3.14159265358979 2718281828 1234567890 0.000123",
}

enc = tiktoken.encoding_for_model("gpt-4")
taus = {}
for name, text in SAMPLES.items():
    tokens = len(enc.encode(text))
    words = len(text.split())
    taus[name] = tokens / words
    print(f"{name:18} tokens={tokens:3} words={words:3} chars={len(text):3} tau={taus[name]:.2f}")

print("highest:", max(taus, key=taus.get), "| lowest:", min(taus, key=taus.get))
plt.barh(list(taus), list(taus.values()))
plt.xlabel("τ = tokens / word")
plt.tight_layout()
plt.show()
📏 Prompt Budget Estimator

Will the system prompt + user message + reserved reply fit in the model's context window? Measured in tokens, not words.

0
System tokens
0
User tokens
0
Context window
0
Tokens left
■ system   ■ user   ■ reserved reply
🐍 Python function with real tiktoken
import tiktoken

CONTEXT_WINDOWS = {
    "gpt-4": 8_192,
    "gpt-4-turbo": 128_000,
    "gpt-4o": 128_000,
}

def fits_in_context(system_prompt: str, user_message: str,
                    model: str = "gpt-4", reserve_for_reply: int = 512) -> dict:
    enc = tiktoken.encoding_for_model(model)
    used = len(enc.encode(system_prompt)) + len(enc.encode(user_message))
    window = CONTEXT_WINDOWS[model]
    left = window - reserve_for_reply - used
    return {"used": used, "window": window, "left": left, "fits": left >= 0}

print(fits_in_context("You are a helpful assistant.", "Hello!"))
Chat APIs add a few tokens of formatting overhead per message, so leave a small safety margin.
🔬 VRAM Weight Calculator

The embedding matrix has V × dmodel parameters. Memory = parameters × bytes per parameter.

0
Parameters (V × d)
🐍 Python CLI
"""vram_calc.py  —  python vram_calc.py 50257 768"""
import sys

BYTES = {"FP32": 4, "FP16": 2, "INT8": 1}

vocab, d_model = int(sys.argv[1]), int(sys.argv[2])
params = vocab * d_model
print(f"Embedding parameters: {params:,}")
for name, size in BYTES.items():
    mib = params * size / 1024**2
    print(f"{name}: {mib:,.1f} MiB")
Embedding matrix only. Activations, the KV cache, and the rest of the model's weights need additional memory.