Count message tokens (Anthropic Messages API)
package main
import ( "fmt" "strings" "net/http" "io")
func main() {
url := "https://api.ai.wpengine.com/v1/messages/count_tokens"
payload := strings.NewReader("{ \"model\": \"anthropic/claude-sonnet-4-6\", \"messages\": [ { \"role\": \"user\", \"content\": \"example\" } ], \"system\": \"example\" }")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>") req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close() body, _ := io.ReadAll(res.Body)
fmt.Println(res) fmt.Println(string(body))
}const url = 'https://api.ai.wpengine.com/v1/messages/count_tokens';const options = { method: 'POST', headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'}, body: '{"model":"anthropic/claude-sonnet-4-6","messages":[{"role":"user","content":"example"}],"system":"example"}'};
try { const response = await fetch(url, options); const data = await response.json(); console.log(data);} catch (error) { console.error(error);}<?php
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'https://api.ai.wpengine.com/v1/messages/count_tokens', [ 'body' => '{ "model": "anthropic/claude-sonnet-4-6", "messages": [ { "role": "user", "content": "example" } ], "system": "example" }', 'headers' => [ 'Authorization' => 'Bearer <token>', 'Content-Type' => 'application/json', ],]);
echo $response->getBody();curl --request POST \ --url https://api.ai.wpengine.com/v1/messages/count_tokens \ --header 'Authorization: Bearer <token>' \ --header 'Content-Type: application/json' \ --data '{ "model": "anthropic/claude-sonnet-4-6", "messages": [ { "role": "user", "content": "example" } ], "system": "example" }'Counts the input tokens a Messages request would consume, without creating a message or incurring model usage. Accepts the same request shape as /v1/messages minus max_tokens. Authentication matches /v1/messages: a WP Engine bearer token or an “x-api-key” header; API keys must carry the “chat:write” permission. Some models do not support token counting and return a 400 error.
Authorizations
Section titled “Authorizations”Request Bodyrequired
Section titled “Request Bodyrequired”Same shape as an Anthropic Messages request without max_tokens; system and tools are included in the count when supplied.
object
Example
anthropic/claude-sonnet-4-6object
Message content: a plain string, or an array of content blocks (text, image, tool_use, tool_result, thinking, …).
System prompt, as a string or array of content blocks.
Responses
Section titled “Responses”Token count for the supplied request
object
Tokens the supplied request would consume as input.
Example
{ "input_tokens": 14}Invalid request body or parameters, in Anthropic error envelope format (error type “invalid_request_error”).
Anthropic error envelope, as parsed by Anthropic SDKs.
object
object
Anthropic error type, e.g. “invalid_request_error”, “authentication_error”, “permission_error”, “not_found_error”, “request_too_large”, “rate_limit_error”, “overloaded_error”, “api_error”, or “content_policy_violation” (request content blocked by moderation policy when content moderation is enabled — never identifies which filter or category matched).
Correlation ID for this request, matching the X-Request-ID response header. Quote it in support reports.
Example
{ "type": "error", "error": { "type": "invalid_request_error", "message": "max_tokens is required and must be greater than zero", "request_id": "80f1e2c3a4b5c6d7e8f9a0b1c2d3e4f5" }}Missing or invalid credentials, in Anthropic error envelope format (error type “authentication_error”).
Anthropic error envelope, as parsed by Anthropic SDKs.
object
object
Anthropic error type, e.g. “invalid_request_error”, “authentication_error”, “permission_error”, “not_found_error”, “request_too_large”, “rate_limit_error”, “overloaded_error”, “api_error”, or “content_policy_violation” (request content blocked by moderation policy when content moderation is enabled — never identifies which filter or category matched).
Correlation ID for this request, matching the X-Request-ID response header. Quote it in support reports.
Example
{ "type": "error", "error": { "type": "invalid_request_error", "message": "max_tokens is required and must be greater than zero", "request_id": "80f1e2c3a4b5c6d7e8f9a0b1c2d3e4f5" }}Headers
Section titled “Headers”Example
Bearer realm="ai-services"Valid credentials but insufficient permission, in Anthropic error envelope format (error type “permission_error”). The same type is returned when billing isn’t set up for the account yet, because AI hasn’t been turned on for it in the User Portal or its setup hasn’t finished; the message says so. On /v1/messages, when content moderation is enabled, a 403 with error type “content_policy_violation” instead means the request content was blocked by content moderation policy; that response never identifies which filter or category matched.
Anthropic error envelope, as parsed by Anthropic SDKs.
object
object
Anthropic error type, e.g. “invalid_request_error”, “authentication_error”, “permission_error”, “not_found_error”, “request_too_large”, “rate_limit_error”, “overloaded_error”, “api_error”, or “content_policy_violation” (request content blocked by moderation policy when content moderation is enabled — never identifies which filter or category matched).
Correlation ID for this request, matching the X-Request-ID response header. Quote it in support reports.
Example
{ "type": "error", "error": { "type": "invalid_request_error", "message": "max_tokens is required and must be greater than zero", "request_id": "80f1e2c3a4b5c6d7e8f9a0b1c2d3e4f5" }}The requested model was not found or does not support this endpoint, in Anthropic error envelope format (error type “not_found_error”).
Anthropic error envelope, as parsed by Anthropic SDKs.
object
object
Anthropic error type, e.g. “invalid_request_error”, “authentication_error”, “permission_error”, “not_found_error”, “request_too_large”, “rate_limit_error”, “overloaded_error”, “api_error”, or “content_policy_violation” (request content blocked by moderation policy when content moderation is enabled — never identifies which filter or category matched).
Correlation ID for this request, matching the X-Request-ID response header. Quote it in support reports.
Example
{ "type": "error", "error": { "type": "invalid_request_error", "message": "max_tokens is required and must be greater than zero", "request_id": "80f1e2c3a4b5c6d7e8f9a0b1c2d3e4f5" }}Request body exceeds the size limit, in Anthropic error envelope format (error type “request_too_large”).
Anthropic error envelope, as parsed by Anthropic SDKs.
object
object
Anthropic error type, e.g. “invalid_request_error”, “authentication_error”, “permission_error”, “not_found_error”, “request_too_large”, “rate_limit_error”, “overloaded_error”, “api_error”, or “content_policy_violation” (request content blocked by moderation policy when content moderation is enabled — never identifies which filter or category matched).
Correlation ID for this request, matching the X-Request-ID response header. Quote it in support reports.
Example
{ "type": "error", "error": { "type": "invalid_request_error", "message": "max_tokens is required and must be greater than zero", "request_id": "80f1e2c3a4b5c6d7e8f9a0b1c2d3e4f5" }}Rate limit exceeded. The request was throttled at the edge; wait and retry later. The response body is a short HTML page generated by the load balancer, not a structured JSON error — clients should rely on the 429 status code rather than parsing the body.
Upstream provider returned an error, in Anthropic error envelope format (error type “api_error”).
Anthropic error envelope, as parsed by Anthropic SDKs.
object
object
Anthropic error type, e.g. “invalid_request_error”, “authentication_error”, “permission_error”, “not_found_error”, “request_too_large”, “rate_limit_error”, “overloaded_error”, “api_error”, or “content_policy_violation” (request content blocked by moderation policy when content moderation is enabled — never identifies which filter or category matched).
Correlation ID for this request, matching the X-Request-ID response header. Quote it in support reports.
Example
{ "type": "error", "error": { "type": "invalid_request_error", "message": "max_tokens is required and must be greater than zero", "request_id": "80f1e2c3a4b5c6d7e8f9a0b1c2d3e4f5" }}A required dependency (such as the authorization service) is temporarily unavailable, in Anthropic error envelope format (error type “overloaded_error”). Retry after a short delay with exponential back-off.
Anthropic error envelope, as parsed by Anthropic SDKs.
object
object
Anthropic error type, e.g. “invalid_request_error”, “authentication_error”, “permission_error”, “not_found_error”, “request_too_large”, “rate_limit_error”, “overloaded_error”, “api_error”, or “content_policy_violation” (request content blocked by moderation policy when content moderation is enabled — never identifies which filter or category matched).
Correlation ID for this request, matching the X-Request-ID response header. Quote it in support reports.
Example
{ "type": "error", "error": { "type": "invalid_request_error", "message": "max_tokens is required and must be greater than zero", "request_id": "80f1e2c3a4b5c6d7e8f9a0b1c2d3e4f5" }}default
Section titled “default”Upstream provider error relayed with its original status code and translated into Anthropic error envelope format. The error type reflects the status code (e.g. “rate_limit_error” for 429, “overloaded_error” for 529).
Anthropic error envelope, as parsed by Anthropic SDKs.
object
object
Anthropic error type, e.g. “invalid_request_error”, “authentication_error”, “permission_error”, “not_found_error”, “request_too_large”, “rate_limit_error”, “overloaded_error”, “api_error”, or “content_policy_violation” (request content blocked by moderation policy when content moderation is enabled — never identifies which filter or category matched).
Correlation ID for this request, matching the X-Request-ID response header. Quote it in support reports.
Example
{ "type": "error", "error": { "type": "invalid_request_error", "message": "max_tokens is required and must be greater than zero", "request_id": "80f1e2c3a4b5c6d7e8f9a0b1c2d3e4f5" }}