Docs / API reference

Count tokens API

Updated 2026-11-22. Published by PentestLLM.

Before a long report or a packet capture excerpt goes to a model, one route under https://api.pentestllm.ai/v1 tells you how many input tokens it will cost, for free and without the model seeing it.

Route

RouteStatuses
POST /v1/messages/count_tokens200, 400, 401, 413

It takes the ordinary key, in the header the Anthropic SDK sends or as a bearer token, and the optional anthropic-version header the SDK adds. The count is made on the gateway: about four characters a token, a few tokens per message and about 1,600 per image, erring high.

Body

The same shape as a Messages request. Anything else Anthropic accepts passes through and is ignored for counting.

FieldTypeNotes
modelstring, requiredThe model the real request would name; a 400 names it when missing.
messagesarray, requiredThe turns to size, in Anthropic shape.
systemstring or arrayA system prompt, counted with the turns.
toolsarrayTool definitions, counted with the turns.
max_tokensintegerAccepted as in a real request; it does not change the count.

Answer

A 200 carries one field, input_tokens, an integer.

Errors

StatusCodeWhen
400invalid_requestNo model, or messages is not a list.
401invalid_api_keyThe key is missing or not valid.
413invalid_requestThe body is too large to size.

Example

shell
1curl https://api.pentestllm.ai/v1/messages/count_tokens \
2 -H "x-api-key: sk-ptl-live-…" \
3 -H "anthropic-version: 2023-06-01" \
4 -H "Content-Type: application/json" \
5 -d '{"model": "<model id>", "messages": [{"role": "user", "content": "Summarise this nuclei report."}]}'

Questions

Does a count touch my balance or the usage log?

No. The gateway computes it without calling a model, so nothing is charged and no request record is written. The bill for the real call uses the count the model reports.

How close is the estimate?

It is built from character length, a small allowance per message and a fixed figure per image, and it rounds up. Read it as an upper estimate when you size a finding dump or a log excerpt, not as the exact number.

Does a body that sizes fine always run?

No. Sizing checks the prompt only. The real call still meets the key limits, the model availability of the moment and the usual request validation.

Join on TelegramHelp