Pure Go implementation of OpenAI's tiktoken tokenizer
Go
458
38 commits
updated Jul 17, 2026
This is a pure go port of OpenAI's tokenizer.
package main
import (
"fmt"
"github.com/tiktoken-go/tokenizer"
)
func main() {
enc, err := tokenizer.Get(tokenizer.Cl100kBase)
if err != nil {
panic("oh oh")
}
// this should print a list of token ids
ids, _, _ := enc.Encode("supercalifragilistic")
fmt.Println(ids)
// this should print the original string back
text, _ := enc.Decode(ids)
fmt.Println(text)
}
Alternatively you can use the included command-line tool
> tokenizer -h
Usage of tokenizer:
-decode string
tokens to decode
-encode string
text to encode
-token string
text to calculate token
> tokenizer -encode supercalifragilistic
This library embeds OpenAI's vocabularies—which are not small (~4Mb)— as go maps. This is different than what the way python version of tiktoken works, which downloads the dictionaries and puts them in a cache folder.
However, since the dictionaries are compiled during the go build process the performance and start-up times should be better than downloading and loading them at runtime.
Here is a list of other libraries that do something similar.
Overall
Maintained
Code review
Go module
Latest release
Releases this year
2,336 followers · starred Apr 2023
378 followers · starred Jul 2026
1,977 followers · starred Apr 2023
229 followers · starred Apr 2023
Pure Go implementation of OpenAI's tiktoken tokenizer
Go
458
38 commits
updated Jul 17, 2026
This is a pure go port of OpenAI's tokenizer.
package main
import (
"fmt"
"github.com/tiktoken-go/tokenizer"
)
func main() {
enc, err := tokenizer.Get(tokenizer.Cl100kBase)
if err != nil {
panic("oh oh")
}
// this should print a list of token ids
ids, _, _ := enc.Encode("supercalifragilistic")
fmt.Println(ids)
// this should print the original string back
text, _ := enc.Decode(ids)
fmt.Println(text)
}
Alternatively you can use the included command-line tool
> tokenizer -h
Usage of tokenizer:
-decode string
tokens to decode
-encode string
text to encode
-token string
text to calculate token
> tokenizer -encode supercalifragilistic
This library embeds OpenAI's vocabularies—which are not small (~4Mb)— as go maps. This is different than what the way python version of tiktoken works, which downloads the dictionaries and puts them in a cache folder.
However, since the dictionaries are compiled during the go build process the performance and start-up times should be better than downloading and loading them at runtime.
Here is a list of other libraries that do something similar.
Overall
Maintained
Code review
Go module
Latest release
Releases this year
2,336 followers · starred Apr 2023
378 followers · starred Jul 2026
1,977 followers · starred Apr 2023
229 followers · starred Apr 2023