ttok
$ ttok Community Low Riskttok (simonw/ttok) is a fast Unix pipeline utility built on tiktoken that counts tokens from stdin or -i files and truncates text cleanly to a -t <tokens> budget. Also supports --encode, --decode, and --model tokenizer selection.
brew install ttok ttok enforces strict token budgets on logs, diffs, or files-to-prompt bundles in one local pipe before calling an LLM.
Agent-Readiness Checklist
Static evaluation (not yet probed on this host). Run npm run data:probe locally to capture live verification.
FCRS-1.0 6-Dimension Breakdown
Clean Unix stdin/stdout filter returning an exact integer or truncated text with -t, -i, -m, --encode, and --decode.
- Pure Unix stdin/stdout filter with
-t,-i,-m,--encode,--decode
100% read-only stateless tokenizer with zero filesystem writes or network calls (once tiktoken BPE cache exists).
- Stateless read-only tokenizer filter
Available via Homebrew, PyPI (pip), and uvx across macOS, Linux, and Windows.
- Published on Homebrew and PyPI (
simonw/ttok)
Solves exact token counting and -t truncation cleanly in shell scripts without writing Python boilerplate.
- One-line token budgeting and truncation in Unix pipes
Maintained by Simon Willison (simonw/ttok) under Apache-2.0.
- Active
simonw/ttokrepository
Crisp README and --help examples.
- Official README at github.com/simonw/ttok
Security, Auth & Guardrails
None; local stateless tokenizer pipeline utility.
100% free and Apache-2.0 open-source.
# ttok Agent Rules - Pipe large outputs through `ttok` first to check token size before loading into context. - Use `ttok -t <max_tokens>` to enforce hard token caps on dynamic command output. - Note `tiktoken` counts are exact for OpenAI encodings (`o200k_base`) and close approximations for Claude/Gemini.
Agent-Ready Command Recipes
Safe Read-Only Queries
ttok -i README.md
Prints the exact integer token count for a file using tiktoken.
git diff | ttok -t 4000
Trims piped text at exact token boundaries to fit a 4,000-token context budget.
Strengths for Agents
- + Exact
tiktoken-backed token counting and-ttruncation in Unix pipes. - + Zero-boilerplate companion to
files-to-prompt,git diff, and log streams. - + Supports
--encode,--decode, and--modeltokenizer selection.
Caveats & Pitfalls
- ! Uses OpenAI's
tiktokenencodings (o200k_baseetc.), so counts for Anthropic/Google models are close estimates rather than exact. - ! First run for a new encoding downloads BPE data into
tiktoken's local cache.