Skip to content

Commit 8f6c4a0

Browse files
committed
feat: expose EstimateTokensFast/WithEncoding/ForModel at the top level
Add three top-level tok wrappers around the existing internal/core estimators: - tok.EstimateTokensFast(text) — fast heuristic, no BPE - tok.EstimateTokensWithEncoding(text, encoding) — BPE with explicit encoding (e.g. cl100k_base, o200k_base) - tok.EstimateTokensForModel(text, model) — BPE for a model family (e.g. gpt-4o, claude-3, gemini-2.5) These were available internally (core.EstimateTokensFast, core.EstimateTokensWithEncoding, core.EstimateTokensForModel) but not exposed at the public tok API. The rtk-style API surface expects these to be available for callers that want to track token counts before compression (e.g. to decide whether compression is worth doing at all). Source: rtk-ai/rtk estimate_tokens module. The functions are direct wrappers; no new logic. Tests: 2 cases covering all variants and empty-input behavior.
1 parent ec5d805 commit 8f6c4a0

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

estimate_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package tok_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/GrayCodeAI/tok"
7+
)
8+
9+
func TestEstimateTokens_AllVariants(t *testing.T) {
10+
in := "The quick brown fox jumps over the lazy dog"
11+
tests := []struct {
12+
name string
13+
fn func() int
14+
}{
15+
{"EstimateTokens", func() int { return tok.EstimateTokens(in) }},
16+
{"EstimateTokensFast", func() int { return tok.EstimateTokensFast(in) }},
17+
{"EstimateTokensPrecise", func() int { return tok.EstimateTokensPrecise(in) }},
18+
{"EstimateTokensWithEncoding_cl100k", func() int { return tok.EstimateTokensWithEncoding(in, "cl100k_base") }},
19+
{"EstimateTokensWithEncoding_o200k", func() int { return tok.EstimateTokensWithEncoding(in, "o200k_base") }},
20+
{"EstimateTokensForModel_gpt4o", func() int { return tok.EstimateTokensForModel(in, "gpt-4o") }},
21+
{"EstimateTokensForModel_claude", func() int { return tok.EstimateTokensForModel(in, "claude-3") }},
22+
}
23+
for _, tc := range tests {
24+
t.Run(tc.name, func(t *testing.T) {
25+
n := tc.fn()
26+
if n <= 0 {
27+
t.Errorf("expected positive token count, got %d", n)
28+
}
29+
})
30+
}
31+
}
32+
33+
func TestEstimateTokens_Empty(t *testing.T) {
34+
if n := tok.EstimateTokens(""); n != 0 {
35+
t.Errorf("expected 0 for empty string, got %d", n)
36+
}
37+
if n := tok.EstimateTokensFast(""); n != 0 {
38+
t.Errorf("expected 0 for empty string, got %d", n)
39+
}
40+
if n := tok.EstimateTokensPrecise(""); n != 0 {
41+
t.Errorf("expected 0 for empty string, got %d", n)
42+
}
43+
}

tok.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,32 @@ func EstimateTokens(text string) int {
3939
return core.EstimateTokens(text)
4040
}
4141

42+
// EstimateTokensFast provides a fast estimate without BPE.
43+
// Use this when exact count isn't critical (e.g. internal budget
44+
// checks before doing precise BPE work).
45+
func EstimateTokensFast(text string) int {
46+
return core.EstimateTokensFast(text)
47+
}
48+
4249
// EstimateTokensPrecise uses BPE tokenization (slower, more accurate).
4350
func EstimateTokensPrecise(text string) int {
4451
return core.EstimateTokensPrecise(text)
4552
}
4653

54+
// EstimateTokensWithEncoding uses BPE for a specific encoding
55+
// (e.g. "cl100k_base", "o200k_base", "p50k_base"). The encoding
56+
// must be one of the names accepted by the BPE tokenizer.
57+
func EstimateTokensWithEncoding(text string, encoding string) int {
58+
return core.EstimateTokensWithEncoding(text, encoding)
59+
}
60+
61+
// EstimateTokensForModel uses BPE for the encoding associated with
62+
// a specific model (e.g. "gpt-4o", "claude-3", "gemini-2.5"). See
63+
// WithModel for the supported model prefixes.
64+
func EstimateTokensForModel(text string, model string) int {
65+
return core.EstimateTokensForModel(text, model)
66+
}
67+
4768
// WarmupTokenizer pre-initializes the BPE tokenizer in the background.
4869
// Call at application startup to avoid latency on the first Compress call.
4970
func WarmupTokenizer() {

0 commit comments

Comments
 (0)