RunarsRunars Docs
EndpointsApi endpoints

POST /v1/messages/count_tokens

Estimate input tokens without generating a response

Estimate the number of input tokens for a request without actually generating a response. This endpoint is useful for Claude Code and other tools to check if a request fits within the context window before sending it.

When to use

  • Check if your prompt + messages fit within the model's context window
  • Estimate cost before sending a request
  • Ensure Claude Code can complete a task without context overflow

Endpoint

POST https://api.runars.ca/v1/messages/count_tokens

Authentication

Include your API key:

-H "x-api-key: sk-runars-..."

Or use Bearer token:

-H "Authorization: Bearer sk-runars-..."

Request body

The request body is identical to /v1/messages except you must omit max_tokens:

FieldTypeRequiredDescription
modelstringYesModel identifier, must be "glm-5.2"
messagesarrayYesArray of message objects with role and content
systemstringNoSystem prompt/instructions
toolsarrayNoTool definitions (tool count affects token estimate)

Response

{
  "input_tokens": 42
}

That's it—just the estimated token count.

Important notes

  • This endpoint does not consume credits or count against rate limits.
  • The estimate is based on the same tokenizer used by the actual request, so it's accurate.
  • The count includes system prompt, messages, and any tool/function definitions.
  • Unlike /v1/messages, there's no stream parameter—this endpoint never streams.

Example

curl https://api.runars.ca/v1/messages/count_tokens \
  -H "x-api-key: sk-runars-..." \
  -H "content-type: application/json" \
  -d '{
    "model": "glm-5.2",
    "system": "You are a helpful assistant.",
    "messages": [
      {
        "role": "user",
        "content": "Explain quantum computing in 10000 tokens."
      }
    ]
  }'

Response:

{
  "input_tokens": 27
}

Using with Claude Code

Claude Code uses this endpoint to track context usage. If you're running Claude Code with Runars, it will automatically call this endpoint to estimate whether it has enough context remaining for the next step.

Using with the Anthropic SDK

The Anthropic SDK doesn't directly expose count_tokens, but you can call it via raw HTTP:

import Anthropic from "@anthropic-ai/sdk"

const anthropic = new Anthropic({
  baseURL: "https://api.runars.ca",
  apiKey: process.env.ANTHROPIC_API_KEY,
})

// Raw HTTP request
const response = await anthropic.request({
  method: "POST",
  path: "/v1/messages/count_tokens",
  body: {
    model: "glm-5.2",
    messages: [
      {
        role: "user",
        content: "Your prompt here",
      },
    ],
  },
})

console.log(response.input_tokens)

Or use the HTTP client directly:

const response = await fetch("https://api.runars.ca/v1/messages/count_tokens", {
  method: "POST",
  headers: {
    "x-api-key": process.env.ANTHROPIC_API_KEY,
    "content-type": "application/json",
  },
  body: JSON.stringify({
    model: "glm-5.2",
    messages: [
      {
        role: "user",
        content: "Your prompt here",
      },
    ],
  }),
})

const data = await response.json()
console.log(data.input_tokens)

Error responses

400 Bad Request

Missing required field:

{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "Missing required field: messages"
  }
}

401 Unauthorized

Invalid or missing API key:

{
  "type": "error",
  "error": {
    "type": "authentication_error",
    "message": "Invalid API key"
  }
}

Note: count_tokens doesn't return 402 errors since it doesn't consume credits.

On this page