{"id":10021323,"date":"2026-03-20T22:13:18","date_gmt":"2026-03-20T14:13:18","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021323"},"modified":"2026-03-20T22:13:18","modified_gmt":"2026-03-20T14:13:18","slug":"%e6%88%91%e6%98%af%e6%80%8e%e4%b9%88%e6%90%9e%e6%b8%85%e6%a5%9a-claude-code-%e6%af%8f%e5%a4%a9%e8%8a%b1%e4%ba%86%e5%a4%9a%e5%b0%91%e9%92%b1%e7%9a%84","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021323","title":{"rendered":"\u6211\u662f\u600e\u4e48\u641e\u6e05\u695a Claude Code \u6bcf\u5929\u82b1\u4e86\u591a\u5c11\u94b1\u7684"},"content":{"rendered":"<blockquote><p>\u4ece\u4e00\u4e2a HUD \u72b6\u6001\u680f\u5de5\u5177\u51fa\u53d1\uff0c\u4e00\u8def\u7ffb\u65e5\u5fd7\u3001\u6252 JSON\u3001\u5199 bash\uff0c\u6700\u7ec8\u641e\u5b9a\u4e00\u5957\u79bb\u7ebf token \u7528\u91cf\u7edf\u8ba1\u65b9\u6848\u3002<\/p><\/blockquote>\n<hr>\n<h2>\u8d77\u70b9\uff1aclaude-hud \u662f\u4e2a\u597d\u4e1c\u897f<\/h2>\n<p>\u5982\u679c\u4f60\u662f Claude Code \u91cd\u5ea6\u7528\u6237\uff0c\u5927\u6982\u7387\u89c1\u8fc7 <a href=\"https:\/\/link.segmentfault.com\/?enc=DRAsR8MHwD3MptA%2FhQjAWQ%3D%3D.OYLqN5cOVj5B0Zhr0KEJGVSl6QEAlkzmQ7b023s0T%2FNbf1J7G6Atx3L67zDCfugK\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">claude-hud<\/a> \u8fd9\u4e2a\u9879\u76ee\u3002\u5b83\u628a Claude Code \u7684\u5b9e\u65f6\u72b6\u6001\u5d4c\u5165\u7ec8\u7aef\u72b6\u6001\u680f\uff08\u652f\u6301 tmux\/iTerm2\/Starship \u7b49\uff09\uff0c\u6548\u679c\u5927\u6982\u662f\u8fd9\u6837\uff1a<\/p>\n<pre><code>[claude-hud]  claude-sonnet-4-6  \u2191 4.2k  \u2193 18k  cache 87%  $0.18<\/code><\/pre>\n<p>\u4e00\u773c\u80fd\u770b\u5230\u5f53\u524d session \u7528\u7684\u662f\u54ea\u4e2a\u6a21\u578b\u3001\u8f93\u5165\u8f93\u51fa\u4e86\u591a\u5c11 token\u3001\u7f13\u5b58\u547d\u4e2d\u7387\u591a\u5c11\uff0c\u4ee5\u53ca\u672c\u6b21\u5bf9\u8bdd\u4f30\u7b97\u82b1\u4e86\u591a\u5c11\u94b1\u3002\u5bf9\u4e8e\u7528 API key \u6216 AWS Bedrock \u7684\u7528\u6237\u6765\u8bf4\uff0c\u8fd9\u4e2a\u5b9e\u65f6\u611f\u77e5\u975e\u5e38\u723d\u3002<\/p>\n<p>\u6211\u7528\u4e86\u4e00\u6bb5\u65f6\u95f4\uff0c\u786e\u5b9e\u5f88\u76f4\u89c2\u3002\u4f46\u6162\u6162\u5730\uff0c\u4e00\u4e2a\u95ee\u9898\u5f00\u59cb\u56f0\u6270\u6211\uff1a<\/p>\n<blockquote><p><strong>&#8220;\u6211\u4eca\u5929\/\u8fd9\u5468\u5230\u5e95\u82b1\u4e86\u591a\u5c11\uff1f&#8221;<\/strong><\/p><\/blockquote>\n<p>claude-hud \u7684 usage \u8fdb\u5ea6\u6761\u662f\u9488\u5bf9 Claude.ai Pro\/Max \u8ba2\u9605\u8bbe\u8ba1\u7684\uff0c\u7edf\u8ba1\u7684\u662f\u8ba2\u9605\u989d\u5ea6\u6d88\u8017\uff1b\u800c\u6211\u7528\u7684\u662f Anthropic API \u76f4\u8fde\u6216 AWS Bedrock\uff0c\u5b83\u5e76\u4e0d\u77e5\u9053\u6211\u5b9e\u9645\u652f\u51fa\u4e86\u591a\u5c11\u7f8e\u5143\u3002\u6bcf\u6b21\u8981\u7b97\u8d26\uff0c\u90fd\u53ea\u80fd\u53bb AWS \u8d26\u5355\u9875\u9762\u7ffb\uff0c\u5ef6\u8fdf\u4e00\u5929\u4e0d\u8bf4\uff0c\u8fd8\u6ca1\u6cd5\u6309\u9879\u76ee\u7ec6\u5206\u3002<\/p>\n<p>\u8fd9\u8ba9\u6211\u5f00\u59cb\u7422\u78e8\uff1a<strong>Claude Code \u628a\u5bf9\u8bdd\u8bb0\u5f55\u5b58\u5728\u54ea\u91cc\uff1f\u80fd\u4e0d\u80fd\u76f4\u63a5\u4ece\u672c\u5730\u6570\u636e\u7edf\u8ba1\uff1f<\/strong><\/p>\n<hr>\n<h2>\u53d1\u73b0\u672c\u5730\u65e5\u5fd7\uff1a<code>.claude\/projects\/<\/code><\/h2>\n<p>\u7ffb\u4e86\u7ffb Claude Code \u7684\u6587\u6863\uff0c\u627e\u5230\u4e86\u7b54\u6848\u3002\u6240\u6709\u5bf9\u8bdd\u8bb0\u5f55\u90fd\u4ee5 JSONL \u683c\u5f0f\u5b58\u50a8\u5728\u672c\u5730\uff1a<\/p>\n<pre><code>~\/.claude\/projects\/\n\u251c\u2500\u2500 -Users-alice-fe-dashboard\/\n\u2502   \u251c\u2500\u2500 3a9f2c1e-5b7d-4e8a-9f1c-2d3e4f5a6b7c.jsonl\n\u2502   \u2514\u2500\u2500 8b1d3e5f-7a9c-4b2d-8e1f-3c5d7e9f1a2b.jsonl\n\u251c\u2500\u2500 -Users-alice-fe-api-service\/\n\u2502   \u2514\u2500\u2500 2f4a6c8e-1b3d-5f7a-9c1e-4b6d8f0a2c4e.jsonl\n\u2514\u2500\u2500 -Users-alice-scripts\/\n    \u2514\u2500\u2500 9e1c3a5b-7d9f-4a2c-8e0b-1d3f5a7c9e1b.jsonl<\/code><\/pre>\n<p>\u76ee\u5f55\u547d\u540d\u89c4\u5219\u5f88\u76f4\u63a5\uff1a\u628a\u5de5\u4f5c\u8def\u5f84\u91cc\u7684 <code>\/<\/code> \u6362\u6210 <code>-<\/code>\uff0c\u5c31\u662f\u76ee\u5f55\u540d\u3002\u6bcf\u4e2a\u76ee\u5f55\u5bf9\u5e94\u4e00\u4e2a\u5de5\u4f5c\u8def\u5f84\uff0c\u76ee\u5f55\u4e0b\u7684\u6bcf\u4e2a <code>.jsonl<\/code> \u6587\u4ef6\u662f\u4e00\u4e2a\u5bf9\u8bdd session\u3002<\/p>\n<p>\u6253\u5f00\u4e00\u4e2a JSONL \u6587\u4ef6\uff0c\u6bcf\u4e00\u884c\u662f\u4e00\u6761\u6d88\u606f\u8bb0\u5f55\u3002\u627e\u5230 <code>type == &quot;assistant&quot;<\/code> \u7684\u884c\uff0c\u91cc\u9762\u6709\u5173\u952e\u7684 <code>usage<\/code> \u5b57\u6bb5\uff1a<\/p>\n<pre><code class=\"json\">{\n  &quot;type&quot;: &quot;assistant&quot;,\n  &quot;timestamp&quot;: &quot;2026-03-20T09:14:32.451Z&quot;,\n  &quot;message&quot;: {\n    &quot;model&quot;: &quot;claude-sonnet-4-6&quot;,\n    &quot;usage&quot;: {\n      &quot;input_tokens&quot;: 1024,\n      &quot;output_tokens&quot;: 8192,\n      &quot;cache_read_input_tokens&quot;: 487321,\n      &quot;cache_creation_input_tokens&quot;: 62144,\n      &quot;cache_creation&quot;: {\n        &quot;ephemeral_5m_input_tokens&quot;: 62144,\n        &quot;ephemeral_1h_input_tokens&quot;: 0\n      }\n    }\n  }\n}<\/code><\/pre>\n<p>\u6570\u636e\u5f88\u5b8c\u6574\u3002\u4f46\u770b\u5230\u8fd9\u51e0\u4e2a\u5b57\u6bb5\uff0c\u65b0\u7684\u95ee\u9898\u6765\u4e86\u2014\u2014<\/p>\n<hr>\n<h2>\u7b49\u7b49\uff0ccache \u8fd9\u51e0\u4e2a\u5b57\u6bb5\u662f\u4ec0\u4e48\u610f\u601d\uff1f<\/h2>\n<p>\u5982\u679c\u4f60\u76f4\u63a5\u628a <code>input_tokens + output_tokens<\/code> \u4e58\u4ee5\u5355\u4ef7\uff0c\u7b97\u51fa\u6765\u7684\u6570\u5b57\u4f1a\u8ba9\u4f60\u56f0\u60d1\uff1a<strong>\u660e\u660e\u7528\u4e86\u5f88\u4e45\uff0c\u600e\u4e48\u8d39\u7528\u8fd9\u4e48\u4f4e\uff1f<\/strong><\/p>\n<p>\u79d8\u5bc6\u5c31\u5728 <code>cache_read_input_tokens<\/code>\u3002<\/p>\n<p>Claude Code \u6bcf\u6b21 API \u8c03\u7528\u90fd\u4f1a\u643a\u5e26\u5b8c\u6574\u7684\u5bf9\u8bdd\u5386\u53f2 + \u7cfb\u7edf prompt\uff0c\u8fd9\u4e2a context \u52a8\u8f84\u51e0\u5341\u4e07 token\u3002\u5982\u679c\u6bcf\u6b21\u90fd\u91cd\u65b0\u4f20\u8f93\u8ba1\u8d39\uff0c\u8d39\u7528\u4f1a\u7206\u70b8\u3002Anthropic \u7684 Prompt Cache \u673a\u5236\u6b63\u662f\u4e3a\u6b64\u8bbe\u8ba1\u7684\uff1a<\/p>\n<pre><code>\u7b2c 1 \u8f6e\u5bf9\u8bdd\uff1a\u5199\u5165\u7f13\u5b58\uff08cache_creation\uff09\n\u7b2c 2 \u8f6e\u5bf9\u8bdd\uff1a\u547d\u4e2d\u7f13\u5b58\uff08cache_read\uff09\u2190 \u53ea\u6536 0.1x \u4ef7\u683c\n\u7b2c 3 \u8f6e\u5bf9\u8bdd\uff1a\u547d\u4e2d\u7f13\u5b58\uff08cache_read\uff09\u2190 \u8fd8\u662f 0.1x \u4ef7\u683c\n...<\/code><\/pre>\n<p>\u5728\u4e00\u4e2a\u5178\u578b\u7684 Claude Code \u5de5\u4f5c session \u91cc\uff0ccache_read \u5360\u603b\u8f93\u5165 token \u7684 <strong>85~90%<\/strong>\uff0c\u5b9e\u9645\u8d39\u7528\u6bd4&#8221;\u5168\u91cf input&#8221;\u8ba1\u8d39\u4fbf\u5b9c\u7ea6 <strong>70~80%<\/strong>\u3002<\/p>\n<p>\u6240\u4ee5\u5b57\u6bb5\u62c6\u89e3\u5982\u4e0b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u5b57\u6bb5<\/th>\n<th>\u542b\u4e49<\/th>\n<th>\u8ba1\u8d39\u500d\u7387\uff08\u76f8\u5bf9 input\uff09<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>input_tokens<\/code><\/td>\n<td>\u672c\u8f6e\u771f\u6b63\u65b0\u53d1\u9001\u7684 token<\/td>\n<td>1x<\/td>\n<\/tr>\n<tr>\n<td><code>output_tokens<\/code><\/td>\n<td>\u6a21\u578b\u751f\u6210\u7684\u8f93\u51fa<\/td>\n<td>~5x (output \u66f4\u8d35)<\/td>\n<\/tr>\n<tr>\n<td><code>cache_read_input_tokens<\/code><\/td>\n<td>\u547d\u4e2d\u5df2\u6709\u7f13\u5b58\uff0c\u590d\u7528\u7684 token<\/td>\n<td>0.1x<\/td>\n<\/tr>\n<tr>\n<td><code>cache_creation.ephemeral_5m_input_tokens<\/code><\/td>\n<td>\u5199\u5165 5 \u5206\u949f\u77ed\u6548\u7f13\u5b58<\/td>\n<td>1.25x<\/td>\n<\/tr>\n<tr>\n<td><code>cache_creation.ephemeral_1h_input_tokens<\/code><\/td>\n<td>\u5199\u5165 1 \u5c0f\u65f6\u957f\u6548\u7f13\u5b58<\/td>\n<td>2x<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6ce8\u610f <code>cache_creation_input_tokens<\/code> \u662f\u4e24\u79cd\u5199\u5165\u7684\u603b\u548c\uff0c\u4f46\u8ba1\u8d39\u8981\u5206\u5f00\u7b97\u2014\u20145m \u548c 1h \u5355\u4ef7\u4e0d\u540c\uff0c\u5fc5\u987b\u7528\u7ec6\u5206\u5b57\u6bb5\u3002<\/p>\n<hr>\n<h2>\u67b6\u6784\uff1a\u4e00\u4e2a bash \u811a\u672c\u641e\u5b9a<\/h2>\n<p>\u7406\u89e3\u4e86\u6570\u636e\u7ed3\u6784\u4e4b\u540e\uff0c\u6574\u4e2a\u65b9\u6848\u5c31\u5f88\u6e05\u6670\u4e86\uff1a<\/p>\n<pre><code>~\/.claude\/projects\/**\/*.jsonl\n         \u2502\n         \u2502  find + xargs jq\n         \u25bc\n   \u539f\u59cb CSV \u6d41\uff08\u6bcf\u884c\u4e00\u6761 assistant \u6d88\u606f\uff09\n   model, input, output, cache_hit, cache_5m, cache_1h, count=1\n         \u2502\n         \u2502  awk \u5206\u7ec4\u805a\u5408\n         \u25bc\n   \u6309\u6a21\u578b\u5206\u7ec4\u7684\u6c47\u603b\u6570\u636e\n         \u2502\n         \u2502  bash \u51fd\u6570\u67e5\u4ef7\u683c\u8868\n         \u25bc\n   per-model \u8d39\u7528 \u2192 awk \u6d6e\u70b9\u52a0\u6cd5\u7d2f\u52a0\n         \u2502\n         \u25bc\n   \u683c\u5f0f\u5316\u8f93\u51fa<\/code><\/pre>\n<p>\u6574\u4e2a\u903b\u8f91\u7528\u4e00\u4e2a\u4e0d\u5230 160 \u884c\u7684 bash \u811a\u672c\u5b9e\u73b0\uff0c\u65e0\u9700\u4efb\u4f55\u670d\u52a1\uff0c\u79bb\u7ebf\u8fd0\u884c\u3002<\/p>\n<hr>\n<h2>\u811a\u672c\u5904\u7406\u7ec6\u8282<\/h2>\n<h3>1. \u65f6\u95f4\u8303\u56f4\u8fc7\u6ee4<\/h3>\n<pre><code class=\"bash\">case &quot;$PERIOD&quot; in\n  today)\n    SINCE=$(date -u +&quot;%Y-%m-%dT00:00:00&quot;)\n    ;;\n  week)\n    # macOS \u7528 -v-7d\uff0cLinux \u7528 -d &quot;7 days ago&quot;\uff0c\u4e24\u8005\u517c\u5bb9\n    SINCE=$(date -u -v-7d +&quot;%Y-%m-%dT00:00:00&quot; 2&gt;\/dev\/null \/\n         || date -u -d &quot;7 days ago&quot; +&quot;%Y-%m-%dT00:00:00&quot;)\n    ;;\n  all)\n    SINCE=&quot;1970-01-01T00:00:00&quot;\n    ;;\nesac<\/code><\/pre>\n<p>\u65f6\u95f4\u6bd4\u8f83\u76f4\u63a5\u5229\u7528 ISO 8601 \u5b57\u7b26\u4e32\u7684\u5b57\u5178\u5e8f\uff08<code>timestamp &gt;= $since<\/code>\uff09\uff0cjq \u91cc\u5b57\u7b26\u4e32\u6bd4\u8f83\u5c31\u591f\u7528\uff0c\u4e0d\u9700\u8981\u8f6c\u65f6\u95f4\u6233\u3002<\/p>\n<h3>2. jq \u63d0\u53d6\uff1a\u4e00\u6b21\u626b\u63cf\uff0c\u8f93\u51fa CSV \u6d41<\/h3>\n<pre><code class=\"bash\">RAW=$(echo &quot;$FILES&quot; | xargs jq -r --arg since &quot;$SINCE&quot; '\n  select(\n    .type == &quot;assistant&quot;\n    and .message.usage != null\n    and .timestamp != null\n    and .timestamp &gt;= $since\n  ) |\n  [\n    (.message.model \/\/ &quot;unknown&quot;),\n    (.message.usage.input_tokens \/\/ 0),\n    (.message.usage.output_tokens \/\/ 0),\n    (.message.usage.cache_read_input_tokens \/\/ 0),\n    (.message.usage.cache_creation.ephemeral_5m_input_tokens \/\/ 0),\n    (.message.usage.cache_creation.ephemeral_1h_input_tokens \/\/ 0),\n    1\n  ] | @csv\n' 2&gt;\/dev\/null)<\/code><\/pre>\n<p>\u51e0\u4e2a\u7ec6\u8282\u503c\u5f97\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li><code>\/\/ 0<\/code> \u662f jq \u7684 alternative \u8fd0\u7b97\u7b26\uff0c\u5b57\u6bb5\u7f3a\u5931\u65f6\u53d6 0\uff0c\u907f\u514d null \u6c61\u67d3\u540e\u7eed awk \u8ba1\u7b97<\/li>\n<li>\u6700\u540e\u4e00\u5217\u56fa\u5b9a\u4e3a <code>1<\/code>\uff0c\u7528\u6765\u5728 awk \u91cc\u76f4\u63a5\u7d2f\u52a0\u8ba1\u6570\uff0c\u4e0d\u9700\u8981\u989d\u5916\u7684 NR \u903b\u8f91<\/li>\n<li><code>@csv<\/code> \u81ea\u52a8\u5904\u7406\u6a21\u578b\u540d\u91cc\u53ef\u80fd\u542b\u9017\u53f7\u7684\u60c5\u51b5\uff08\u7528\u5f15\u53f7\u5305\u88f9\u5b57\u7b26\u4e32\u5b57\u6bb5\uff09<\/li>\n<li><code>2&gt;\/dev\/null<\/code> \u9759\u9ed8\u6389\u635f\u574f\u7684 JSONL \u884c\uff08\u88ab\u4e2d\u65ad\u7684\u8bf7\u6c42\u53ef\u80fd\u5199\u4e86\u4e0d\u5b8c\u6574\u7684 JSON\uff09<\/li>\n<\/ul>\n<h3>3. awk \u5206\u7ec4\u805a\u5408<\/h3>\n<pre><code class=\"bash\">MODEL_LINES=$(echo &quot;$RAW&quot; | awk -F, '\n{\n  gsub(\/&quot;\/, &quot;&quot;, $1)       # \u53bb\u6389 @csv \u7ed9\u6a21\u578b\u540d\u52a0\u7684\u5f15\u53f7\n  m = $1\n  mi[m]  += $2            # input tokens\n  mo[m]  += $3            # output tokens\n  mch[m] += $4            # cache hit\n  mc5[m] += $5            # cache write 5m\n  mc1[m] += $6            # cache write 1h\n  mcalls[m] += $7         # call count\n}\nEND {\n  for (m in mcalls) print m, mi[m], mo[m], mch[m], mc5[m], mc1[m], mcalls[m]\n}\n' | sort)<\/code><\/pre>\n<p>\u7528\u5173\u8054\u6570\u7ec4\uff08awk \u7684 <code>array[key]<\/code>\uff09\u6309\u6a21\u578b\u540d\u5206\u7ec4\uff0cO(n) \u626b\u63cf\u5b8c\u6210\u805a\u5408\u3002<\/p>\n<h3>4. \u4ef7\u683c\u8868\u67e5\u8be2\uff1abash \u51fd\u6570 + case<\/h3>\n<pre><code class=\"bash\">model_price() {\n  local m=&quot;$1&quot;\n  case &quot;$m&quot; in\n    *opus*)   echo &quot;5.00 25.00 0.50 6.25 10.00&quot; ;;\n    *haiku*)  echo &quot;1.00  5.00 0.10 1.25  2.00&quot; ;;\n    *gemini*) echo &quot;0 0 0 0 0&quot; ;;\n    *)        echo &quot;3.00 15.00 0.30 3.75  6.00&quot; ;;  # Sonnet \/ \u672a\u77e5\n  esac\n}<\/code><\/pre>\n<p>glob \u6a21\u5f0f\u5339\u914d\uff08<code>*opus*<\/code>\uff09\u53ef\u4ee5\u540c\u65f6\u8986\u76d6 <code>claude-opus-4-5<\/code>\u3001<code>claude-opus-4-6<\/code> \u7b49\u7248\u672c\u53d8\u4f53\u3002Gemini \u8d70\u7684\u662f Google \u7684\u8ba1\u8d39\u4f53\u7cfb\uff0c\u7edf\u4e00\u8fd4\u56de 0 \u4e0d\u5f71\u54cd\u603b\u8ba1\u3002<\/p>\n<h3>5. \u6d6e\u70b9\u8ba1\u7b97\uff1a\u5168\u90e8\u4ea4\u7ed9 awk<\/h3>\n<p>bash \u672c\u8eab\u4e0d\u652f\u6301\u6d6e\u70b9\uff0c\u8d39\u7528\u8ba1\u7b97\u5168\u90e8\u901a\u8fc7 <code>awk BEGIN<\/code> \u5757\u5b8c\u6210\uff1a<\/p>\n<pre><code class=\"bash\">mcost=$(awk -v i=&quot;$mi&quot; -v o=&quot;$mo&quot; -v ch=&quot;$mch&quot; -v c5=&quot;$mc5&quot; -v c1=&quot;$mc1&quot; \/\n            -v pi=&quot;$pi&quot; -v po=&quot;$po&quot; -v ph=&quot;$ph&quot; -v p5=&quot;$p5&quot; -v p1=&quot;$p1&quot; '\n  BEGIN { printf &quot;%.4f&quot;, (i*pi + o*po + ch*ph + c5*p5 + c1*p1)\/1000000 }')<\/code><\/pre>\n<p>\u7d2f\u52a0\u540c\u7406\uff0c\u907f\u514d\u7528 <code>bc<\/code> \u6216 Python\uff08\u51cf\u5c11\u4f9d\u8d56\uff09\uff1a<\/p>\n<pre><code class=\"bash\">TOTAL_COST=$(awk -v a=&quot;$TOTAL_COST&quot; -v b=&quot;$mcost&quot; 'BEGIN{printf &quot;%.4f&quot;, a+b}')<\/code><\/pre>\n<h3>6. \u6570\u5b57\u5343\u4f4d\u5206\u9694\u7b26\u683c\u5f0f\u5316<\/h3>\n<pre><code class=\"bash\">fmt() {\n  printf '%d\/n' &quot;$1&quot; | awk '\n    {\n      while(length($0)&gt;3) {\n        s = &quot;,&quot; substr($0, length($0)-2) s\n        $0 = substr($0, 1, length($0)-3)\n      }\n      print $0 s\n    }'\n}<\/code><\/pre>\n<p>\u7eaf awk \u5b9e\u73b0\u5343\u4f4d\u5206\u7ec4\uff0c\u4e0d\u4f9d\u8d56 <code>numfmt<\/code> \u6216 <code>printf &quot;%'d&quot;<\/code>\uff08macOS \u7684 printf \u4e0d\u652f\u6301 <code>'<\/code> \u4fee\u9970\u7b26\uff09\u3002<\/p>\n<hr>\n<h2>\u6700\u7ec8\u8f93\u51fa\u6548\u679c<\/h2>\n<pre><code>\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\n  Claude Token Usage \u2014 Last 7 days\n\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\n  API calls:                 1,847\n\n  Input tokens:              28,431\n  Output tokens:             312,058\n  Cache hit tokens:          21,493,612\n  Cache write 5m tokens:     1,876,234\n  Cache write 1h tokens:     0\n  Total input:               23,398,277\n\n  Est. cost (USD):           $18.4231\n\n  By model:\n  claude-haiku-4-5    calls=48    $0.2134\n  claude-haiku-4-6    calls=23    $0.1027\n  claude-sonnet-4-5   calls=201   $2.8841\n  claude-sonnet-4-6   calls=1571  $15.1823\n  claude-sonnet-4-7   calls=4     $0.0406\n\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501<\/code><\/pre>\n<hr>\n<h2>\u8ba1\u8d39\u516c\u5f0f\u6c47\u603b<\/h2>\n<p>\u6bcf\u4e2a\u6a21\u578b\u7684\u8d39\u7528\uff08\u4ee5 Sonnet \u4e3a\u4f8b\uff09\uff1a<\/p>\n<pre><code>cost =  input_tokens              \u00d7 $3.00  \/ 1,000,000\n      + output_tokens             \u00d7 $15.00 \/ 1,000,000\n      + cache_read_input_tokens   \u00d7 $0.30  \/ 1,000,000   \u2190 0.1\u00d7 input\n      + cache_write_5m_tokens     \u00d7 $3.75  \/ 1,000,000   \u2190 1.25\u00d7 input\n      + cache_write_1h_tokens     \u00d7 $6.00  \/ 1,000,000   \u2190 2\u00d7 input\n\ntotal_cost = \u03a3 cost(model_i)<\/code><\/pre>\n<p>\u5404\u6a21\u578b\u5b8c\u6574\u5b9a\u4ef7\uff08per 1M tokens\uff09\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u6a21\u578b<\/th>\n<th align=\"right\">Input<\/th>\n<th align=\"right\">Output<\/th>\n<th align=\"right\">Cache Read<\/th>\n<th align=\"right\">Cache Write 5m<\/th>\n<th align=\"right\">Cache Write 1h<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Opus 4.x<\/td>\n<td align=\"right\">$5.00<\/td>\n<td align=\"right\">$25.00<\/td>\n<td align=\"right\">$0.50<\/td>\n<td align=\"right\">$6.25<\/td>\n<td align=\"right\">$10.00<\/td>\n<\/tr>\n<tr>\n<td>Sonnet 4.x<\/td>\n<td align=\"right\">$3.00<\/td>\n<td align=\"right\">$15.00<\/td>\n<td align=\"right\">$0.30<\/td>\n<td align=\"right\">$3.75<\/td>\n<td align=\"right\">$6.00<\/td>\n<\/tr>\n<tr>\n<td>Haiku 4.x<\/td>\n<td align=\"right\">$1.00<\/td>\n<td align=\"right\">$5.00<\/td>\n<td align=\"right\">$0.10<\/td>\n<td align=\"right\">$1.25<\/td>\n<td align=\"right\">$2.00<\/td>\n<\/tr>\n<tr>\n<td>Gemini<\/td>\n<td align=\"right\">\u2014<\/td>\n<td align=\"right\">\u2014<\/td>\n<td align=\"right\">\u2014<\/td>\n<td align=\"right\">\u2014<\/td>\n<td align=\"right\">\u2014<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote><p>\u5b9a\u4ef7\u6765\u6e90\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=Pxvvhm363VgUaLuqUJx5WQ%3D%3D.z94wvTF6uWMz%2Bf2IR94hsKnqI%2BhlVkrTBuAcDEdikDo8Wl8AqxmAYBUUdnfzk6RK19jJWE6LjK7l%2FJr1pn6Agtoy7iYwl3txr6CrpXjHWEM%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Anthropic \u5b98\u65b9\u6587\u6863 \u2014 Prompt Caching<\/a><br \/>\u6a21\u578b\u5339\u914d\u89c4\u5219\uff1a\u540d\u79f0\u542b <code>opus<\/code> \u2192 Opus\uff0c\u542b <code>haiku<\/code> \u2192 Haiku\uff0c\u542b <code>gemini<\/code> \u2192 \u4e0d\u8ba1\u8d39\uff0c\u5176\u4f59\u9ed8\u8ba4 Sonnet\u3002<\/p><\/blockquote>\n<hr>\n<h2>\u540e\u8bb0\uff1a\u8fd9\u7bc7\u6587\u7ae0\u672c\u8eab\u82b1\u4e86\u591a\u5c11\u94b1\uff1f<\/h2>\n<p>\u8fd9\u7bc7\u6587\u7ae0\u4ece\u6784\u601d\u3001\u63a2\u7d22\u4ee3\u7801\u3001\u5230\u5199\u4f5c\uff0c\u5168\u7a0b\u7531 Claude Code \u5b8c\u6210\u3002\u5199\u5b8c\u4e4b\u540e\u987a\u624b\u8dd1\u4e86\u4e00\u4e0b\u811a\u672c\uff1a<\/p>\n<p>\u7531\u4e8e\u4eca\u5929\u8fd8\u6709\u5176\u4ed6\u9879\u76ee\u5728\u4f7f\u7528\uff0c\u76f4\u63a5\u770b <code>today<\/code> \u6570\u636e\u4f1a\u6df7\u5165\u65e0\u5173\u7684\u5bf9\u8bdd\u3002\u811a\u672c\u652f\u6301\u7684\u6700\u5c0f\u7c92\u5ea6\u662f\u5929\uff0c\u6240\u4ee5\u4e34\u65f6\u7528 jq \u8fc7\u6ee4\u6700\u8fd1 1 \u5c0f\u65f6\uff1a<\/p>\n<pre><code class=\"bash\">SINCE=$(date -u -v-1H +&quot;%Y-%m-%dT%H:%M:%S&quot;)\nfind ~\/.claude\/projects -name &quot;*.jsonl&quot; | xargs jq -r --arg since &quot;$SINCE&quot; '\n  select(\n    .type == &quot;assistant&quot;\n    and .message.usage != null\n    and .timestamp &gt;= $since\n  ) |\n  [(.message.model \/\/ &quot;unknown&quot;),\n   (.message.usage.input_tokens \/\/ 0),\n   (.message.usage.output_tokens \/\/ 0),\n   (.message.usage.cache_read_input_tokens \/\/ 0),\n   (.message.usage.cache_creation.ephemeral_5m_input_tokens \/\/ 0),\n   (.message.usage.cache_creation.ephemeral_1h_input_tokens \/\/ 0),\n   1] | @csv\n' | awk -F, '\n{\n  gsub(\/&quot;\/, &quot;&quot;, $1)\n  input += $2; output += $3; cache_hit += $4; cache_5m += $5; calls += $7\n}\nEND {\n  cost = (input*3 + output*15 + cache_hit*0.3 + cache_5m*3.75) \/ 1000000\n  printf &quot;Calls: %d  Output: %d  Cache hit: %d  Est cost: $%.4f\/n&quot;,\n    calls, output, cache_hit, cost\n}'<\/code><\/pre>\n<p>\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\n  Claude Token Usage \u2014 Last 1 hour\n\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\n  API calls:                 30\n\n  Input tokens:              644\n  Output tokens:             13,102\n  Cache hit tokens:          512,694\n  Cache write 5m tokens:     115,809\n  Cache write 1h tokens:     0\n  Total input:               629,147\n\n  Est. cost (USD):           $0.7866\n\n  By model:\n  claude-sonnet-4-6    calls=30     $0.7866\n\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501<\/code><\/pre>\n<p>\u5199\u8fd9\u7bc7\u6587\u7ae0\uff1a<strong>30 \u6b21 API \u8c03\u7528\uff0c\u7ea6 $0.79<\/strong>\u3002<\/p>\n<p>\u8f93\u51fa\u4e86 13,102 \u4e2a token \u7684\u6587\u7ae0\u5185\u5bb9\uff0c\u80cc\u540e\u662f 51 \u4e07 cache hit token \u5728\u6491\u7740\u4e0a\u4e0b\u6587\u2014\u2014\u6bcf\u8f6e\u5bf9\u8bdd\u90fd\u628a\u6574\u4e2a\u4ee3\u7801\u6587\u4ef6\u3001\u5bf9\u8bdd\u5386\u53f2\u5e26\u8fdb\u6765\uff0c\u547d\u4e2d\u7f13\u5b58\u53ea\u6536 0.1\u00d7 \u7684\u4ef7\u683c\uff0c\u5426\u5219\u8d39\u7528\u4f1a\u9ad8\u51fa\u6570\u500d\u3002<\/p>\n<p>\u7528\u8fd9\u4e2a\u811a\u672c\u7edf\u8ba1\u5199\u8fd9\u7bc7\u4ecb\u7ecd\u8fd9\u4e2a\u811a\u672c\u7684\u6587\u7ae0\u7684\u82b1\u8d39\u2014\u2014\u8fd8\u633a\u6709\u9012\u5f52\u7684\u610f\u601d\u3002<\/p>\n<hr>\n<h2>\u6570\u636e\u5c40\u9650\u6027\u8bf4\u660e<\/h2>\n<p>\u8fd9\u5957\u65b9\u6848\u7684\u8fb9\u754c\u5f88\u6e05\u695a\uff1a<\/p>\n<ul>\n<li><strong>\u53ea\u7edf\u8ba1\u672c\u673a<\/strong>\uff1a\u5176\u4ed6\u8bbe\u5907\u7684\u5bf9\u8bdd\u4e0d\u5728 <code>~\/.claude\/projects\/<\/code> \u91cc<\/li>\n<li><strong>\u4e0d\u542b\u7f51\u9875\u7248<\/strong>\uff1aclaude.ai \u7684\u5bf9\u8bdd\u4e0d\u5199\u672c\u5730\u65e5\u5fd7<\/li>\n<li><strong>\u88ab\u4e2d\u65ad\u7684\u8bf7\u6c42<\/strong>\uff08\u7ea6 1~2%\uff09\u6ca1\u6709 <code>usage<\/code> \u5b57\u6bb5\uff0c\u4e0d\u8ba1\u5165<\/li>\n<li><strong>AWS Bedrock \u5b9a\u4ef7<\/strong>\u4e0e Anthropic API \u57fa\u672c\u4e00\u81f4\uff0c\u90e8\u5206\u533a\u57df\u53ef\u80fd\u6709\u8f7b\u5fae\u5dee\u5f02<\/li>\n<\/ul>\n<hr>\n<h2>\u4e0e claude-hud \u7684\u5b9a\u4f4d\u5dee\u5f02<\/h2>\n<table>\n<thead>\n<tr>\n<th> <\/th>\n<th>claude-hud<\/th>\n<th>token-usage skill<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u9002\u7528\u8ba1\u8d39\u65b9\u5f0f<\/td>\n<td>Claude.ai Pro\/Max \u8ba2\u9605\u989d\u5ea6<\/td>\n<td>Anthropic API \/ AWS Bedrock<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u7c92\u5ea6<\/td>\n<td>\u5f53\u524d session \u5b9e\u65f6<\/td>\n<td>today \/ week \/ month \/ all<\/td>\n<\/tr>\n<tr>\n<td>\u9879\u76ee\u8fc7\u6ee4<\/td>\n<td>\u65e0<\/td>\n<td><code>--project &lt;name&gt;<\/code><\/td>\n<\/tr>\n<tr>\n<td>\u8fd0\u884c\u65b9\u5f0f<\/td>\n<td>\u7ec8\u7aef\u72b6\u6001\u680f\u5b9e\u65f6\u663e\u793a<\/td>\n<td>\u6309\u9700\u67e5\u8be2<\/td>\n<\/tr>\n<tr>\n<td>\u79bb\u7ebf\u53ef\u7528<\/td>\n<td>\u662f<\/td>\n<td>\u662f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e24\u4e2a\u5de5\u5177\u5e76\u4e0d\u51b2\u7a81\uff0c\u5404\u81ea\u89e3\u51b3\u4e0d\u540c\u573a\u666f\u7684\u95ee\u9898\u3002<\/p>\n<hr>\n<h2>\u4f9d\u8d56<\/h2>\n<ul>\n<li><code>bash<\/code> \u2014 macOS \/ Linux \u5185\u7f6e<\/li>\n<li><code>jq<\/code> \u2014 <code>brew install jq<\/code> \u6216 <code>apt install jq<\/code><\/li>\n<li><code>awk<\/code> \u2014 \u7cfb\u7edf\u5185\u7f6e<\/li>\n<\/ul>\n<p>\u6574\u4e2a\u5b9e\u73b0\u4e0d\u5230 160 \u884c bash\uff0c\u6ca1\u6709 Python\u3001Node \u6216\u5176\u4ed6\u8fd0\u884c\u65f6\u4f9d\u8d56\u3002<\/p>\n<hr>\n<h2>\u5b8c\u6574\u4ee3\u7801<\/h2>\n<pre><code class=\"bash\">#!\/usr\/bin\/env bash\n# Claude Code Token Usage Statistics\n# Usage: token-stats.sh [today|week|month|all] [--project &lt;name&gt;]\n\nCLAUDE_DIR=&quot;${CLAUDE_CONFIG_DIR:-$HOME\/.claude}&quot;\nPERIOD=&quot;${1:-today}&quot;\nPROJECT_FILTER=&quot;&quot;\n\n# Parse args\nwhile [[ $# -gt 0 ]]; do\n  case &quot;$1&quot; in\n    --project|-p) PROJECT_FILTER=&quot;$2&quot;; shift 2 ;;\n    today|week|month|all) PERIOD=&quot;$1&quot;; shift ;;\n    *) shift ;;\n  esac\ndone\n\n# Determine date cutoff\ncase &quot;$PERIOD&quot; in\n  today)\n    SINCE=$(date -u +&quot;%Y-%m-%dT00:00:00&quot;)\n    LABEL=&quot;Today&quot;\n    ;;\n  week)\n    SINCE=$(date -u -v-7d +&quot;%Y-%m-%dT00:00:00&quot; 2&gt;\/dev\/null || date -u -d &quot;7 days ago&quot; +&quot;%Y-%m-%dT00:00:00&quot;)\n    LABEL=&quot;Last 7 days&quot;\n    ;;\n  month)\n    SINCE=$(date -u -v-30d +&quot;%Y-%m-%dT00:00:00&quot; 2&gt;\/dev\/null || date -u -d &quot;30 days ago&quot; +&quot;%Y-%m-%dT00:00:00&quot;)\n    LABEL=&quot;Last 30 days&quot;\n    ;;\n  all)\n    SINCE=&quot;1970-01-01T00:00:00&quot;\n    LABEL=&quot;All time&quot;\n    ;;\nesac\n\n# Find relevant jsonl files\nif [[ -n &quot;$PROJECT_FILTER&quot; ]]; then\n  FILES=$(find &quot;$CLAUDE_DIR\/projects&quot; -path &quot;*${PROJECT_FILTER}*&quot; -name &quot;*.jsonl&quot; 2&gt;\/dev\/null)\nelse\n  FILES=$(find &quot;$CLAUDE_DIR\/projects&quot; -name &quot;*.jsonl&quot; 2&gt;\/dev\/null)\nfi\n\nif [[ -z &quot;$FILES&quot; ]]; then\n  echo &quot;No data found.&quot;\n  exit 0\nfi\n\n# Aggregate stats via jq, grouped by model\n# Fields: model, input, output, cache_hit, cache_write_5m, cache_write_1h\nRAW=$(echo &quot;$FILES&quot; | xargs jq -r --arg since &quot;$SINCE&quot; '\n  select(\n    .type == &quot;assistant&quot;\n    and .message.usage != null\n    and .timestamp != null\n    and .timestamp &gt;= $since\n  ) |\n  [\n    (.message.model \/\/ &quot;unknown&quot;),\n    (.message.usage.input_tokens \/\/ 0),\n    (.message.usage.output_tokens \/\/ 0),\n    (.message.usage.cache_read_input_tokens \/\/ 0),\n    (.message.usage.cache_creation.ephemeral_5m_input_tokens \/\/ 0),\n    (.message.usage.cache_creation.ephemeral_1h_input_tokens \/\/ 0),\n    1\n  ] | @csv\n' 2&gt;\/dev\/null)\n\n# Sum totals across all models\nSTATS=$(echo &quot;$RAW&quot; | awk -F, '\n{\n  gsub(\/&quot;\/, &quot;&quot;, $1)\n  input     += $2\n  output    += $3\n  cache_hit += $4\n  cache_5m  += $5\n  cache_1h  += $6\n  calls     += $7\n}\nEND { print input, output, cache_hit, cache_5m, cache_1h, calls }\n')\nread -r INPUT OUTPUT CACHE_HIT CACHE_5M CACHE_1H CALLS &lt;&lt;&lt; &quot;$STATS&quot;\n\nCACHE_NEW=$((CACHE_5M + CACHE_1H))\nTOTAL_INPUT=$((INPUT + CACHE_HIT + CACHE_NEW))\n\n# Per-model pricing (Anthropic API \/ AWS Bedrock us-east-1, per 1M tokens)\n# Returns: &quot;in out hit w5m w1h&quot; for a given model string\nmodel_price() {\n  local m=&quot;$1&quot;\n  case &quot;$m&quot; in\n    *opus*)   echo &quot;5.00 25.00 0.50 6.25 10.00&quot; ;;\n    *haiku*)  echo &quot;1.00  5.00 0.10 1.25  2.00&quot; ;;\n    *gemini*) echo &quot;0 0 0 0 0&quot; ;;   # Gemini: no Anthropic billing\n    *)        echo &quot;3.00 15.00 0.30 3.75  6.00&quot; ;;  # sonnet \/ unknown\n  esac\n}\n\n# Per-model cost breakdown\nMODEL_LINES=$(echo &quot;$RAW&quot; | awk -F, '\n{\n  gsub(\/&quot;\/, &quot;&quot;, $1)\n  m = $1\n  mi[m]  += $2; mo[m]  += $3; mch[m] += $4\n  mc5[m] += $5; mc1[m] += $6; mcalls[m] += $7\n}\nEND {\n  for (m in mcalls) print m, mi[m], mo[m], mch[m], mc5[m], mc1[m], mcalls[m]\n}\n' | sort)\n\n# Compute total cost by summing per-model costs\nTOTAL_COST=0\nMODEL_COST_LINES=&quot;&quot;\nwhile IFS= read -r line; do\n  m=$(echo &quot;$line&quot; | awk '{print $1}')\n  mi=$(echo &quot;$line&quot; | awk '{print $2}')\n  mo=$(echo &quot;$line&quot; | awk '{print $3}')\n  mch=$(echo &quot;$line&quot; | awk '{print $4}')\n  mc5=$(echo &quot;$line&quot; | awk '{print $5}')\n  mc1=$(echo &quot;$line&quot; | awk '{print $6}')\n  mcalls=$(echo &quot;$line&quot; | awk '{print $7}')\n  prices=$(model_price &quot;$m&quot;)\n  pi=$(echo $prices | awk '{print $1}')\n  po=$(echo $prices | awk '{print $2}')\n  ph=$(echo $prices | awk '{print $3}')\n  p5=$(echo $prices | awk '{print $4}')\n  p1=$(echo $prices | awk '{print $5}')\n  mcost=$(awk -v i=&quot;$mi&quot; -v o=&quot;$mo&quot; -v ch=&quot;$mch&quot; -v c5=&quot;$mc5&quot; -v c1=&quot;$mc1&quot; \/\n              -v pi=&quot;$pi&quot; -v po=&quot;$po&quot; -v ph=&quot;$ph&quot; -v p5=&quot;$p5&quot; -v p1=&quot;$p1&quot; '\n    BEGIN { printf &quot;%.4f&quot;, (i*pi + o*po + ch*ph + c5*p5 + c1*p1)\/1000000 }')\n  TOTAL_COST=$(awk -v a=&quot;$TOTAL_COST&quot; -v b=&quot;$mcost&quot; 'BEGIN{printf &quot;%.4f&quot;, a+b}')\n  MODEL_COST_LINES=&quot;${MODEL_COST_LINES}  ${m}  calls=${mcalls}  \/$${mcost}\/n&quot;\ndone &lt;&lt;&lt; &quot;$MODEL_LINES&quot;\n\nfmt() { printf '%d\/n' &quot;$1&quot; | awk '{while(length($0)&gt;3){s=&quot;,&quot;substr($0,length($0)-2)s;$0=substr($0,1,length($0)-3)}print $0 s}'; }\n\necho &quot;&quot;\necho &quot;\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501&quot;\necho &quot;  Claude Token Usage \u2014 $LABEL&quot;\n[[ -n &quot;$PROJECT_FILTER&quot; ]] &amp;&amp; echo &quot;  Project: $PROJECT_FILTER&quot;\necho &quot;\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501&quot;\nprintf &quot;  %-26s %s\/n&quot;   &quot;API calls:&quot;             &quot;$CALLS&quot;\necho &quot;&quot;\nprintf &quot;  %-26s %s\/n&quot;   &quot;Input tokens:&quot;           &quot;$(fmt $INPUT)&quot;\nprintf &quot;  %-26s %s\/n&quot;   &quot;Output tokens:&quot;          &quot;$(fmt $OUTPUT)&quot;\nprintf &quot;  %-26s %s\/n&quot;   &quot;Cache hit tokens:&quot;       &quot;$(fmt $CACHE_HIT)&quot;\nprintf &quot;  %-26s %s\/n&quot;   &quot;Cache write 5m tokens:&quot;  &quot;$(fmt $CACHE_5M)&quot;\nprintf &quot;  %-26s %s\/n&quot;   &quot;Cache write 1h tokens:&quot;  &quot;$(fmt $CACHE_1H)&quot;\nprintf &quot;  %-26s %s\/n&quot;   &quot;Total input:&quot;            &quot;$(fmt $TOTAL_INPUT)&quot;\necho &quot;&quot;\nprintf &quot;  %-26s \/$%s\/n&quot; &quot;Est. cost (USD):&quot;        &quot;$TOTAL_COST&quot;\necho &quot;&quot;\necho &quot;  By model:&quot;\nprintf &quot;%b&quot; &quot;$MODEL_COST_LINES&quot;\necho &quot;\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501&quot;\necho &quot;&quot;<\/code><\/pre>\n<hr>\n<h2>\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<h3>\u5b89\u88c5\u4f9d\u8d56<\/h3>\n<pre><code class=\"bash\"># macOS\nbrew install jq\n\n# Ubuntu \/ Debian\napt install jq<\/code><\/pre>\n<h3>\u4e0b\u8f7d\u811a\u672c<\/h3>\n<pre><code class=\"bash\">mkdir -p ~\/.claude\/skills\/token-usage\/scripts\ncurl -o ~\/.claude\/skills\/token-usage\/scripts\/token-stats.sh \/\n  https:\/\/raw.githubusercontent.com\/your-repo\/token-stats.sh\nchmod +x ~\/.claude\/skills\/token-usage\/scripts\/token-stats.sh<\/code><\/pre>\n<p>\u6216\u8005\u76f4\u63a5\u628a\u4e0a\u9762\u7684\u5b8c\u6574\u4ee3\u7801\u7c98\u8d34\u5230 <code>~\/.claude\/skills\/token-usage\/scripts\/token-stats.sh<\/code>\uff0c\u7136\u540e <code>chmod +x<\/code>\u3002<\/p>\n<h3>\u8fd0\u884c\u793a\u4f8b<\/h3>\n<pre><code class=\"bash\"># \u67e5\u770b\u4eca\u5929\u7684\u7528\u91cf\uff08\u9ed8\u8ba4\uff09\nbash ~\/.claude\/skills\/token-usage\/scripts\/token-stats.sh\n\n# \u67e5\u770b\u672c\u5468\nbash ~\/.claude\/skills\/token-usage\/scripts\/token-stats.sh week\n\n# \u67e5\u770b\u8fd1 30 \u5929\nbash ~\/.claude\/skills\/token-usage\/scripts\/token-stats.sh month\n\n# \u67e5\u770b\u5168\u90e8\u5386\u53f2\nbash ~\/.claude\/skills\/token-usage\/scripts\/token-stats.sh all\n\n# \u6309\u9879\u76ee\u8fc7\u6ee4\uff08\u652f\u6301\u90e8\u5206\u5339\u914d\uff09\nbash ~\/.claude\/skills\/token-usage\/scripts\/token-stats.sh week --project api-service\n\n# \u7f29\u5199\u5f62\u5f0f\nbash ~\/.claude\/skills\/token-usage\/scripts\/token-stats.sh month -p dashboard<\/code><\/pre>\n<h3>\u52a0\u4e2a\u522b\u540d\u65b9\u4fbf\u8c03\u7528<\/h3>\n<p>\u5728 <code>~\/.zshrc<\/code> \u6216 <code>~\/.bashrc<\/code> \u91cc\u52a0\u4e00\u884c\uff1a<\/p>\n<pre><code class=\"bash\">alias token-usage='bash ~\/.claude\/skills\/token-usage\/scripts\/token-stats.sh'<\/code><\/pre>\n<p>\u4e4b\u540e\u76f4\u63a5\uff1a<\/p>\n<pre><code class=\"bash\">token-usage          # \u4eca\u5929\ntoken-usage week     # \u672c\u5468\ntoken-usage all -p fe-dashboard  # \u5168\u90e8\u5386\u53f2 \u00d7 \u6307\u5b9a\u9879\u76ee<\/code><\/pre>\n<h3>\u4f5c\u4e3a Claude Code Skill \u4f7f\u7528<\/h3>\n<p>\u5982\u679c\u4f60\u4f7f\u7528 Claude Code\uff0c\u53ef\u4ee5\u628a\u8fd9\u4e2a\u811a\u672c\u6ce8\u518c\u4e3a skill\uff0c\u76f4\u63a5\u7528\u81ea\u7136\u8bed\u8a00\u89e6\u53d1\uff1a<\/p>\n<ul>\n<li>&#8220;\u67e5\u4e00\u4e0b\u4eca\u5929\u7684 token \u7528\u91cf&#8221;<\/li>\n<li>&#8220;\u672c\u5468\u82b1\u4e86\u591a\u5c11\u94b1&#8221;<\/li>\n<li>&#8220;all time \u7684\u8d39\u7528\u4f30\u7b97&#8221;<\/li>\n<li>&#8220;my-project \u9879\u76ee\u8fd9\u5468\u7684\u7528\u91cf&#8221;<\/li>\n<\/ul>\n<p>Claude \u4f1a\u81ea\u52a8\u8c03\u7528\u811a\u672c\u5e76\u5c55\u793a\u7ed3\u679c\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047666964\">https:\/\/segmentfault.com\/a\/1190000047666964<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ece\u4e00\u4e2a HUD \u72b6\u6001\u680f\u5de5\u5177\u51fa\u53d1\uff0c\u4e00\u8def\u7ffb\u65e5\u5fd7\u3001\u6252 JSON\u3001\u5199 bash\uff0c\u6700\u7ec8\u641e\u5b9a\u4e00\u5957\u79bb\u7ebf token \u7528\u91cf\u7edf\u8ba1 [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021323","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021323"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021323\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}