-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken-counter.html
More file actions
171 lines (162 loc) · 5.94 KB
/
Copy pathtoken-counter.html
File metadata and controls
171 lines (162 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Token Counter — paste text, see the token count and what fits your model</title>
<meta name="description" content="Free browser-based token counter. Paste text or code, see the exact token count and how much of common LLM context windows it uses. No upload, runs entirely in your browser.">
<style>
:root {
--bg: #0b0d10;
--card: #14171c;
--text: #e8eaed;
--muted: #9aa0a6;
--accent: #6ee7b7;
--border: #262a30;
--code-bg: #0e1013;
--warn: #f59e0b;
--danger: #f87171;
}
@media (prefers-color-scheme: light) {
:root {
--bg: #f7f7f8;
--card: #ffffff;
--text: #16181c;
--muted: #5f6368;
--accent: #059669;
--border: #e2e4e8;
--code-bg: #f1f2f4;
--warn: #b45309;
--danger: #b91c1c;
}
}
* { box-sizing: border-box; }
body {
margin: 0;
background: var(--bg);
color: var(--text);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
display: flex;
justify-content: center;
padding: 40px 20px;
}
.card {
max-width: 760px;
width: 100%;
background: var(--card);
border: 1px solid var(--border);
border-radius: 16px;
padding: 32px;
}
h1 { font-size: 1.4rem; margin: 0 0 6px; }
p.sub { color: var(--muted); margin: 0 0 24px; font-size: 0.9rem; }
textarea {
width: 100%;
min-height: 260px;
background: var(--code-bg);
color: var(--text);
border: 1px solid var(--border);
border-radius: 10px;
padding: 14px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.85rem;
resize: vertical;
}
.stats {
display: flex;
gap: 24px;
margin: 16px 0 24px;
flex-wrap: wrap;
}
.stat { }
.stat .n { font-size: 1.6rem; font-weight: 700; }
.stat .l { font-size: 0.8rem; color: var(--muted); }
table {
width: 100%;
border-collapse: collapse;
font-size: 0.88rem;
}
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 600; font-size: 0.8rem; }
.bar-wrap { width: 140px; background: var(--code-bg); border-radius: 6px; overflow: hidden; height: 8px; }
.bar { height: 100%; background: var(--accent); }
.bar.warn { background: var(--warn); }
.bar.danger { background: var(--danger); }
.pct { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 0.8rem; }
.note {
font-size: 0.85rem;
color: var(--muted);
line-height: 1.5;
border-top: 1px solid var(--border);
padding-top: 20px;
margin-top: 24px;
}
a { color: var(--accent); }
</style>
</head>
<body>
<div class="card">
<h1>Token Counter</h1>
<p class="sub">Paste text or code below. Counted entirely in your browser — nothing is uploaded anywhere.</p>
<textarea id="input" placeholder="Paste text, code, a prompt, anything..."></textarea>
<div class="stats">
<div class="stat"><div class="n" id="tokenCount">0</div><div class="l">tokens</div></div>
<div class="stat"><div class="n" id="charCount">0</div><div class="l">characters</div></div>
<div class="stat"><div class="n" id="ratio">—</div><div class="l">chars / token</div></div>
</div>
<table>
<thead>
<tr><th>Model</th><th>Context window</th><th>Usage</th><th></th></tr>
</thead>
<tbody id="modelRows"></tbody>
</table>
<div class="note">
Uses <a href="https://www.npmjs.com/package/gpt-tokenizer">gpt-tokenizer</a> (cl100k encoding — the same tokenizer OpenAI's models use, and a close estimate for Claude and most other current models too). Context-window percentages already reserve 15% headroom for system prompt, conversation, and response — the same margin <a href="https://www.npmjs.com/package/llm-ctxpack">llm-ctxpack</a> uses when budgeting a repo pack.
<br><br>
Need this for a whole repo instead of pasted text? <a href="https://www.npmjs.com/package/llm-ctxpack">llm-ctxpack</a> does git-diff-aware, budget-aware repo packing with secret redaction built in: <code>npx llm-ctxpack . --budget 50000</code>.
</div>
</div>
<script src="tokenizer.bundle.js"></script>
<script>
const MODELS = [
{ name: 'Claude (Opus / Sonnet / Haiku)', window: 200000 },
{ name: 'GPT-4o / GPT-4.1 / GPT-4 Turbo', window: 128000 },
{ name: 'GPT-3.5', window: 16000 },
{ name: 'Gemini 1.5/2.x', window: 1000000 },
{ name: 'Llama 3.1/3.2/3.3', window: 128000 },
];
const HEADROOM = 0.15;
const input = document.getElementById('input');
const tokenCountEl = document.getElementById('tokenCount');
const charCountEl = document.getElementById('charCount');
const ratioEl = document.getElementById('ratio');
const modelRows = document.getElementById('modelRows');
function render() {
const text = input.value;
const chars = text.length;
let tokens = 0;
try {
tokens = text.length ? window.__tokenCounters.cl100k(text) : 0;
} catch (e) {
tokens = 0;
}
tokenCountEl.textContent = tokens.toLocaleString();
charCountEl.textContent = chars.toLocaleString();
ratioEl.textContent = tokens > 0 ? (chars / tokens).toFixed(2) : '—';
modelRows.innerHTML = MODELS.map(m => {
const usable = Math.floor(m.window * (1 - HEADROOM));
const pct = usable > 0 ? Math.min(100, (tokens / usable) * 100) : 0;
const barClass = pct >= 90 ? 'danger' : pct >= 60 ? 'warn' : '';
return `<tr>
<td>${m.name}</td>
<td>${m.window.toLocaleString()}</td>
<td><div class="bar-wrap"><div class="bar ${barClass}" style="width:${pct}%"></div></div></td>
<td class="pct">${pct.toFixed(1)}%</td>
</tr>`;
}).join('');
}
input.addEventListener('input', render);
render();
</script>
</body>
</html>