Skip to main content
Glama

count_tokens

Estimate token consumption of text for LLMs like GPT-4 and Claude to prevent unexpected costs. Returns token, character, and byte counts with a cost estimate.

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. Use before sending long context to an LLM to avoid surprises.

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.
Behavior4/5

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

With no annotations, the description fully discloses behavioral traits: it uses a byte-pair encoding approximation, is compatible with cl100k_base, accurate to ±10% on English prose, and returns token count, character count, byte count, and a cost estimate footnote. It does not discuss non-English accuracy or error handling.

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?

The description is concise (4 sentences), front-loaded with purpose, and each sentence adds value without redundancy.

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

Completeness4/5

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

For a simple tool with 2 parameters and no output schema, the description covers purpose, usage, return fields, and approximation details. It is nearly complete, though it lacks mention of error cases or performance on non-English text.

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

Parameters4/5

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

Schema coverage is 100%, so the description adds value beyond schema: it clarifies the model parameter as a hint, lists accepted values, and notes they all use the same approximation. The text param's description in schema is sufficient.

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?

The description clearly states the tool estimates the number of tokens for text sent to an LLM, using a specific verb (estimate) and resource (text). It distinguishes itself from siblings, none of which perform token counting.

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

Usage Guidelines4/5

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

The description advises using the tool before sending long context to avoid surprises, providing clear context. It does not explicitly mention when not to use or alternatives, but given no sibling does this, it's adequate.

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