Install tokencount and count your first input.

Count the input tokens in a UTF-8 file, a pipe, or a literal string. The CLI sends the text directly to the selected model provider’s counting endpoint as one user input. Whitespace and trailing newlines are preserved. Anthropic describes its preflight count as an estimate that can differ slightly from the eventual message usage.

Install

macOS and Linux:

curl -fsSL https://tokencount.ashray.xyz/install.sh | sh

On Windows, run irm https://tokencount.ashray.xyz/install.ps1 | iex in PowerShell. Installers verify the release checksum. Use tokencount upgrade to update.

Count tokens

Set the environment variable for the provider you use: OPENAI_API_KEY, ANTHROPIC_API_KEY, or GEMINI_API_KEY (also accepts GOOGLE_API_KEY).

tokencount models
tokencount --model gpt-6.2-astra README.md
git diff | tokencount --model claude-opus-5-5
tokencount --model gemini-4-neon --text 'Hello, world!'
tokencount --model gpt-6-luna --json main.go

The default output is one integer. JSON includes model and input_tokens. Provider and input errors exit nonzero. Put options before the file path. For a filename beginning with a dash, use -- ./filename.