Skip to main content
Glama

count_tokens

Counts tokens in text using model-specific tokenizers. Specify a model name to route between Anthropic/Claude and OpenAI/tiktoken for accurate token counts.

Instructions

Count tokens in text.

Routes by model name to Anthropic/Claude (default) or OpenAI/tiktoken.

Args:
    text: The text to tokenize.
    model: Model or encoding name (e.g. claude-opus-4-8, gpt-4, o200k_base).
        Empty uses ANTHROPIC_TOKEN_COUNT_MODEL.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYes
modelNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes
Install Server

TDQS

A4.2/5.0
Behavior4/5

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

No annotations are present, so the description carries the full burden. It discloses two meaningful behaviors not visible in the schema: tokenizer selection is routed by model name with an Anthropic default, and an empty model falls back to ANTHROPIC_TOKEN_COUNT_MODEL. This is useful context beyond a bare 'count tokens' statement, though it does not mention failure modes for unsupported model names.

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?

Three short sentences plus a compact Args list; every line adds information (purpose, routing, parameter behavior). The core purpose is front-loaded and no filler is present.

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 token-counting tool with an output schema present, the description covers purpose, parameter semantics, and routing. It does not explicitly address edge cases (e.g., unknown model, empty text), but the complexity is low enough that these omissions are not harmful.

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 description coverage is 0%, so the description must clarify the parameters, and it does. 'text' is explained only minimally, but 'model' is well specified with concrete examples (claude-opus-4-8, gpt-4, o200k_base) and the empty-string fallback behavior, adding real semantic value over the schema's bare type/default.

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 opens with a specific verb and resource ('Count tokens in text'), which directly differentiates it from the file/folder siblings by scoping to raw text. The model-routing clause adds useful specialization without making the purpose ambiguous.

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

Usage Guidelines3/5

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

The description implies its use case through 'in text' and the sibling names suggest file/folder alternatives, but it never explicitly states when to choose this tool over count_tokens_file/count_tokens_folder or mentions any exclusion criteria. The routing and default model info is model-selection guidance, not tool-selection guidance.

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

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/the-phase-space/tokenizer-mcp'

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