Skip to content

Commit 9b2fc27

Browse files
wdvrclaude
andauthored
Add token usage and model tracking to Claude billing (#7729)
## Summary - Adds token tracking fields to `misc.claude_code_usage` schema: - `input_tokens`, `output_tokens` - `cache_read_input_tokens`, `cache_creation_input_tokens` - `model` - Updates `upload-claude-usage` action to extract these fields from Claude output - Updates S3 replicator lambda to handle new fields - Creates v2 Grafana dashboard with token metrics ## Dashboard v2 https://pytorchci.grafana.net/public-dashboards/83058a8d65d44a099eb8d9ac2916f411 New metrics: - Total input/output tokens - Cache hit rate - Cost by model breakdown - Token usage by workflow - Daily cache performance ## Test plan - [ ] Deploy schema changes (ALTER TABLE to add columns) - [ ] Deploy lambda changes - [ ] Verify new data flows with token fields populated - [ ] Verify v2 dashboard shows token metrics 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.5 <[email protected]>
1 parent a08e3d5 commit 9b2fc27

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

.github/actions/upload-claude-usage/action.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ runs:
4444
--argjson duration_ms "$(echo "$RESULT" | jq -r '.duration_ms // 0')" \
4545
--argjson num_turns "$(echo "$RESULT" | jq -r '.num_turns // 0')" \
4646
--argjson total_cost_usd "$(echo "$RESULT" | jq -r '.total_cost_usd // 0')" \
47+
--argjson input_tokens "$(echo "$RESULT" | jq -r '.usage.input_tokens // 0')" \
48+
--argjson output_tokens "$(echo "$RESULT" | jq -r '.usage.output_tokens // 0')" \
49+
--argjson cache_read_input_tokens "$(echo "$RESULT" | jq -r '.usage.cache_read_input_tokens // 0')" \
50+
--argjson cache_creation_input_tokens "$(echo "$RESULT" | jq -r '.usage.cache_creation_input_tokens // 0')" \
51+
--arg model "$(echo "$RESULT" | jq -r '.modelUsage | keys[0] // ""')" \
4752
'{
4853
repo: $repo,
4954
run_id: $run_id,
@@ -54,7 +59,12 @@ runs:
5459
timestamp: $timestamp,
5560
duration_ms: $duration_ms,
5661
num_turns: $num_turns,
57-
total_cost_usd: $total_cost_usd
62+
total_cost_usd: $total_cost_usd,
63+
input_tokens: $input_tokens,
64+
output_tokens: $output_tokens,
65+
cache_read_input_tokens: $cache_read_input_tokens,
66+
cache_creation_input_tokens: $cache_creation_input_tokens,
67+
model: $model
5868
}' > /tmp/claude_usage.json
5969
6070
aws s3 cp /tmp/claude_usage.json \

aws/lambda/clickhouse-replicator-s3/lambda_function.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,12 @@ def claude_code_usage_adapter(table, bucket, key):
680680
`timestamp` DateTime64(3),
681681
`duration_ms` Int64,
682682
`num_turns` Int32,
683-
`total_cost_usd` Float64
683+
`total_cost_usd` Float64,
684+
`input_tokens` Int64,
685+
`output_tokens` Int64,
686+
`cache_read_input_tokens` Int64,
687+
`cache_creation_input_tokens` Int64,
688+
`model` String
684689
"""
685690
general_adapter(table, bucket, key, schema, ["none"], "JSONEachRow")
686691

clickhouse_db_schema/misc.claude_code_usage/schema.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ CREATE TABLE misc.claude_code_usage
1010
`duration_ms` Int64,
1111
`num_turns` Int32,
1212
`total_cost_usd` Float64,
13+
`input_tokens` Int64 DEFAULT 0,
14+
`output_tokens` Int64 DEFAULT 0,
15+
`cache_read_input_tokens` Int64 DEFAULT 0,
16+
`cache_creation_input_tokens` Int64 DEFAULT 0,
17+
`model` String DEFAULT '',
1318
`_meta` Tuple(
1419
bucket String,
1520
key String),

0 commit comments

Comments
 (0)