Count tokens (LangChain - Anthropic format)

Counts tokens using Anthropic-compatible format via LangChain.

POST
/langchain/anthropic/v1/messages/count_tokens
AuthorizationBearer <token>

Bearer token authentication. Use your MPilot virtual-key JWT or admin JWT. Virtual keys (prefixed with sk-bf-) can also be passed here.

In: header

modelstring

Model identifier (e.g., claude-3-opus-20240229)

max_tokensinteger

Maximum tokens to generate

messagesarray<AnthropicMessage>

List of messages in the conversation

system?string | array<AnthropicContentBlock>

System prompt

cache_control?CacheControl

Automatic caching directives for the whole request

metadata?object
stream?boolean

Whether to stream the response

temperature?number
Range0 <= value <= 1
top_p?number
top_k?integer
stop_sequences?array<string>
tools?array<object>
tool_choice?object
mcp_servers?array<object>

MCP servers configuration (requires beta header)

thinking?object
output_format?object

Structured output format (requires beta header)

Empty Object

fallbacks?array<string>

Response Body

curl -X POST "https://loading/{AI_GATEWAY_URL}/langchain/anthropic/v1/messages/count_tokens" \  -H "Content-Type: application/json" \  -d '{    "model": "claude-3-opus-20240229",    "max_tokens": 0,    "messages": [      {        "role": "user",        "content": "string"      }    ]  }'
{
  "input_tokens": 0
}
{
  "type": "error",
  "error": {
    "type": "string",
    "message": "string"
  }
}
{
  "type": "error",
  "error": {
    "type": "string",
    "message": "string"
  }
}
On this page

On this page

No Headings