Prompt Token Counter (toksum)
First load reminder: This skill provides the scripts CLI (toksum). Use it when the user asks to count tokens, estimate API costs, or audit OpenClaw component token consumption (memory, persona, skills).
Before Installing — Security & Privacy
- - What will be read: The audit workflow reads files under
~/.openclaw/workspace and ~/.openclaw/skills (AGENTS.md, SOUL.md, MEMORY.md, SKILL.md, etc.). Those files may contain personal data or secrets. Only install if you accept that access. - URL fetching: The CLI can fetch HTTP(S) URLs via
-u. SKILL.md requires the agent to confirm each URL with the user before fetching. Insist the agent follow that rule; never allow automatic fetching of unknown URLs. - Source verification: Source: https://github.com/Zhaobudaoyuema/prompt-token-counter. Review
scripts/core.py and scripts/cli.py before use. The code performs local file reads and optional HTTP GETs only; no other network calls or data exfiltration. - Run locally first: If unsure, run the CLI manually in an isolated environment against safe test files to verify behavior.
Primary Use: OpenClaw Token Consumption Audit
Goal: Help users identify which OpenClaw components consume tokens and how much.
1. Memory & Persona Files
These files are injected into sessions and consume tokens. Search and count them:
| File | Purpose | Typical Location |
|---|
| INLINECODE6 | Operating instructions, workflow, priorities | INLINECODE7 |
| INLINECODE8 |
Persona, tone, values, behavioral guidelines |
~/.openclaw/workspace/ |
|
IDENTITY.md | Name, role, goals, visual description |
~/.openclaw/workspace/ |
|
USER.md | User preferences, communication style |
~/.openclaw/workspace/ |
|
MEMORY.md | Long-term memory, persistent facts |
~/.openclaw/workspace/ |
|
TOOLS.md | Tool quirks, path conventions |
~/.openclaw/workspace/ |
|
HEARTBEAT.md | Periodic maintenance checklist |
~/.openclaw/workspace/ |
|
BOOT.md | Startup ritual (when hooks enabled) |
~/.openclaw/workspace/ |
|
memory/YYYY-MM-DD.md | Daily memory logs |
~/.openclaw/workspace/memory/ |
Workspace path: Default ~/.openclaw/workspace; may be overridden in ~/.openclaw/openclaw.json via agent.workspace.
2. Skill Files (SKILL.md)
Skills are loaded per session. Count each SKILL.md:
| Location | Scope |
|---|
| INLINECODE28 | OpenClaw managed skills |
| INLINECODE29 |
Workspace-specific skills (override) |
3. Audit Workflow
- 1. Locate workspace: Resolve
~/.openclaw/workspace (or config override). - Collect files: List all memory/persona files and
SKILL.md paths above. - Count tokens: Run
python -m scripts.cli <path1> <path2> ... -m <model> -c (batch mode). - Summarize: Group by category (memory, persona, skills), report total and per-file.
Example audit command (PowerShell):
CODEBLOCK0
Example audit (Bash):
WS=~/.openclaw/workspace
python -m scripts.cli -m gpt-4o -c "$WS/AGENTS.md" "$WS/SOUL.md" "$WS/USER.md" "$WS/IDENTITY.md" "$WS/MEMORY.md" "$WS/TOOLS.md"
Project Layout
CODEBLOCK2
Invoke: python -m scripts.cli from project root.
Version Sync (publish_npm.py)
When publishing to npm, publish_npm.py bumps the patch version and syncs it to:
- -
package.json — INLINECODE36 - INLINECODE37 — frontmatter INLINECODE38
- INLINECODE39 — INLINECODE40
Run: python publish_npm.py (after npm login).
Runtime Dependencies
- - Python 3 — required
- tiktoken (optional) —
pip install tiktoken for exact OpenAI counts
Language Rule
Respond in the user's language. Match the user's language (e.g. Chinese if they write in Chinese, English if they write in English).
URL Usage — Mandatory Agent Rule
Before using -u / --url to fetch content from any URL, you MUST:
- 1. Explicitly warn the user that the CLI will make an outbound HTTP/HTTPS request to the given URL.
- Confirm the URL is trusted — tell the user: "Only use URLs you fully trust. Untrusted URLs may expose your IP, leak data, or be used for SSRF. Do you confirm this URL is safe?"
- Prefer alternatives — if the user can provide the content via
-f (local file) or inline text, suggest that instead of URL fetch. - Never auto-fetch — do not invoke
-u without the user having explicitly provided the URL and acknowledged the risk.
If the user insists on using a URL: Proceed only after they confirm. State clearly: "I will fetch from [URL] to count tokens. Proceed?"
Model Name — Mandatory Agent Rule
Before invoking the CLI, you MUST have a concrete model name from the user.
- 1. Require explicit model —
-m / --model is required. Do not guess or assume; the user must provide the exact name (e.g. gpt-4o, claude-3-5-sonnet-20241022). - If unclear, ask — if the user says "GPT" or "Claude" or "the latest model" without a specific name, ask: "Please specify the exact model name (e.g. gpt-4o, claude-3-5-sonnet-20241022). Run
python -m scripts.cli -l to list supported models." - Do not auto-pick — never substitute a model on behalf of the user without their confirmation.
- Validate when possible — if the model name seems ambiguous, offer
-l output or confirm: "I'll use [model]. Is that correct?"
CLI Usage
Default: Read from local file(s). No segmentation. Supports multiple file paths for batch execution.
CODEBLOCK3
| Option | Short | Description |
|---|
| INLINECODE52 | INLINECODE53 | Model name (required unless --list-models) — Agent must obtain exact name from user; ask if unclear |
| INLINECODE55 |
-f | Read from file (repeatable) |
|
--url |
-u | Read from URL (repeatable) —
Agent must warn user before use; only trusted URLs |
|
--list-models |
-l | List supported models |
|
--cost |
-c | Show cost estimate |
|
--output-tokens | | Use output token pricing |
|
--currency | | USD or INR |
|
--verbose |
-v | Detailed output |
Examples
CODEBLOCK4
Python API
CODEBLOCK5
Supported Models
300+ models across 34+ providers: OpenAI, Anthropic, Google, Meta, Mistral, Cohere, xAI, DeepSeek, etc. Use python -m scripts.cli -l for full list.
- - OpenAI: exact via tiktoken
- Others: ~85–95% approximation
Response Output — Agent Guideline
After returning token count or cost estimate results, the agent MUST:
- 1. Include the project link — e.g.
> Source:
prompt-token-counter
- 2. Briefly explain how tokens are calculated — e.g.
>
How tokens are counted: OpenAI models use tiktoken (exact). Other models use provider-specific formulas calibrated from benchmark data. For CJK-heavy text, the ratio is blended by CJK character ratio so that Chinese gets fewer chars per token.
Common Issues
| Issue | Action |
|---|
| "tiktoken is required" | INLINECODE68 |
| UnsupportedModelError |
Use
-l for valid names |
| Cost "NA" | Model has no pricing; count still valid |
| User provides URL |
Agent must warn: outbound request, SSRF risk, only trusted URLs; confirm before
-u |
| Model unclear / vague |
Agent must ask: user to specify exact model name; offer
-l to list; do not guess |
When to Trigger This Skill
Activate this skill when the user:
| Trigger | Example phrases |
|---|
| Token count | "How many tokens?", "Count tokens in this prompt", "Token length of X" |
| Cost estimate |
"Estimate API cost", "How much for this text?", "Cost for GPT-4" |
|
Prompt size | "Check prompt length", "Is this too long?", "Context window limit" |
|
OpenClaw audit | "How many tokens does my workspace use?", "Audit OpenClaw memory/persona/skills", "Which components consume tokens?", "Token usage of AGENTS.md / SOUL.md / skills" |
|
Model comparison | "Compare token cost across models", "Which model is cheaper?" |
Also trigger when the agent needs to count tokens or estimate cost before/after generating content.
Quick Reference
| Item | Command |
|---|
| Invoke | INLINECODE72 |
| List models |
python -m scripts.cli -l |
| Cost |
-c (input) /
--output-tokens (output) |
| Currency |
--currency USD or
INR |
Prompt Token Counter (toksum)
首次加载提醒: 本技能提供 scripts CLI(toksum)。当用户要求统计令牌数、估算 API 成本或审计 OpenClaw 组件的令牌消耗(记忆、角色、技能)时使用。
安装前 — 安全与隐私
- - 读取内容: 审计工作流会读取 ~/.openclaw/workspace 和 ~/.openclaw/skills 下的文件(AGENTS.md、SOUL.md、MEMORY.md、SKILL.md 等)。这些文件可能包含个人数据或机密信息。仅在你接受此访问权限时安装。
- URL 获取: CLI 可通过 -u 获取 HTTP(S) URL。SKILL.md 要求代理在获取前与用户确认每个 URL。坚持让代理遵守此规则;绝不允许自动获取未知 URL。
- 来源验证: 来源:https://github.com/Zhaobudaoyuema/prompt-token-counter。使用前请审查 scripts/core.py 和 scripts/cli.py。该代码仅执行本地文件读取和可选的 HTTP GET 操作;无其他网络调用或数据泄露。
- 先在本地运行: 如有疑问,请在隔离环境中手动运行 CLI,针对安全测试文件以验证其行为。
主要用途:OpenClaw 令牌消耗审计
目标: 帮助用户识别哪些 OpenClaw 组件消耗令牌以及消耗多少。
1. 记忆与角色文件
这些文件会被注入到会话中并消耗令牌。搜索并统计它们:
| 文件 | 用途 | 典型位置 |
|---|
| AGENTS.md | 操作指令、工作流程、优先级 | ~/.openclaw/workspace/ |
| SOUL.md |
角色、语气、价值观、行为准则 | ~/.openclaw/workspace/ |
| IDENTITY.md | 名称、角色、目标、视觉描述 | ~/.openclaw/workspace/ |
| USER.md | 用户偏好、沟通风格 | ~/.openclaw/workspace/ |
| MEMORY.md | 长期记忆、持久事实 | ~/.openclaw/workspace/ |
| TOOLS.md | 工具特性、路径约定 | ~/.openclaw/workspace/ |
| HEARTBEAT.md | 定期维护检查清单 | ~/.openclaw/workspace/ |
| BOOT.md | 启动仪式(启用钩子时) | ~/.openclaw/workspace/ |
| memory/YYYY-MM-DD.md | 每日记忆日志 | ~/.openclaw/workspace/memory/ |
工作区路径: 默认为 ~/.openclaw/workspace;可在 ~/.openclaw/openclaw.json 中通过 agent.workspace 覆盖。
2. 技能文件(SKILL.md)
技能按会话加载。统计每个 SKILL.md:
| 位置 | 范围 |
|---|
| ~/.openclaw/skills//SKILL.md | OpenClaw 管理的技能 |
| ~/.openclaw/workspace/skills//SKILL.md |
工作区特定技能(覆盖) |
3. 审计工作流
- 1. 定位工作区: 解析 ~/.openclaw/workspace(或配置覆盖)。
- 收集文件: 列出上述所有记忆/角色文件和 SKILL.md 路径。
- 统计令牌: 运行 python -m scripts.cli ... -m -c(批处理模式)。
- 汇总: 按类别分组(记忆、角色、技能),报告总计和每个文件。
审计命令示例(PowerShell):
powershell
$ws = $env:USERPROFILE\.openclaw\workspace
python -m scripts.cli -m gpt-4o -c $ws\AGENTS.md $ws\SOUL.md $ws\USER.md $ws\IDENTITY.md $ws\MEMORY.md $ws\TOOLS.md
审计命令示例(Bash):
bash
WS=~/.openclaw/workspace
python -m scripts.cli -m gpt-4o -c $WS/AGENTS.md $WS/SOUL.md $WS/USER.md $WS/IDENTITY.md $WS/MEMORY.md $WS/TOOLS.md
项目结构
prompttokencounter/
├── SKILL.md
├── package.json # npm 包(OpenClaw 技能)
├── publish_npm.py # 发布到 npm;同步版本
└── scripts/ # Python 包,CLI + 示例
├── cli.py # 入口点
├── core.py # TokenCounter, estimate_cost
├── registry/
│ ├── models.py # 300+ 模型
│ └── pricing.py # 定价数据
└── examples/ # 脚本示例
├── count_prompt.py
├── estimate_cost.py
├── batch_compare.py
└── benchmarktokenratio.py
调用:从项目根目录运行 python -m scripts.cli。
版本同步(publish_npm.py)
发布到 npm 时,publish_npm.py 会递增补丁版本并将其同步到:
- - package.json — version
- SKILL.md — 前置元数据 version
- scripts/init.py — version
运行:python publish_npm.py(在 npm login 之后)。
运行时依赖
- - Python 3 — 必需
- tiktoken(可选)— pip install tiktoken 用于精确的 OpenAI 统计
语言规则
使用用户的语言回复。 匹配用户的语言(例如,如果用户用中文写就用中文,如果用英文写就用英文)。
URL 使用 — 强制代理规则
在使用 -u / --url 从任何 URL 获取内容之前,你必须:
- 1. 明确警告用户 CLI 将向给定 URL 发起出站 HTTP/HTTPS 请求。
- 确认 URL 可信 — 告诉用户:仅使用你完全信任的 URL。不可信的 URL 可能会暴露你的 IP、泄露数据或被用于 SSRF。你确认此 URL 安全吗?
- 优先选择替代方案 — 如果用户可以通过 -f(本地文件)或内联文本提供内容,建议使用这些方式而不是 URL 获取。
- 绝不自动获取 — 在用户明确提供 URL 并确认风险之前,不要调用 -u。
如果用户坚持使用 URL: 仅在用户确认后继续。明确说明:我将从 [URL] 获取内容以统计令牌。继续吗?
模型名称 — 强制代理规则
在调用 CLI 之前,你必须从用户那里获得具体的模型名称。
- 1. 要求明确的模型 — -m / --model 是必需的。不要猜测或假设;用户必须提供确切的名称(例如 gpt-4o、claude-3-5-sonnet-20241022)。
- 如果不清楚,询问 — 如果用户说GPT或Claude或最新模型而没有具体名称,询问:请指定确切的模型名称(例如 gpt-4o、claude-3-5-sonnet-20241022)。运行 python -m scripts.cli -l 查看支持的模型列表。
- 不要自动选择 — 未经用户确认,绝不要代表用户替换模型。
- 尽可能验证 — 如果模型名称看起来模糊,提供 -l 输出或确认:我将使用 [模型]。正确吗?
CLI 使用
默认: 从本地文件读取。无分段。支持多个文件路径进行批处理执行。
bash
python -m scripts.cli [OPTIONS] [FILE ...]
| 选项 | 短选项 | 描述 |
|---|
| --model | -m | 模型名称(除非使用 --list-models,否则必需)— 代理必须从用户处获取确切名称;如果不清楚则询问 |
| --file |
-f | 从文件读取(可重复) |
| --url | -u | 从 URL 读取(可重复)—
代理在使用前必须警告用户;仅限可信 URL |
| --list-models | -l | 列出支持的模型 |