Docs

Model APIs

Anthropic Messages and token estimates

Configure Anthropic-native requests, system instructions, content blocks, streaming and count_tokens.

Updated

POST/v1/messages
POST/v1/messages/count_tokens

Native Messages requests

POST /v1/messages uses Anthropic message structure. system is a top-level field, not a system role in messages. Set an explicit max_tokens output budget instead of relying on client or provider defaults.

cURL
curl --fail-with-body https://api.token4ai.cloud/v1/messages \
  -H "x-api-key: $TOKEN4AI_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  --data '{"model":"YOUR_MODEL_ID","max_tokens":256,"system":"Be concise.","messages":[{"role":"user","content":"Hello"}]}' 

Content blocks and tool turns

Response content is an array of blocks, not necessarily one text string. Read text from type:text blocks. For tool_use, execute an authorized tool and return the matching tool_result. Inspect stop_reason and usage instead of displaying the raw content array.

Set stream:true for native streaming. Its message and content-block lifecycle events differ from OpenAI choices[].delta. Use a protocol-specific parser.

Estimate input tokens

POST /v1/messages/count_tokens accepts the same family of Messages input and returns input_tokens. The gateway currently uses a local estimator rather than the provider’s exact tokenizer, and does not run generation. Use it for planning and reconcile final costs with actual usage records.

cURL
curl --fail-with-body https://api.token4ai.cloud/v1/messages/count_tokens \
  -H "Authorization: Bearer $TOKEN4AI_API_KEY" \
  -H "Content-Type: application/json" \
  --data '{
  "model": "YOUR_MODEL_ID",
  "messages": [
    {
      "role": "user",
      "content": "Estimate this input."
    }
  ]
}'

Related guides