Session Cost
Analyze OpenClaw session logs for token usage, costs, and performance metrics grouped by agent and model.
By default, scans all agents in ~/.openclaw/agents/. Each agent's sessions are read from ~/.openclaw/agents/<name>/sessions/.
Quick Start
CODEBLOCK0
Options
- -
--path <dir> — Directory to scan for .jsonl files (overrides agent auto-discovery) - INLINECODE4 — Filter by agent name (e.g.,
main, codegen) - INLINECODE7 — Only include sessions from the last N units (
30m, 2h, 7d) - INLINECODE11 — Filter by model provider (
anthropic, openai, ollama, etc.) - INLINECODE15 — Show per-session details. Optionally pass a session ID to show just that session (searches across all agents for
<id>.jsonl) - INLINECODE17 — Show details in compact table format (use with
--details) - INLINECODE19 — Output format:
text (default), json, or INLINECODE22 - INLINECODE23 — Shorthand for
--format json (backwards compat) - INLINECODE25 ,
-h — Show help message
Examples
CODEBLOCK1
Output Format
Text Summary (Default)
Results are grouped by agent, then by model within each agent. A grand total section shows per-agent subtotals and a combined total.
CODEBLOCK2
When only a single agent is present, the grand total shows "All models (N)" instead.
Text Details (--details)
Shows per-session breakdown (session ID, agent, model, duration, timestamps, tokens, cache, cost) followed by the agent/model summary.
Table Format (--details --table)
Compact table view. When multiple agents are present, an Agent column is included.
CODEBLOCK3
With a single agent, the Agent column is omitted and the table matches the previous format.
JSON (--format json)
Results are nested by agent. Each agent contains its model summaries and an agent-level totals object. A top-level grandTotal aggregates across all agents.
CODEBLOCK4
Discord (--format discord)
Optimized for chat platforms (Discord, Slack, etc.) - concise, markdown-friendly, no tables:
CODEBLOCK5
The "By Agent" section is shown only when multiple agents are present.
Output Fields
- - Agent — Agent name (derived from directory under
~/.openclaw/agents/) - Sessions — Number of session files analyzed
- Tokens — Total, input, and output token counts
- Cache — Cache read and write token counts
- Cost — Total cost broken down by input, output, cache read, and cache write
- Duration — Session duration in minutes (details mode)
- Timestamps — First and last activity timestamps (details mode)
Notes
- - When
--path is provided, it overrides agent auto-discovery and scans exactly that directory. The agent name is inferred from the path (e.g., .../agents/main/sessions → "main"). - INLINECODE36 and
--provider filters can be combined (e.g., --agent main --provider anthropic). - Single session lookup (
--details <id>) searches across all discovered agents to find the session file.
会话成本
分析 OpenClaw 会话日志中的令牌使用量、成本和性能指标,按代理和模型分组。
默认情况下,扫描 ~/.openclaw/agents/ 中的所有代理。每个代理的会话从 ~/.openclaw/agents/<名称>/sessions/ 中读取。
快速开始
bash
所有代理的摘要
node /home/claw/.openclaw/workspace/skills/session-cost/scripts/session-cost.js
显示所有会话详情
node /home/claw/.openclaw/workspace/skills/session-cost/scripts/session-cost.js --details
显示特定会话的详情(在所有代理中搜索)
node /home/claw/.openclaw/workspace/skills/session-cost/scripts/session-cost.js --details abc123
选项
- - --path <目录> — 扫描 .jsonl 文件的目录(覆盖代理自动发现)
- --agent <名称> — 按代理名称过滤(例如 main、codegen)
- --offset <时间> — 仅包含最近 N 个时间单位的会话(30m、2h、7d)
- --provider <名称> — 按模型提供商过滤(anthropic、openai、ollama 等)
- --details [会话ID] — 显示每个会话的详情。可选地传入会话 ID 以仅显示该会话(在所有代理中搜索 .jsonl)
- --table — 以紧凑表格格式显示详情(与 --details 一起使用)
- --format <类型> — 输出格式:text(默认)、json 或 discord
- --json — --format json 的简写(向后兼容)
- --help、-h — 显示帮助信息
示例
bash
最近 24 小时摘要
node /home/claw/.openclaw/workspace/skills/session-cost/scripts/session-cost.js --offset 24h
仅主代理
node /home/claw/.openclaw/workspace/skills/session-cost/scripts/session-cost.js --agent main
最近 7 天,JSON 输出
node /home/claw/.openclaw/workspace/skills/session-cost/scripts/session-cost.js --offset 7d --json
Discord 友好格式(用于机器人/聊天)
node /home/claw/.openclaw/workspace/skills/session-cost/scripts/session-cost.js --format discord
带过滤器的 Discord 格式
node /home/claw/.openclaw/workspace/skills/session-cost/scripts/session-cost.js --format discord --offset 24h --provider anthropic
按提供商过滤
node /home/claw/.openclaw/workspace/skills/session-cost/scripts/session-cost.js --provider anthropic
所有会话的紧凑表格格式
node /home/claw/.openclaw/workspace/skills/session-cost/scripts/session-cost.js --details --table
自定义路径带详情(覆盖代理发现,扫描指定目录)
node /home/claw/.openclaw/workspace/skills/session-cost/scripts/session-cost.js --path /other/dir --details
单个会话详情(自动在代理间查找)
node /home/claw/.openclaw/workspace/skills/session-cost/scripts/session-cost.js --details 9df7a399-8254-411b-a875-e7337df73d29
最近 24 小时的 Anthropic 会话,表格格式
node /home/claw/.openclaw/workspace/skills/session-cost/scripts/session-cost.js --provider anthropic --offset 24h --details --table
输出格式
文本摘要(默认)
结果按代理分组,然后在每个代理内按模型分组。总计部分显示每个代理的小计和合并总计。
在 2 个代理中找到 52 个 .jsonl 文件,匹配 52 个
====================================================================================================
按代理摘要
====================================================================================================
代理:main
anthropic/claude-sonnet-4-5-20250929
会话数:30
令牌数:1,234,567(输入:900,000,输出:334,567)
缓存:读取:500,000 令牌,写入:200,000 令牌
成本:$12.3456
输入:$5.4000
输出:$5.0185
缓存读取:$1.5000(包含在总计中,折扣费率)
缓存写入:$0.4271(包含在总计中)
anthropic/claude-opus-4-6
会话数:5
令牌数:250,000(输入:180,000,输出:70,000)
...
代理:codegen
anthropic/claude-sonnet-4-5-20250929
会话数:17
...
====================================================================================================
总计
====================================================================================================
main — 35 个会话,$15.8200
codegen — 17 个会话,$8.5600
所有代理(2 个)
会话数:52
令牌数:...
成本:$24.3800
...
当只有一个代理时,总计显示为所有模型(N 个)。
文本详情(--details)
显示每个会话的细分(会话 ID、代理、模型、持续时间、时间戳、令牌、缓存、成本),然后是代理/模型摘要。
表格格式(--details --table)
紧凑表格视图。当存在多个代理时,包含代理列。
会话详情
============================================================================================================================================
代理 模型 持续时间 令牌数 缓存 成本 会话
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
main anthropic/claude-sonnet-4.5 45 分钟 128.5K 15.2K / 8.1K $0.3245 abc123def456
codegen anthropic/claude-sonnet-4.5 12 分钟 45.3K 2.1K / 1.5K $0.8921 xyz789abc012
当只有一个代理时,代理列被省略,表格保持之前的格式。
JSON(--format json)
结果按代理嵌套。每个代理包含其模型摘要和一个代理级别的 totals 对象。顶层 grandTotal 聚合所有代理。
json
{
agents: {
main: {
models: {
anthropic/claude-sonnet-4-5-20250929: {
sessions: 30,
tokens: { input: 900000, output: 334567, total: 1234567 },
cache: { read: 500000, write: 200000 },
cost: { total: 12.3456, input: 5.4, output: 5.0185, cacheRead: 1.5, cacheWrite: 0.4271 }
}
},
totals: {
sessions: 35,
tokens: { input: 1080000, output: 404567, total: 1484567 },
cache: { read: 600000, write: 250000 },
cost: { total: 15.82, ... }
}
},
codegen: {
models: { ... },
totals: { ... }
}
},
grandTotal: {
sessions: 52,
tokens: { input: 1500000, output: 600000, total: 2100000 },
cache: { read: 800000, write: 350000 },
cost: { total: 24.38, ... }
}
}
Discord(--format discord)
针对聊天平台(Discord、Slack 等)优化——简洁、支持 Markdown、无表格:
💰 使用摘要
(最近 24 小时)
总成本: $24.38
总令牌数: 2.1M
会话数: 52
按代理:
• main:$15.82(35 个会话)
• codegen:$8.56(17 个会话)
按提供商:
• anthropic:$22.50(1.9M 令牌)
• openai:$1.88(200K 令牌)
热门模型:
• anthropic/claude-sonnet-4.5:$18.20(1.5M 令牌)