GitHub - samjoch/tok: Claude Tokens Counter · GitHub
/" data-turbo-transient="true" />
Skip to content
Search or jump to...
Search code, repositories, users, issues, pull requests...
-->
Search
Clear
Search syntax tips
Provide feedback
--><br>We read every piece of feedback, and take your input very seriously.
Include my email address so I can be contacted
Cancel
Submit feedback
Saved searches
Use saved searches to filter your results more quickly
-->
Name
Query
To see all available qualifiers, see our documentation.
Cancel
Create saved search
Sign in
/;ref_cta:Sign up;ref_loc:header logged out"}"<br>Sign up
Appearance settings
Resetting focus
You signed in with another tab or window. Reload to refresh your session.<br>You signed out in another tab or window. Reload to refresh your session.<br>You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
{{ message }}
samjoch
tok
Public
Notifications<br>You must be signed in to change notification settings
Fork
Star
main
BranchesTags
Go to file
CodeOpen more actions menu
Folders and files<br>NameNameLast commit message<br>Last commit date<br>Latest commit
History<br>1 Commit<br>1 Commit
src
src
.gitignore
.gitignore
Cargo.lock
Cargo.lock
Cargo.toml
Cargo.toml
README.md
README.md
View all files
Repository files navigation
tok
Exact Claude token counter for the terminal. Reuses your local Claude Code<br>login from the macOS Keychain — no ANTHROPIC_API_KEY required — and calls<br>Anthropic's official count_tokens endpoint, so the number matches what Claude<br>actually bills.
Build
cargo build --release<br># binary at target/release/tok (optionally: cp target/release/tok ~/.local/bin/)
Requires that Claude Code is installed and you've logged in at least once<br>(claude). The OAuth token is read from the Keychain entry<br>Claude Code-credentials.
Interactive mode (default)
Run with no input to open a Claude-Code-style field with a live count:
tok
Type or paste your prompt — the token count and estimated input cost<br>update as you edit (debounced).
Ctrl+V — smart paste: attaches an image if the clipboard holds one,<br>otherwise inserts text. (Plain ⌘V text paste also works.)
Pasted images appear inline as [Image 1], [Image 2], … just like Claude<br>Code. The placeholder is display-only — the image itself is counted as an<br>image block, the [Image N] text is not.
Ctrl+X — drop the last attached image · Ctrl+L — clear everything
Esc / Ctrl+C — quit
Pipe / scripting mode
Any text, file, or image argument switches to a single count-and-print:
16 tokens · ≈ $0.000240 input<br>echo "from a pipe" | tok # reads stdin<br>tok -q "just the number" # -> 11 (integer only, for scripts)<br>tok --file prompt.txt # count a file's contents<br>tok "describe this" -i shot.png # text + image (repeat -i for more)<br>tok -m claude-haiku-4-5 "hi" # price reflects the chosen model">tok "Hello, world!" # -> 16 tokens · ≈ $0.000240 input<br>echo "from a pipe" | tok # reads stdin<br>tok -q "just the number" # -> 11 (integer only, for scripts)<br>tok --file prompt.txt # count a file's contents<br>tok "describe this" -i shot.png # text + image (repeat -i for more)<br>tok -m claude-haiku-4-5 "hi" # price reflects the chosen model
The cost shown is the input cost (what it takes to send this prompt once)<br>at the model's per-token input price — count_tokens doesn't generate output,<br>so there's no output cost to include. Prices are approximate, matched by model<br>family (opus/sonnet/haiku).
Options
flag<br>meaning
-f, --file<br>read prompt text from a file
-i, --image<br>attach an image (png/jpg/gif/webp), repeatable
-m, --model<br>model to count against (counts are identical across Claude models)
-q, --quiet<br>print only the integer
--interactive<br>force the field even when input is piped
How it works
tok reads the claudeAiOauth.accessToken (scope user:inference) from the<br>Keychain and POSTs your text/images to<br>https://api.anthropic.com/v1/messages/count_tokens with the<br>anthropic-beta: oauth-2025-04-20 header. It only sends content for counting;<br>no completion is generated and nothing is billed for tokens.
If you see a 401, your login expired — run claude once to refresh it.
About
Claude Tokens Counter
Resources
Readme
Uh oh!
There was an error while loading. Please reload this page.
Activity
Stars
stars
Watchers
watching
Forks
forks
Report repository
Releases
No releases published
Packages
Uh oh!
There was an error while loading. Please reload this page.
Contributors
Uh oh!
There was an error while loading. Please reload this page.
Languages
Rust<br>100.0%
You can’t perform that action at this time.