{"uid":"cap_78eHLs1V3MZd5CbCdqLRW","slug":"agents-llmapi-ai-count-tokens-in-messages-llm-api-ac2ad1d9","name":"Count Tokens in Messages (LLM API)","description":"Pre-flight token count for an Anthropic style request. Free.","url":"https://agents.llmapi.ai/zc/v1/messages/count_tokens?utm_source=zero.xyz","method":"POST","headers":{},"bodySchema":{"type":"object","example":{"model":"claude-opus-5","messages":[{"role":"user","content":"hello"}]},"required":["model","messages"],"properties":{"model":{"enum":["claude-opus-5","claude-sonnet-5","gpt-5.2","gpt-5.1","gpt-5-mini","gemini-3.1-pro-preview","gemini-3.7-flash","deepseek-v4-pro","deepseek-v4-flash","grok-4.6","kimi-k2.6","glm-5.3"],"type":"string","description":"One of the storefront model ids."},"messages":{"type":"array","items":{"type":"object"}}}},"responseSchema":{"type":"object","properties":{"count":{"type":"integer"}}},"example":null,"exampleRequest":null,"tags":[],"displayCostAmount":"unknown","displayCostAsset":"USD","priceDynamic":true,"priceHint":null,"priceStatus":"unknown_unparsed","priceSource":"registry","requiresHandshake":false,"reviewCount":0,"rating":{"score":"0.00","successRate":"0.00","reviews":0,"stars":null,"state":"unrated"},"availabilityStatus":"unknown","priceObserved":null,"sessionDeposit":null,"pricing":{"kind":"unknown","summary":"price unknown","primary":{"kind":"dynamic","protocol":"mpp","network":"tempo","amountUsd":null,"per":"call","confidence":"unknown"},"accepted":[{"kind":"dynamic","protocol":"mpp","network":"tempo","amountUsd":null,"per":"call","confidence":"unknown"}]},"paymentMethods":[{"uid":"pm_gvDDshUH2KzHWeLaUQvpi","protocol":"mpp","methodType":"crypto","chain":"tempo","mode":"charge","costAmount":"unknown","costPer":"request","priority":0,"asset":null,"unit":"request","depositMicros":null,"planRef":null}],"brandName":"agents.llmapi.ai","brandSlug":"agents-llmapi-ai-x402","brandBaseUrl":"https://agents.llmapi.ai","brandDocsUrl":null,"whatItDoes":"Returns the token count for a given set of messages against a specified model, without generating a response — free of charge.","exampleAgentPrompt":"Before I send this conversation to claude-opus-5, can you check how many tokens it will use? The messages are: [{\"role\":\"user\",\"content\":\"Explain quantum entanglement in simple terms\"}]","exampleUseCases":[{"title":"Context window overflow prevention","prompt":"I'm about to send a long conversation history to claude-sonnet-5 — can you count the tokens first to make sure it won't exceed the context limit? Here are the messages: [{\"role\":\"user\",\"content\":\"Let's discuss the history of the Roman Empire...\"}]"},{"title":"Cost estimation before inference","prompt":"Before I run this batch of messages through gpt-5.1, count the tokens so I can estimate how much it'll cost. Messages: [{\"role\":\"system\",\"content\":\"You are a helpful assistant.\"},{\"role\":\"user\",\"content\":\"Summarize the last 10 years of AI research.\"}]"},{"title":"Prompt optimization comparison","prompt":"I've rewritten my system prompt to be more concise — can you count the tokens for deepseek-v4-pro on both versions so I can see how much I saved? Here's the new version: [{\"role\":\"system\",\"content\":\"Be concise. Answer only what is asked.\"},{\"role\":\"user\",\"content\":\"What is 2+2?\"}]"}],"resultDescription":"Returns a JSON object with a single integer field `count` representing the total number of tokens the provided messages would consume for the specified model — no response text is generated.","failureModes":["Invalid model identifier returns a validation error — must be one of the supported enum values","Malformed messages array (missing role or content fields) causes a 400 error","Empty messages array may return zero or trigger a validation error","Model-specific tokenization differences mean counts are model-specific and not interchangeable across models"],"whenToPreferThis":"Use this endpoint when you need to check token usage before committing to an inference call — ideal for context window management, cost estimation, or prompt optimization. It is free, so it should be called proactively whenever there is uncertainty about whether a prompt fits within a model's context window. Prefer this over estimating tokens client-side, as tokenization rules differ per model and this endpoint uses the exact same tokenizer as the inference endpoint.","instructions":null,"reviewSummary":null,"reviewSummaryHighlights":null,"reviewSummaryConcerns":null,"reviewSummaryGeneratedAt":null,"activationCount":0,"lastUsedAt":null,"lastSuccessfullyRanAt":null,"lastHealthCheckAt":"2026-10-17T18:39:59.138Z","isFirstParty":false,"canonicalSlug":"agents-llmapi-ai-count-tokens-in-messages-llm-api-ac2ad1d9"}