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

# Anthropic-compatible token counting (POST /v1/messages/count_tokens).

> Estimate input tokens using the same request parser as the message endpoint. This count is an estimate, not a model generation or proof decision…

Estimate input tokens using the same request parser as the message endpoint. This count is an estimate, not a model generation or proof decision. The example uses a plain-text conversation; use the deployed message contract for complex content blocks.

### Headers

**Idempotency-Key** `string`

Retry-safe key. Replaying the same key with the same body returns the original response and sets `idempotent-replayed`; a different body with the same key is rejected 422. Use it so a retried write cannot double-execute or double-bill.

### Request body

`object`

**messages** `object[]` *required*

Conversation turns to count.

**messages**

**role** `string` *required*

**content** `string | object[]` *required*

**content**

**Option 1: string**

Value of type `string`.

**Option 2: object\[]**

Value of type `object[]`.

**model** `string,null`

Model identifier accepted by your deployment.

**system** `string | object[],null`

**system**

**Option 1: string**

Value of type `string`.

**Option 2: object\[]**

Value of type `object[]`.

**max\_tokens** `integer,null`

Minimum: `0`.

**tools** `any[]`

Default: `[]`.

**tool\_choice** `object,null`

**tool\_choice**

**type** `string` *required*

**name** `string,null`

**stop\_sequences** `string[]`

Default: `[]`.

**metadata** `object`

Opaque JSON metadata; defaults to null.

**temperature** `number,null`

**top\_p** `number,null`

**confidence\_threshold** `number,null`

**top\_k** `integer,null`

Minimum: `0`.

**max\_depth** `integer,null`

Minimum: `0`.

**max\_iterations** `integer,null`

Minimum: `0`.

**stream** `boolean,null`

**hydrate\_org\_context** `boolean,null`

**include\_nsr\_metadata** `boolean,null`

**kb\_limits** `RecursiveChatKbLimits,null`

**RecursiveChatKbLimits**

**max\_entities** `integer`

Minimum: `0`.

**max\_properties\_per\_entity** `integer`

Minimum: `0`.

**max\_rules** `integer`

Minimum: `0`.

**max\_triples** `integer`

Minimum: `0`.

**facts** `RecursiveChatFactInput[],null`

**RecursiveChatFactInput**

**confidence** `number (double)`

**predicate** `RecursiveChatPredicateInput` *required*

**RecursiveChatPredicateInput**

**args** `Arg[]` *required*

**name** `string` *required*

**negated** `boolean`

Negation-as-failure for a rule CONDITION: `not fraud_flagged(?o, ?r)`. Meaningful only on rule conditions (ignored on facts and conclusions). Defaulted and skipped-when-false so existing payloads are unchanged. Without this the negation was silently dropped and the condition read as POSITIVE — a fraud-flagged subject slipped a `not fraud_flagged` guard.

**source** `string`

**rules** `RecursiveChatRuleInput[],null`

**RecursiveChatRuleInput**

**category** `string`

**constraints** `any[]`

**description** `string`

**effect** `object`

**enabled** `boolean`

**id** `string`

**if** `RecursiveChatPredicateInput[]` *required*

**RecursiveChatPredicateInput**

**args** `Arg[]` *required*

**name** `string` *required*

**negated** `boolean`

Negation-as-failure for a rule CONDITION: `not fraud_flagged(?o, ?r)`. Meaningful only on rule conditions (ignored on facts and conclusions). Defaulted and skipped-when-false so existing payloads are unchanged. Without this the negation was silently dropped and the condition read as POSITIVE — a fraud-flagged subject slipped a `not fraud_flagged` guard.

**name** `string` *required*

**priority** `integer (int32)`

**then** `RecursiveChatPredicateInput[]` *required*

**RecursiveChatPredicateInput**

**args** `Arg[]` *required*

**name** `string` *required*

**negated** `boolean`

Negation-as-failure for a rule CONDITION: `not fraud_flagged(?o, ?r)`. Meaningful only on rule conditions (ignored on facts and conclusions). Defaulted and skipped-when-false so existing payloads are unchanged. Without this the negation was silently dropped and the condition read as POSITIVE — a fraud-flagged subject slipped a `not fraud_flagged` guard.

### Response

`CountTokensResponse`

**input\_tokens** `integer (int32)` *required*

Minimum: `0`.

### Status codes

| Code | Meaning |
| - | - |
| `200` | Estimated input token count |
| `401` | Unauthorized — missing or invalid credentials |
| `403` | Forbidden — the key/token lacks the required scope |
| `429` | Rate limited — see Retry-After / X-RateLimit-Reset |

### Using this contract

Read the [source OpenAPI document](https://docs.stateset.com/spec/nsr.json) for declared schemas and alternatives.
This page also includes documented corrections from the [spec overlays](https://github.com/stateset/docs/tree/main/spec/overlays).
Example IDs and values are illustrative; replace them with records from your workspace.

```bash cURL theme={null}
curl --silent --show-error --fail-with-body --request POST \
  --url 'https://api.nsr.stateset.com/v1/messages/count_tokens' \
  --header "X-API-Key: $STATESET_NSR_API_KEY" \
  --header "Idempotency-Key: ${IDEMPOTENCY_KEY:?Set a unique key for this operation; preserve it on retries}" \
  --header 'Content-Type: application/json' \
  --data '{
  "messages": [
    {
      "role": "user",
      "content": "Check the status of my order."
    }
  ]
}'
```

```json 200 theme={null}
{
  "input_tokens": 1
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.