Skip to main content
Glama

count_tokens

Prevent token limit surprises by estimating token consumption locally before sending text to an LLM. Returns token, character, byte counts and cost estimate using byte-pair encoding compatible with modern models.

Instructions

Estimate the number of tokens a text will consume when sent to an LLM. Uses a byte-pair encoding approximation compatible with cl100k_base (GPT-4, Claude, and most modern models). Accurate to ±10% on English prose. Returns token count, character count, byte count, and a cost estimate footnote. Does NOT send text to any LLM — estimation is purely local. Has no side effects. Free. Use before sending long context to an LLM to avoid surprises. Do NOT use to count characters or words — use text_stats instead.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYesText to count tokens for.
modelNoModel name hint (default "cl100k_base"). Accepted: cl100k_base, gpt-4, gpt-4o, gpt-3.5-turbo, claude, claude-3, claude-sonnet, claude-haiku, claude-opus, text-embedding-ada-002. All currently use the same cl100k_base approximation.
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Discloses no side effects, purely local estimation, does not send to LLM, free, and accuracy range. No annotations provided, so description covers all behavioral aspects.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Concise, well-structured description with essential information front-loaded. Every sentence adds value.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given low complexity and complete schema coverage, description sufficiently covers return values and usage context. No output schema needed; description mentions returned fields.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, but description adds meaning by explaining the model parameter's accepted values and noting they all use the same approximation. Adds value beyond schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Clearly states the tool estimates token count for LLM consumption, specifying the algorithm and accuracy. Distinguishes from sibling text_stats which counts characters/words.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Explicitly states when to use (before sending long context) and when not to (for characters/words, use text_stats). Also clarifies no side effects and free.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/icosaedro-git/toolsnap-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server