Tok is a Go library (github.com/GrayCodeAI/tok) that compresses prompts and
filters text to reduce LLM token costs by 60-90%. There is no standalone tok
binary — you call it from Go code, or use the CLI verbs exposed through
Hawk, which embeds this library.
go get github.com/GrayCodeAI/tokimport "github.com/GrayCodeAI/tok"in := "Hey, could you please help me figure out why this React component keeps " +
"re-rendering every time the props change?"
out, stats := tok.Compress(in, tok.Aggressive)
// out: "React component re-renders on prop change. Why?"
// stats: compression ratio, token savings, etc.PromptCompress lets you pick an intensity level directly:
out, stats := tok.PromptCompress(in, tok.IntensityFull)
// intensities: tok.IntensityLite, tok.IntensityFull, tok.IntensityUltracost := tok.EstimateCost(out, "claude-3-5-sonnet")Filter rules are TOML files loaded with tok.LoadFilterRules and applied via
tok.WithCustomFilters:
rules, err := tok.LoadFilterRules("rules.toml")
if err != nil {
log.Fatal(err)
}
out, _ := tok.Compress(in, tok.WithCustomFilters(rules))// Preserve code structure for a given language.
out, _ := tok.Compress(src, tok.WithCodeAware("go"))
// Drop the lowest-information tokens using your own scorer.
out, _ = tok.Compress(in, tok.WithPerplexityGuided(scorer, 0.5))profile, err := tok.LoadProfile("profile.toml")
out, _ := tok.Compress(in, profile.Options()...)det := tok.NewSecretDetector()
findings := det.Scan(text)
if tok.IsSensitiveFilename(".env") {
// skip or redact
}short := tok.SmartTruncate(longText, 500)
payload, err := tok.ExtractJSON(mixedOutput)tracker := tok.NewTracker(ctx)
// record and report token/cost usage over a runThe tok ... subcommands you may remember (compress, estimate, etc.) are
provided by Hawk, which embeds this library:
hawk tok compress < prompt.txt
hawk tok estimate < prompt.txtSee Hawk for the full command surface.
./benchmarks/run.sh
./evals/pipeline-bench.shBoth wrap go test -bench.
See the main README for full documentation.