[ ABORT TO HUD ]
SEQ. 1
SEQ. 2

Files API & Token Counting

📎 Citations & Files API15 min600 BASE XP⌨ HANDS-ON LAB

Token Counting & File Management

Anthropic provides the /v1/messages/count_tokens endpoint to calculate the exact token consumption of a request (including system prompts, tools, images, and documents) before sending it for inference. This endpoint is free of charge and creates no Message objects.

const count = await anthropic.messages.count_tokens({
  model: "claude-3-7-sonnet-20250219",
  messages: [{ role: "user", content: "Your prompt here..." }],
  system: "Your system instructions..."
});
console.log("Input tokens:", count.input_tokens);
⌨ HANDS-ON LABCount Tokens Before You Spend
⭐ +150 XP

Your prompt might blow the context budget. Use the official /v1/messages/count_tokens endpoint to measure it for free - no tokens billed, no message created.

1POST to https://api.anthropic.com/v1/messages/count_tokens with curl (include the anthropic-version header).
lab-sandbox — simulated environment
INFINITY LAB SANDBOX v2.6 — simulated shell
Type the command for the current objective. Helpers: "hint", "solution", "clear".
$
OBJECTIVE 1 / 1 — type "hint" if stuck
SYNAPSE VERIFICATION
QUERY 1 // 1
What is the cost of calling the /v1/messages/count_tokens endpoint?
$0.01 per call
Standard input token rate
Free of charge - no tokens billed
Requires premium subscription