|
| 1 | +name: Token Cost |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + pull_request: |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pull-requests: write |
| 11 | + checks: write |
| 12 | + statuses: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + measure-tokens: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - name: Setup Node.js |
| 21 | + uses: actions/setup-node@v4 |
| 22 | + with: |
| 23 | + node-version: "20" |
| 24 | + |
| 25 | + # pnpm/action-setup@v4 |
| 26 | + - uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda |
| 27 | + name: Install pnpm |
| 28 | + with: |
| 29 | + run_install: false |
| 30 | + |
| 31 | + - name: Get pnpm store directory |
| 32 | + shell: bash |
| 33 | + run: | |
| 34 | + echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV |
| 35 | +
|
| 36 | + - uses: actions/cache@v4 |
| 37 | + name: Setup pnpm cache |
| 38 | + with: |
| 39 | + path: ${{ env.STORE_PATH }} |
| 40 | + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} |
| 41 | + restore-keys: | |
| 42 | + ${{ runner.os }}-pnpm-store- |
| 43 | +
|
| 44 | + - name: Install dependencies |
| 45 | + run: pnpm install --no-frozen-lockfile |
| 46 | + |
| 47 | + - name: Build tool definitions |
| 48 | + run: pnpm -w run build |
| 49 | + |
| 50 | + - name: Measure token cost |
| 51 | + id: measure |
| 52 | + working-directory: packages/mcp-server |
| 53 | + run: | |
| 54 | + # Run token counter with JSON output to file |
| 55 | + pnpm run measure-tokens -- -o token-stats.json |
| 56 | +
|
| 57 | + # Extract key metrics from JSON for GitHub outputs |
| 58 | + TOTAL_TOKENS=$(jq -r '.total_tokens' token-stats.json) |
| 59 | + TOOL_COUNT=$(jq -r '.tool_count' token-stats.json) |
| 60 | + AVG_TOKENS=$(jq -r '.avg_tokens_per_tool' token-stats.json) |
| 61 | +
|
| 62 | + # Save for later steps |
| 63 | + echo "total_tokens=$TOTAL_TOKENS" >> $GITHUB_OUTPUT |
| 64 | + echo "tool_count=$TOOL_COUNT" >> $GITHUB_OUTPUT |
| 65 | + echo "avg_tokens=$AVG_TOKENS" >> $GITHUB_OUTPUT |
| 66 | +
|
| 67 | + - name: Generate detailed report |
| 68 | + id: report |
| 69 | + working-directory: packages/mcp-server |
| 70 | + run: | |
| 71 | + # Build markdown table from JSON |
| 72 | + cat > token-report.md <<REPORT_EOF |
| 73 | + ## 📊 MCP Server Token Cost Report |
| 74 | +
|
| 75 | + **Summary** |
| 76 | + - **Total Tokens:** ${{ steps.measure.outputs.total_tokens }} tokens |
| 77 | + - **Tool Count:** ${{ steps.measure.outputs.tool_count }} tools |
| 78 | + - **Average:** ${{ steps.measure.outputs.avg_tokens }} tokens/tool |
| 79 | +
|
| 80 | + ### Per-Tool Breakdown |
| 81 | +
|
| 82 | + | Tool | Tokens | % of Total | |
| 83 | + |------|--------|------------| |
| 84 | + REPORT_EOF |
| 85 | +
|
| 86 | + # Add each tool as a table row |
| 87 | + jq -r '.tools[] | "| `\(.name)` | \(.tokens) | \(.percentage)% |"' token-stats.json >> token-report.md |
| 88 | +
|
| 89 | + cat >> token-report.md <<REPORT_EOF |
| 90 | +
|
| 91 | + --- |
| 92 | +
|
| 93 | + **Note:** This measures the static overhead of tool definitions sent to LLM clients with every request. Lower is better. The \`use_sentry\` tool is excluded as it's only available in agent mode. |
| 94 | + REPORT_EOF |
| 95 | +
|
| 96 | + # Display report |
| 97 | + cat token-report.md |
| 98 | +
|
| 99 | + # Save for summary |
| 100 | + cat token-report.md >> $GITHUB_STEP_SUMMARY |
| 101 | +
|
| 102 | + - name: Download main branch token stats |
| 103 | + if: github.event_name == 'pull_request' |
| 104 | + uses: dawidd6/action-download-artifact@v6 |
| 105 | + continue-on-error: true |
| 106 | + with: |
| 107 | + workflow: token-cost.yml |
| 108 | + branch: main |
| 109 | + name_is_regexp: true |
| 110 | + name: 'token-stats-.*' |
| 111 | + path: main-stats |
| 112 | + search_artifacts: true |
| 113 | + |
| 114 | + - name: Compare with main branch |
| 115 | + id: compare |
| 116 | + if: github.event_name == 'pull_request' |
| 117 | + working-directory: packages/mcp-server |
| 118 | + run: | |
| 119 | + # Check if we got main's stats |
| 120 | + if [ -f ../../main-stats/token-stats.json ]; then |
| 121 | + MAIN_TOKENS=$(jq -r '.total_tokens' ../../main-stats/token-stats.json) |
| 122 | + CURRENT_TOKENS=${{ steps.measure.outputs.total_tokens }} |
| 123 | + DELTA=$((CURRENT_TOKENS - MAIN_TOKENS)) |
| 124 | +
|
| 125 | + echo "has_comparison=true" >> $GITHUB_OUTPUT |
| 126 | + echo "main_tokens=$MAIN_TOKENS" >> $GITHUB_OUTPUT |
| 127 | + echo "delta=$DELTA" >> $GITHUB_OUTPUT |
| 128 | +
|
| 129 | + if [ $DELTA -gt 0 ]; then |
| 130 | + echo "delta_direction=increased" >> $GITHUB_OUTPUT |
| 131 | + echo "delta_symbol=📈" >> $GITHUB_OUTPUT |
| 132 | + elif [ $DELTA -lt 0 ]; then |
| 133 | + echo "delta_direction=decreased" >> $GITHUB_OUTPUT |
| 134 | + echo "delta_symbol=📉" >> $GITHUB_OUTPUT |
| 135 | + else |
| 136 | + echo "delta_direction=unchanged" >> $GITHUB_OUTPUT |
| 137 | + echo "delta_symbol=➡️" >> $GITHUB_OUTPUT |
| 138 | + fi |
| 139 | + else |
| 140 | + echo "has_comparison=false" >> $GITHUB_OUTPUT |
| 141 | + fi |
| 142 | +
|
| 143 | + - name: Add comparison to report |
| 144 | + if: github.event_name == 'pull_request' && steps.compare.outputs.has_comparison == 'true' |
| 145 | + working-directory: packages/mcp-server |
| 146 | + run: | |
| 147 | + # Add comparison section at the top of the report |
| 148 | + cat > comparison-header.md <<COMPARISON_EOF |
| 149 | + ### ${{ steps.compare.outputs.delta_symbol }} Comparison with Main Branch |
| 150 | +
|
| 151 | + - **Current (PR):** ${{ steps.measure.outputs.total_tokens }} tokens |
| 152 | + - **Main branch:** ${{ steps.compare.outputs.main_tokens }} tokens |
| 153 | + - **Change:** ${{ steps.compare.outputs.delta }} tokens (${{ steps.compare.outputs.delta_direction }}) |
| 154 | +
|
| 155 | + --- |
| 156 | +
|
| 157 | + COMPARISON_EOF |
| 158 | +
|
| 159 | + # Insert comparison at the beginning (after the title) |
| 160 | + sed -i '2r comparison-header.md' token-report.md |
| 161 | +
|
| 162 | + # Update job summary with new report |
| 163 | + cat token-report.md >> $GITHUB_STEP_SUMMARY |
| 164 | +
|
| 165 | + - name: Set commit status |
| 166 | + uses: actions/github-script@v7 |
| 167 | + if: always() |
| 168 | + with: |
| 169 | + script: | |
| 170 | + const totalTokens = '${{ steps.measure.outputs.total_tokens }}'; |
| 171 | + const hasComparison = '${{ steps.compare.outputs.has_comparison }}' === 'true'; |
| 172 | + const delta = '${{ steps.compare.outputs.delta }}'; |
| 173 | + const deltaSymbol = '${{ steps.compare.outputs.delta_symbol }}' || '📊'; |
| 174 | +
|
| 175 | + let description = `${totalTokens} tokens`; |
| 176 | + if (hasComparison && delta) { |
| 177 | + const deltaPrefix = parseInt(delta) >= 0 ? '+' : ''; |
| 178 | + description += ` (${deltaPrefix}${delta} from main) ${deltaSymbol}`; |
| 179 | + } |
| 180 | +
|
| 181 | + await github.rest.repos.createCommitStatus({ |
| 182 | + owner: context.repo.owner, |
| 183 | + repo: context.repo.repo, |
| 184 | + sha: context.sha, |
| 185 | + state: 'success', |
| 186 | + context: 'Token Cost', |
| 187 | + description: description, |
| 188 | + target_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}` |
| 189 | + }); |
| 190 | +
|
| 191 | + - name: Upload token stats artifact |
| 192 | + uses: actions/upload-artifact@v4 |
| 193 | + if: always() |
| 194 | + with: |
| 195 | + name: token-stats-${{ github.sha }} |
| 196 | + path: packages/mcp-server/token-stats.json |
| 197 | + retention-days: 90 |
0 commit comments