← 返回
数据分析 中文

Token Cost Estimator

Estimate API token costs from OpenClaw session transcripts. Analyzes all agent sessions to calculate what you'd pay on per-token pricing vs subscription plan...
从 OpenClaw 会话记录估算 API token 费用。分析所有代理会话,计算按 token 计费与订阅方案的花费差异。
npfaerber
数据分析 clawhub v1.0.1 1 版本 99879.7 Key: 无需
★ 2
Stars
📥 790
下载
💾 11
安装
1
版本
#api#billing#costs#latest#tokens

概述

Token Cost Estimator

Estimate real API costs from OpenClaw session transcript data.

How It Works

OpenClaw stores session transcripts as JSONL files in ~/.openclaw/agents//sessions/*.jsonl. Each line is a turn with role, content, and sometimes usage data.

Estimation Script

Run this Python script to analyze all agents:

import json, glob, os

AGENTS_DIR = os.path.expanduser('~/.openclaw/agents')
# Pricing per million tokens (update as needed)
PRICING = {
    'opus': {'input': 15, 'output': 75, 'cache_read': 1.875},
    'sonnet': {'input': 3, 'output': 15, 'cache_read': 0.375},
}

agent_dirs = [d for d in os.listdir(AGENTS_DIR) if os.path.isdir(os.path.join(AGENTS_DIR, d))]
grand_in, grand_out = 0, 0

for agent in sorted(agent_dirs):
    sess_dir = os.path.join(AGENTS_DIR, agent, 'sessions')
    if not os.path.isdir(sess_dir):
        continue
    total_in, total_out, sessions = 0, 0, 0
    for f in glob.glob(os.path.join(sess_dir, '*.jsonl')):
        sessions += 1
        turns = []
        for line in open(f):
            try:
                obj = json.loads(line)
                msg = obj.get('message', {})
                if not isinstance(msg, dict): continue
                role = msg.get('role', '')
                raw = json.dumps(msg)
                turns.append((role, len(raw)))
            except: pass
        # Account for context re-sending
        cumulative = 0
        for role, chars in turns:
            if role in ('user', 'system', 'tool'):
                cumulative += chars
            elif role == 'assistant':
                total_in += cumulative // 4
                total_out += chars // 4
    print(f'{agent}: {sessions} sessions, ~{total_in:,} input, ~{total_out:,} output tokens')
    grand_in += total_in
    grand_out += total_out

print(f'\nTotal input: ~{grand_in:,}, output: ~{grand_out:,}')
for tier, rates in PRICING.items():
    for cache_pct in [0.6, 0.9]:
        cached = int(grand_in * cache_pct)
        uncached = grand_in - cached
        cost = (uncached/1e6)*rates['input'] + (cached/1e6)*rates['cache_read'] + (grand_out/1e6)*rates['output']
        print(f'{tier} ({int(cache_pct*100)}% cache): ${cost:,.2f}')

Key Concepts

Context re-sending: Every API call sends the full conversation history as input. A 50-turn conversation re-sends all prior turns on each new message. This is the #1 cost driver.

Cache hits: OpenClaw caches prompt prefixes. Typical cache hit rates: 60-90%. Cache reads cost 87.5% less than fresh input.

What transcripts miss: System prompts, tool definitions, and internal retries aren't always logged. Real cost is typically 1.5-2x the transcript estimate.

Comparing Plans

PlanMonthly CostBest For
-----------------------------
API (Opus)VariableHeavy agentic use (>$200/mo equivalent)
API (Sonnet)VariableMost agent tasks, 5x cheaper than Opus
Claude Max ($100)$100 flatLight-medium use via OAuth (if allowed)
Claude Max ($200)$200 flatHeavy use via OAuth (if allowed)

Break-even: If your estimated API cost exceeds your subscription price, the subscription saves money. Note: Anthropic has restricted OAuth token use in third-party tools.

版本历史

共 1 个版本

  • v1.0.1 当前
    2026-03-29 23:47 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

data-analysis

Data Analysis

ivangdavila
{"answer":"数据分析与可视化。查询数据库、生成报告、自动化电子表格,将原始数据转化为清晰可行的见解。适用于:(1) 您……"}
★ 198 📥 64,854
data-analysis

A股量化 AkShare

mbpz
A股量化数据分析工具,基于AkShare库获取A股行情、财务数据、板块信息等。用于回答关于A股股票查询、行情数据、财务分析、选股等问题。
★ 162 📥 59,672
security-compliance

Node.js Security Audit

npfaerber
审计 Node.js HTTP 服务器和 Web 应用安全漏洞,检查 OWASP Top 10、CORS、身份验证绕过、XSS、路径遍历、硬编码密钥、缺失...
★ 0 📥 1,149