AgentSkillsCN

ai-coding-usage

分析用户在 Claude Code 和 Cursor 中的 AI 编码助手使用模式。当用户询问其编码使用情况、工具统计信息、生产力规律、技能热度、会话历史,或希望查询自己的 AI 编码日志时,可调用此技能。触发关键词包括:“使用情况”、“我用了多少”、“最常用工具”、“技能热度”、“编码数据”、“生产力规律”。

SKILL.md
--- frontmatter
name: ai-coding-usage
description: Analyze AI coding assistant usage patterns across Claude Code and Cursor. Use when user asks about their coding usage, tool statistics, productivity patterns, skill popularity, session history, or wants to query their AI coding logs. Triggers include "usage", "how much have I used", "most used tools", "skill popularity", "coding stats", "productivity patterns".
license: Apache-2.0

AI Coding Usage

Unified usage analyzer for Claude Code and Cursor. Loads logs into DuckDB for SQL analysis.

Quick Start

bash
# Run the script (loads data on first run, incremental updates after)
scripts/ai-coding-usage

# Show database schema and example queries
scripts/ai-coding-usage --schema

# Query your data
scripts/ai-coding-usage query "SELECT * FROM tool_summary"

Commands

CommandDescription
(default)Auto-detect changes, incremental update, show summary
updateExplicit incremental update
reloadForce reload all data (with backup)
query "SQL"Execute SQL query
search "query"Search conversation content
shellInteractive DuckDB shell
--schemaDatabase schema with example queries
--helpFull help documentation

Search

bash
# ILIKE search on conversation content (default)
scripts/ai-coding-usage search "memory"

# BM25 full-text search (covers content + thinking)
scripts/ai-coding-usage search "memory" --fts

# Search reasoning traces
scripts/ai-coding-usage search "memory" --thinking

# Search both content and thinking
scripts/ai-coding-usage search "memory" --all

# Filter by role, repo, time
scripts/ai-coding-usage search "refactor" --user --repo bertram-chat --since 7d

# Limit results
scripts/ai-coding-usage search "deploy" -n 20

Common Queries

sql
-- Most used tools
SELECT * FROM tool_summary;

-- Daily usage (last 2 weeks)
SELECT * FROM daily_summary ORDER BY date DESC LIMIT 14;

-- Skill popularity
SELECT regexp_extract(context, '"skill":"([^"]+)"', 1) as skill, COUNT(*) as uses
FROM claude_tools WHERE tool_name = 'Skill'
GROUP BY skill ORDER BY uses DESC;

-- Peak coding hours
SELECT hour_of_day, SUM(interactions) as total
FROM peak_hours GROUP BY hour_of_day ORDER BY total DESC LIMIT 5;

-- Activity by repository (aggregates worktrees)
SELECT repo_name, SUM(interactions) as total, SUM(worktrees) as branches
FROM repo_activity GROUP BY repo_name ORDER BY total DESC LIMIT 10;

-- Turn durations
SELECT * FROM turn_durations ORDER BY duration_ms DESC LIMIT 10;

-- Session overview with summaries
SELECT session_id, repo_name, summary FROM session_overview
WHERE summary IS NOT NULL ORDER BY started_at DESC LIMIT 10;

-- API errors
SELECT * FROM api_errors ORDER BY timestamp DESC LIMIT 10;

-- PR links
SELECT * FROM pr_links;

-- Cost by repo
SELECT repo_name, ROUND(SUM(cost_usd), 2) as cost
FROM usage_with_cost
WHERE CAST(timestamp AS TIMESTAMP) >= CURRENT_DATE - INTERVAL 7 DAY
GROUP BY repo_name ORDER BY cost DESC;

-- Full cost summary by repo and model
SELECT * FROM cost_summary ORDER BY cost_usd DESC;

Cost Calculation

The script tracks tokens and calculates API costs automatically:

Token columns in claude_tools:

  • input_tokens, output_tokens - Direct tokens
  • cache_write_tokens, cache_read_tokens - Prompt caching tokens
  • model - Model used (opus/sonnet/haiku)

Cost views:

  • model_pricing - API rates per million tokens (update when prices change)
  • usage_with_cost - Each row has pre-calculated cost_usd
  • cost_summary - Pre-aggregated by repo/model

Key Tables/Views

Core Tables

  • claude_tools - Tool invocations (with model, tokens, repo/branch, source_file)
  • claude_sessions - Session metadata
  • messages - Conversation content (user text, assistant text + thinking)
  • system_events - System records (turn_duration, api_error, stop_hook_summary)
  • queue_operations - User inputs queued during assistant response
  • pr_links - Session-to-PR mappings
  • _sessions_index - Session metadata from sessions-index.json (summary, first_prompt)
  • _loaded_files - File mtime tracking for incremental loading

Views

  • turn_durations - Response timing from system events
  • api_errors - API error events
  • session_overview - Sessions joined with index metadata
  • interactions - Unified view (Claude + Cursor)
  • conversation_search - Messages with content/thinking previews
  • session_messages - Per-session aggregation with topic
  • recent_conversations - Last 50 sessions
  • conversation_pairs - User/assistant turns joined on parent_uuid
  • message_stats - Daily message volume by harness/role
  • repo_activity - Repository-level summary (aggregates worktrees)
  • project_activity - Project-level with worktree info
  • usage_with_cost - Tool invocations with pre-calculated cost_usd
  • cost_summary - Pre-aggregated costs by repo/model
  • model_pricing - API rates (editable)

Run --schema for complete documentation.