---
title: "Count tokens API: size a prompt before it runs"
description: "Estimate the input tokens of a Messages request with no model call and no charge. The gateway counts locally and rounds high, so budget a prompt before it runs."
canonical: https://pentestllm.ai/docs/count-tokens
generated: 2026-11-27T06:25:49.330Z
source: https://pentestllm.ai/docs/count-tokens
base_url: https://api.pentestllm.ai/v1
---

[Docs](https://pentestllm.ai/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

| Route | Statuses |
| --- | --- |
| `POST /v1/messages/count_tokens` | 200, 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.

| Field | Type | Notes |
| --- | --- | --- |
| `model` | string, required | The model the real request would name; a 400 names it when missing. |
| `messages` | array, required | The turns to size, in Anthropic shape. |
| `system` | string or array | A system prompt, counted with the turns. |
| `tools` | array | Tool definitions, counted with the turns. |
| `max_tokens` | integer | Accepted as in a real request; it does not change the count. |

## Answer

A 200 carries one field, `input_tokens`, an integer.

## Errors

| Status | Code | When |
| --- | --- | --- |
| `400` | invalid_request | No model, or messages is not a list. |
| `401` | invalid_api_key | The key is missing or not valid. |
| `413` | invalid_request | The body is too large to size. |

## Example

```bash
curl https://api.pentestllm.ai/v1/messages/count_tokens \
  -H "x-api-key: sk-ptl-live-…" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -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.

## Read next

- [MCP servers](https://pentestllm.ai/docs/mcp-servers)
- [Log export](https://pentestllm.ai/docs/exports)
- [Invoices](https://pentestllm.ai/docs/invoices)
- [Billing details](https://pentestllm.ai/docs/billing-details)
- [Statement signing key](https://pentestllm.ai/docs/statements-public-key)
- [Keys and scopes](https://pentestllm.ai/docs/keys)
- [Usage and signed receipts](https://pentestllm.ai/docs/usage-and-receipts)
- [Spend limits and balance](https://pentestllm.ai/docs/spend-limits)
- [Redaction and guardrails](https://pentestllm.ai/docs/redaction-and-guardrails)
- [Sessions](https://pentestllm.ai/docs/sessions)
- [Embeddings, rerank and moderation](https://pentestllm.ai/docs/retrieval-and-moderation)
- [Batch files](https://pentestllm.ai/docs/files)
- [Batches](https://pentestllm.ai/docs/batches)
- [Status and proof feeds](https://pentestllm.ai/docs/status-and-proof)
- [Usage by app](https://pentestllm.ai/docs/stats-apps)
- [App sign-in keys](https://pentestllm.ai/docs/auth-keys)
- [Webhook deliveries](https://pentestllm.ai/docs/webhooks-deliveries)
- [Docs: connect in two minutes](https://pentestllm.ai/docs)
- [Price a call before sending](https://pentestllm.ai/docs/spend-limits)
