Skip to content

CLI

Terminal window
tokcost [options] [files...]
cat file.md | tokcost [options]

tokcost counts the tokens in one or more files, or in text piped on stdin, prints the count along with the model and encoding it used, and (with --cost) estimates the input dollars. With no input it prints an error and exits non-zero.

tokcost gathers its input in one of two ways:

  • Files — every positional argument is read as a file path. Pass one file for a single count, or several for per-file counts plus a combined total.
  • Stdin — when no files are given and stdin is piped (not a TTY), tokcost reads all of stdin and counts it as <stdin>.

If you pass no files and stdin is a terminal, tokcost exits with an error telling you to pass a file or pipe text in.

FlagDescription
-m, --model <name>Model to count for (default gpt-4o). Selects the tiktoken encoding.
--costEstimate input $ using the built-in approximate price table.
--jsonOutput machine-readable JSON.
-h, --helpShow help and exit.
-v, --versionShow the version and exit.

The model name selects the encoding — see Models & pricing for the full mapping. The encoding actually used is always printed, so the number is traceable.

Terminal window
tokcost prompt.md
Terminal window
[1m42[0m tokens [2m(prompt.md)[0m
[2mmodel: gpt-4o encoding: o200k_base[0m

Pass several files to get a tab-separated count per file plus a total line:

Terminal window
tokcost system.md examples.md tools.md
Terminal window
[1m214[0m system.md
[1m1840[0m examples.md
[1m96[0m tools.md
[1m2150[0m total
[2mmodel: gpt-4o encoding: o200k_base[0m

The total is the sum of every file’s tokens. When you add --cost, the estimate is computed against that combined total.

Terminal window
tokcost --cost -m gpt-4o prompt.md
Terminal window
[1m42[0m tokens [2m(prompt.md)[0m
[2mmodel: gpt-4o encoding: o200k_base[0m
[2m≈[0m [1m$0.0001[0m (input)[2m — approximate, editable pricing[0m

When the model isn’t in the pricing table, the cost line instead reads:

Terminal window
[2mcost unavailable — model 'davinci' is not in the pricing table[0m

--json emits a structured object and suppresses the colored human output:

Terminal window
tokcost --json prompt.md
{
"model": "gpt-4o",
"encoding": "o200k_base",
"files": [{ "name": "prompt.md", "tokens": 42 }],
"total": 42
}

With --cost, a cost object is added. usd and pricePerMTokUsd are null when the model isn’t priced:

{
"model": "gpt-4o",
"encoding": "o200k_base",
"files": [{ "name": "prompt.md", "tokens": 42 }],
"total": 42,
"cost": {
"available": true,
"usd": 0.000105,
"pricePerMTokUsd": 2.5,
"note": "approximate input pricing; edit the table to suit your needs"
}
}
FieldTypeNotes
modelstringThe model name that was requested.
encodingstringThe tiktoken encoding actually used.
files[]arrayOne { name, tokens } per input (or a single <stdin> entry).
totalnumberSum of every file’s tokens.
costobject?Present only with --cost. See above.
CodeMeaning
0Success.
2Error — e.g. an unreadable file, an invalid flag, or no input.

A 0 / 2 split makes tokcost easy to wire into scripts: any non-zero exit is a hard failure (bad model name, missing file, empty stdin), not an over-budget signal.

Count a file with the default model:

Terminal window
tokcost prompt.md

Count several files at once and read the total:

Terminal window
tokcost system.md examples.md tools.md

Estimate the input cost for a Claude prompt (approximate encoding):

Terminal window
tokcost --cost -m claude-3.5-sonnet prompt.md

Pipe stdin and parse the result with jq:

Terminal window
echo "hello world" | tokcost --json | jq .total

Count for an older OpenAI model that uses cl100k_base:

Terminal window
tokcost -m gpt-4 prompt.md