44 "os"
55 "path/filepath"
66 "strings"
7+ "sync"
78
89 "github.com/agent-ecosystem/skill-validator/types"
910 "github.com/tiktoken-go/tokenizer"
@@ -26,6 +27,19 @@ const (
2627 otherTotalHardLimit = 100_000
2728)
2829
30+ var (
31+ encoderOnce sync.Once
32+ cachedEnc tokenizer.Codec
33+ encoderErr error
34+ )
35+
36+ func getEncoder () (tokenizer.Codec , error ) {
37+ encoderOnce .Do (func () {
38+ cachedEnc , encoderErr = tokenizer .Get (tokenizer .O200kBase )
39+ })
40+ return cachedEnc , encoderErr
41+ }
42+
2943// CheckTokens counts tokens for the SKILL.md body, reference files, asset files,
3044// and non-standard files. It returns validation results, standard token counts,
3145// and non-standard ("other") token counts.
@@ -34,7 +48,7 @@ func CheckTokens(dir, body string, opts Options) ([]types.Result, []types.TokenC
3448 var results []types.Result
3549 var counts []types.TokenCount
3650
37- enc , err := tokenizer . Get ( tokenizer . O200kBase )
51+ enc , err := getEncoder ( )
3852 if err != nil {
3953 results = append (results , ctx .Errorf ("failed to initialize tokenizer: %v" , err ))
4054 return results , counts , nil
0 commit comments