A new open-source terminal tool called Tok lets you count exact Claude tokens without needing an ANTHROPIC_API_KEY. Created by developer samjoch and shared on Hacker News, the utility reuses your existing Claude Code login stored in macOS Keychain to call Anthropic's official count_tokens endpoint—meaning you're getting the same numbers that actually appear on your bill. The project is built with Rust, compiles via cargo build --release, and requires only that Claude Code has been installed and authenticated at least once.

How It Works Under the Hood

Tok reads the OAuth token from the Keychain entry labeled "Claude Code-credentials" using the scope user:inference. It then POSTs your content directly to Anthropic's https://api.anthropic.com/v1/messages/count_tokens endpoint with the header anthropic-beta: oauth-2025-04-20. The tool only sends content for counting—no completion is generated, so nothing gets billed twice. If you encounter a 401 error, your login simply expired; running claude once refreshes it.

Interactive Mode

Running tok without arguments opens an interactive field with live token counting and cost estimation that updates as you type or paste (debounced). The tool supports smart pasting via Ctrl+V: if the clipboard contains an image, it's attached inline as [Image 1], [Image 2], etc., matching Claude Code's display format exactly. Plain text paste still works normally with ⌘V. Additional hotkeys include Ctrl+X to drop the last attached image and Ctrl+L to clear everything—Esc or Ctrl+C exits.

Scripting and Pipe Mode

For automation, tok accepts piped input like echo "from a pipe" | tok or direct string arguments like tok "Hello, world!", which outputs both token count and estimated input cost. The -q flag prints only the integer for script-friendly parsing. File counting works via --file prompt.txt, while images attach with repeatable -i shot.png flags. Model selection uses -m claude-haiku-4-5 to reflect pricing by model family (opus/sonnet/haiku).

Pricing Nuances

The cost shown represents input token pricing at the model's per-token rate—there's no output cost included since count_tokens doesn't generate completions. Prices are approximate and matched by model family, so opus queries show higher estimates than haiku even for identical content.

Key Takeaways

  • No API key management: Tok pulls auth from your existing Claude Code Keychain entry
  • Exact billing alignment: Calls Anthropic's official endpoint, not a third-party estimator
  • Dual interface: Interactive mode for manual use, pipe-friendly for scripting
  • Image support: Attachments count correctly as image blocks with inline display labels

The Bottom Line

This is the kind of utility that should ship with Claude Code itself. Token counting has always been opaque—developers guess or pay first and regret later. Tok eliminates that friction entirely by leveraging auth you already have, making cost estimation a zero-effort part of your workflow rather than an afterthought.