Prompt Token Analyzer

A Node.js CLI tool that analyzes prompt token usage using a GPT-compatible tokenizer. Helps agents estimate prompt size, debug context overflow, and optimize token cost.

Safety Notice

This listing is from the official public ClawHub registry. Review SKILL.md and referenced scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "Prompt Token Analyzer" with this command: npx skills add putixiaosheng/prompt-token-analyzer

Prompt Token Analyzer

Prompt Token Analyzer is a lightweight CLI tool that calculates how many tokens a prompt contains.
It uses the gpt-tokenizer package to approximate GPT-style tokenization.

This helps AI agents and developers:

  • estimate prompt size
  • reduce unnecessary token usage
  • debug large prompts
  • optimize RAG pipelines

Installation

Install the tokenizer:

npm install -g gpt-tokenizer

Create the CLI tool:

cat <<'EOF' > prompt-token
#!/usr/bin/env node

import { encode } from "gpt-tokenizer"
import fs from "fs"

const args = process.argv.slice(2)

if (args.length === 0) {
  console.log("Usage:")
  console.log("  prompt-token analyze <file>")
  console.log("  prompt-token text \"your prompt here\"")
  process.exit(1)
}

let text = ""

if (args[0] === "analyze") {
  const file = args[1]

  if (!file) {
    console.error("Missing file path")
    process.exit(1)
  }

  text = fs.readFileSync(file, "utf8")
}

else if (args[0] === "text") {
  text = args.slice(1).join(" ")
}

else {
  console.error("Unknown command")
  process.exit(1)
}

const tokens = encode(text)

console.log("Prompt Token Analysis")
console.log("---------------------")
console.log("Characters:", text.length)
console.log("Tokens:", tokens.length)
console.log("Average chars/token:", (text.length / tokens.length).toFixed(2))

const estimatedCost = tokens.length / 1000000 * 5

console.log("")
console.log("Estimated cost (example $5 / 1M tokens):")
console.log("$" + estimatedCost.toFixed(6))

EOF

Make the tool executable:

chmod +x prompt-token

Move it into PATH:

sudo mv prompt-token /usr/local/bin/

Quick Start

Analyze a prompt file:

prompt-token analyze prompt.txt

Example output:

Prompt Token Analysis
---------------------
Characters: 7341
Tokens: 1832
Average chars/token: 4.01

Estimated cost (example $5 / 1M tokens):
$0.009160

Analyze raw text

prompt-token text "Explain reinforcement learning in simple terms"

Example output:

Prompt Token Analysis
---------------------
Characters: 47
Tokens: 9
Average chars/token: 5.22

Use Cases

Prompt Engineering

Measure how prompt changes affect token size.

prompt-token text "You are an AI assistant..."

RAG Context Analysis

Check how large retrieved documents are before sending them to an LLM.

prompt-token analyze rag_context.txt

Debugging Context Overflow

Large prompts may exceed model limits.

Analyze them before sending to the model.


Troubleshooting

If the tokenizer is missing:

npm install -g gpt-tokenizer

Check Node installation:

node --version

Notes

  • Token counts are approximate but close to OpenAI-style tokenization.
  • Actual API usage may include additional system tokens.
  • Long RAG contexts are the most common cause of token waste.

Reporting Issues

Reinstall tokenizer if needed:

npm install -g gpt-tokenizer

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

GitHub Trending Skill

每日 GitHub Trending 热榜推送,支持日榜和月度汇总

Registry SourceRecently Updated
Coding

Proworkflow

ProWorkflow integration. Manage Clients, Staffs, Quotes, Templates, Messages, Groups. Use when the user wants to interact with ProWorkflow data.

Registry SourceRecently Updated
Coding

retarus-sms4a

Send SMS jobs and check SMS delivery status through the Retarus SMS for Applications REST API. Use when Codex or OpenClaw needs to create SMS jobs, inspect p...

Registry SourceRecently Updated
Coding

Remote Chrome CDP

Control Chrome/Chromium via CDP (Chrome DevTools Protocol) — open tabs, navigate URLs, take screenshots, execute JS. Supports local and remote machines via S...

Registry SourceRecently Updated