> ## Documentation Index
> Fetch the complete documentation index at: https://docs.openp.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Count Tokens

> POST /v1/messages/count_tokens —— 不计费地预估 token 数

在真正调用 `/v1/messages` 前预估 token 数,用于:

* 提前判断是否超出上下文窗口。
* 在批量任务中精准预估成本。
* 在客户端展示 token 数。

## 请求

```bash theme={null}
curl https://openp.ai/v1/messages/count_tokens \
  -H "x-api-key: $OPENPAI_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-opus-4-8",
    "messages": [{"role": "user", "content": "hello"}]
  }'
```

字段与 `/v1/messages` 相同(可包含 `system` / `tools` / `messages`),
**无需** 提供 `max_tokens` 等生成参数。

## 响应

```json theme={null}
{ "input_tokens": 9 }
```

## 计费

`count_tokens` 接口 **不消耗模型配额**,但仍会按 OpenPAI 网关侧 RPM 限流。
