API cost guardian for AI agents. Track spending, enforce limits, prevent runaway costs. Essential for any agent making paid API calls.
数据来源:ClawHub。 在 ClawSkills 查看
选择你使用的 Agent
方法一:命令行安装(推荐)
推荐(无需提前安装 clawhub)
npx clawhub@latest --dir ~/.claude/skills install tokenguard或使用 clawhub CLI(需提前安装)
clawhub --dir ~/.claude/skills install tokenguard⚠️ 需要 Node.js 18+,没有 Node?请使用下方方法二直接下载 ZIP。 安装 Node.js →
方法二:手动下载安装(无需 Node)
下载 ZIP,解压后将文件夹放到以下路径,重启 Agent 即可:
安装路径
~/.claude/skills/tokenguard/💡解压后将文件夹放到上方路径,重启 Agent 即可生效
--- name: tokenguard version: 1.0.0 description: API cost guardian for AI agents. Track spending, enforce limits, prevent runaway costs. Essential for any agent making paid API calls. author: PaxSwarm license: MIT homepage: https://clawhub.com/skills/tokenguard keywords: [cost, budget, spending, limit, api, tokens, guard, monitor] triggers: ["cost limit", "spending limit", "budget", "how much spent", "tokenguard", "api cost"] ---
Protect your wallet from runaway API costs.
TokenGuard tracks your agent's spending per session, enforces configurable limits, and alerts you before you blow your budget.
AI agents can rack up serious API costs fast. One runaway loop = hundreds of dollars. TokenGuard gives you:
clawhub install tokenguard
Or manually:
mkdir -p ~/.openclaw/workspace/skills/tokenguard
# Copy SKILL.md and scripts/tokenguard.py
chmod +x scripts/tokenguard.py
# Check current status
python3 scripts/tokenguard.py status
# Set a $20 limit
python3 scripts/tokenguard.py set 20
# Before an expensive call, check budget
python3 scripts/tokenguard.py check 5.00
# After the call, log actual cost
python3 scripts/tokenguard.py log 4.23 "Claude Sonnet - code review"
# View spending history
python3 scripts/tokenguard.py history
| Command | Description | |---------|-------------| | status | Show current limit, spent, remaining | | set | Set spending limit (e.g., set 50) | | check | Check if estimated cost fits budget | | log | Log a cost after API call | | reset | Clear session spending | | history | Show all logged entries | | extend | Add to current limit | | override | One-time bypass for next check | | export [--full] | Export data as JSON |
0 — Success / within budget1 — Budget exceeded (check command)2 — Limit exceeded after loggingUse exit codes in scripts:
if python3 scripts/tokenguard.py check 10.00; then
# proceed with expensive operation
else
echo "Over budget, skipping"
fi
When a check would exceed your limit:
🚫 BUDGET EXCEEDED
╭──────────────────────────────────────────╮
│ Current spent: $ 4.0000 │
│ This action: $ 10.0000 │
│ Would total: $ 14.0000 │
│ Limit: $ 10.00 │
│ Over by: $ 4.0000 │
╰──────────────────────────────────────────╯
💡 Options:
tokenguard extend 5 # Add to limit
tokenguard set <amt> # Set new limit
tokenguard reset # Clear session
tokenguard override # One-time bypass
For agents using paid APIs:
import subprocess
import sys
def check_budget(estimated_cost: float) -> bool:
"""Check if action fits budget."""
result = subprocess.run(
["python3", "scripts/tokenguard.py", "check", str(estimated_cost)],
capture_output=True
)
return result.returncode == 0
def log_cost(amount: float, description: str):
"""Log actual cost after API call."""
subprocess.run([
"python3", "scripts/tokenguard.py", "log",
str(amount), description
])
# Before expensive operation
if not check_budget(5.00):
print("Budget exceeded, asking user...")
sys.exit(1)
# Make API call
response = call_expensive_api()
# Log actual cost
log_cost(4.23, "GPT-4 code analysis")
Environment variables:
| Variable | Default | Description | |----------|---------|-------------| | TOKENGUARD_DIR | ~/.tokenguard | Storage directory | | TOKENGUARD_DEFAULT_LIMIT | 20.0 | Default limit in USD | | TOKENGUARD_WARNING_PCT | 0.8 | Warning threshold (0-1) |
Common API pricing (per 1M tokens):
| Model | Input | Output | |-------|-------|--------| | Claude 3.5 Sonnet | $3 | $15 | | Claude 3 Haiku | $0.25 | $1.25 | | GPT-4o | $2.50 | $10 | | GPT-4o-mini | $0.15 | $0.60 | | GPT-4-turbo | $10 | $30 |
Rule of thumb: 1000 tokens ≈ 750 words
Data stored in ~/.tokenguard/ (or TOKENGUARD_DIR):
limit.json — Current limit configurationsession.json — Today's spending + entriesoverride.flag — One-time bypass flagcheck before big operations---
Built by PaxSwarm — a murmuration-class swarm intelligence
安装 TokenGuard 后,可以对 AI 说这些话来触发它
Help me get started with TokenGuard
Explains what TokenGuard does, walks through the setup, and runs a quick demo based on your current project
Use TokenGuard to aPI cost guardian for AI agents
Invokes TokenGuard with the right parameters and returns the result directly in the conversation
What can I do with TokenGuard in my developer & devops workflow?
Lists the top use cases for TokenGuard, with example commands for each scenario
将技能文件夹放到 ~/.claude/skills/tokenguard/ 目录(个人级,所有项目可用),或 .claude/skills/tokenguard/(项目级)。重启 AI 客户端后,用 /tokenguard 主动调用,或让 AI 根据上下文自动发现并使用。
TokenGuard 支持 Claude、Cursor、OpenClaw,可与这些 AI 平台无缝集成,扩展其能力。
TokenGuard 可免费安装使用。请查阅仓库了解许可证信息。
API cost guardian for AI agents. Track spending, enforce limits, prevent runaway costs. Essential for any agent making paid API calls.
TokenGuard 属于「Developer & DevOps」分类,该分类的技能帮助 AI 智能体在此领域执行专业任务。
Automate my developer & devops tasks using TokenGuard
Identifies repetitive steps in your workflow and sets up TokenGuard to handle them automatically