-
Notifications
You must be signed in to change notification settings - Fork 322
feat: Add daily token usage analysis and optimization workflows #24192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,397 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: Claude Token Optimizer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: Analyzes the most expensive Claude workflow identified by the token usage analyzer and creates an optimization issue with Anthropic-specific token-saving recommendations including cache efficiency improvements | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| on: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workflow_run: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workflows: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - "Claude Token Usage Analyzer" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| types: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - completed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| branches: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - main | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workflow_dispatch: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| skip-if-match: 'is:issue is:open in:title "⚡ Claude Token Optimization:"' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| permissions: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| contents: read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| actions: read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| issues: read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pull-requests: read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| engine: copilot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| features: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| copilot-requests: true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| strict: true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tools: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bash: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - "*" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| github: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| toolsets: [default, issues, actions, repos] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| safe-outputs: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| create-issue: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| title-prefix: "⚡ Claude Token Optimization: " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| labels: [automated-analysis, token-optimization, claude, cost-reduction] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expires: 7d | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| max: 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| close-older-issues: true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| noop: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| network: defaults | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeout-minutes: 30 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| steps: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - name: Find and download artifacts from the most expensive Claude workflow | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| env: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| run: | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| set -euo pipefail | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mkdir -p /tmp/token-optimizer-claude | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo "📥 Loading Claude workflow runs from last 24 hours..." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ./gh-aw logs \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --engine claude \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --start-date -1d \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --json \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -c 300 \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > /tmp/token-optimizer-claude/claude-runs.json 2>/dev/null || echo "[]" > /tmp/token-optimizer-claude/claude-runs.json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RUN_COUNT=$(jq '. | length' /tmp/token-optimizer-claude/claude-runs.json 2>/dev/null || echo 0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo "Found ${RUN_COUNT} Claude runs" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [ "$RUN_COUNT" -eq 0 ]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo "No Claude runs found, nothing to optimize" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exit 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Find the most expensive workflow (by total tokens across all its runs) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo "🔍 Identifying most expensive workflow..." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| jq -r ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| group_by(.workflowName) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| map({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+70
to
+74
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| workflow: .[0].workflowName, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total_tokens: (map(.tokenUsage) | add), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| total_cost: (map(.estimatedCost) | add), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| run_count: length, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| avg_tokens: ((map(.tokenUsage) | add) / length), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+73
to
+79
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| group_by(.workflowName) | | |
| map({ | |
| workflow: .[0].workflowName, | |
| total_tokens: (map(.tokenUsage) | add), | |
| total_cost: (map(.estimatedCost) | add), | |
| run_count: length, | |
| avg_tokens: ((map(.tokenUsage) | add) / length), | |
| sort_by(.workflowName) | | |
| group_by(.workflowName) | | |
| map({ | |
| workflow: .[0].workflowName, | |
| total_tokens: (map(.tokenUsage // 0) | add), | |
| total_cost: (map(.estimatedCost // 0) | add), | |
| run_count: length, | |
| avg_tokens: ((map(.tokenUsage // 0) | add) / length), |
Copilot
AI
Apr 3, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Workflow source discovery is brittle: it assumes ${WORKFLOW_NAME} maps to a .md filename via lowercasing/spaces→hyphens, then falls back to grepping for ^name: $WORKFLOW_NAME in markdown. Many workflows in this repo don’t define name: in frontmatter (e.g. .github/workflows/agent-performance-analyzer.md), so this often won’t find the source and the optimizer will skip declared-vs-used tool analysis. Consider locating the matching .lock.yml by its name: and deriving the .md path from the lock filename (or use workflowPath if available in logs output).
| WORKFLOW_MD_NAME=$(echo "$WORKFLOW_NAME" | tr '[:upper:]' '[:lower:]' | tr ' ' '-') | |
| WORKFLOW_MD=".github/workflows/${WORKFLOW_MD_NAME}.md" | |
| if [ -f "$WORKFLOW_MD" ]; then | |
| echo "Found workflow source: $WORKFLOW_MD" | |
| cp "$WORKFLOW_MD" /tmp/token-optimizer-claude/workflow-source.md | |
| else | |
| find .github/workflows -name "*.md" -exec grep -l "^name: $WORKFLOW_NAME" {} \; 2>/dev/null | head -1 | while read -r f; do | |
| echo "Found: $f" | |
| cp "$f" /tmp/token-optimizer-claude/workflow-source.md | |
| done | |
| WORKFLOW_MD="" | |
| if [ -n "${WORKFLOW_PATH:-}" ] && [ -f "$WORKFLOW_PATH" ]; then | |
| WORKFLOW_MD="$WORKFLOW_PATH" | |
| else | |
| WORKFLOW_MD_NAME=$(echo "$WORKFLOW_NAME" | tr '[:upper:]' '[:lower:]' | tr ' ' '-') | |
| CANDIDATE_WORKFLOW_MD=".github/workflows/${WORKFLOW_MD_NAME}.md" | |
| if [ -f "$CANDIDATE_WORKFLOW_MD" ]; then | |
| WORKFLOW_MD="$CANDIDATE_WORKFLOW_MD" | |
| else | |
| LOCK_FILE=$(find .github/workflows -name "*.lock.yml" -exec grep -l "^name: $WORKFLOW_NAME$" {} \; 2>/dev/null | head -1) | |
| if [ -n "$LOCK_FILE" ]; then | |
| CANDIDATE_WORKFLOW_MD="${LOCK_FILE%.lock.yml}.md" | |
| if [ -f "$CANDIDATE_WORKFLOW_MD" ]; then | |
| WORKFLOW_MD="$CANDIDATE_WORKFLOW_MD" | |
| fi | |
| fi | |
| if [ -z "$WORKFLOW_MD" ]; then | |
| WORKFLOW_MD=$(find .github/workflows -name "*.md" -exec grep -l "^name: $WORKFLOW_NAME$" {} \; 2>/dev/null | head -1) | |
| fi | |
| fi | |
| fi | |
| if [ -n "$WORKFLOW_MD" ] && [ -f "$WORKFLOW_MD" ]; then | |
| echo "Found workflow source: $WORKFLOW_MD" | |
| cp "$WORKFLOW_MD" /tmp/token-optimizer-claude/workflow-source.md | |
| else | |
| echo "Workflow source not found for: $WORKFLOW_NAME" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This workflow is named/positioned as the Claude token optimizer, but its frontmatter sets
engine: copilot(and enablescopilot-requests). That means the optimizer itself will be logged as a Copilot-engine run and can skew Copilot usage reports; it also diverges from the intended “Claude” optimization pipeline described in the PR. Consider switchingenginetoclaude(and adjusting features/permissions accordingly) or renaming to make it explicit that Copilot is performing the analysis.