|
| 1 | +--- |
| 2 | +name: AI Spend |
| 3 | +description: Track per-employee Claude AI spend in Cortex using custom metrics, with a full team hierarchy for rollup visibility. |
| 4 | +--- |
| 5 | + |
| 6 | +# AI Spend |
| 7 | + |
| 8 | +Answers the question: **"How much are we spending on Claude AI, and who's spending it?"** |
| 9 | + |
| 10 | +Register every employee as a Cortex entity linked to their team, push weekly Claude spend as a custom metric, and roll costs up the org hierarchy — from individual → sub-team → top-level engineering. |
| 11 | + |
| 12 | +## How It Works |
| 13 | + |
| 14 | +``` |
| 15 | + ┌─────────────────────┐ every Monday 06:00 UTC |
| 16 | + │ GitHub Actions │◄──────────────────────────────────┐ |
| 17 | + │ sync-claude-spend │ │ |
| 18 | + └────────┬────────────┘ (cron schedule) |
| 19 | + │ |
| 20 | + │ GET /v1/organizations/analytics/costs |
| 21 | + ▼ |
| 22 | + ┌─────────────────────┐ |
| 23 | + │ Anthropic Claude │ per-user spend for the week |
| 24 | + │ Analytics API │ |
| 25 | + └────────┬────────────┘ |
| 26 | + │ |
| 27 | + │ map email → employee-first-last |
| 28 | + │ sum members → team rollups |
| 29 | + ▼ |
| 30 | + ┌─────────────────────┐ |
| 31 | + │ Cortex API │ POST ai-spend custom metric |
| 32 | + │ Custom Metrics │ per employee + per team |
| 33 | + └────────┬────────────┘ |
| 34 | + │ |
| 35 | + ▼ |
| 36 | + ┌──────────────────────────────────────────────┐ |
| 37 | + │ Cortex Catalog │ |
| 38 | + │ │ |
| 39 | + │ team-engineering $1,212/wk Silver │ |
| 40 | + │ ├── team-platform $473/wk Gold │ |
| 41 | + │ │ ├── employee-alice $291/wk │ |
| 42 | + │ │ └── employee-bob $182/wk │ |
| 43 | + │ ├── team-frontend $380/wk Silver │ |
| 44 | + │ │ ├── employee-carol $245/wk │ |
| 45 | + │ │ └── employee-david $136/wk │ |
| 46 | + │ └── team-data $359/wk Bronze │ |
| 47 | + │ └── employee-emma $359/wk │ |
| 48 | + │ │ |
| 49 | + │ Scorecard: ai-spend-scorecard │ |
| 50 | + │ Plugin: team-ai-spend (per-team chart) │ |
| 51 | + └──────────────────────────────────────────────┘ |
| 52 | +``` |
| 53 | + |
| 54 | +## What's Included |
| 55 | + |
| 56 | +| Resource | Tag / Key | |
| 57 | +|---|---| |
| 58 | +| Entity type | `employee` | |
| 59 | +| Relationship type | `team-member` (team → team\|employee) | |
| 60 | +| Teams | `team-engineering`, `team-platform`, `team-frontend`, `team-data` | |
| 61 | +| Employees | `employee-alice-chen`, `employee-bob-martinez`, `employee-carol-kim`, `employee-david-osei`, `employee-emma-johnson` | |
| 62 | +| Custom metric sample data | `ai-spend` (8 weeks, fictional, per-employee and team rollups) | |
| 63 | +| Plugin | `team-ai-spend` (team-scoped spend visualization) | |
| 64 | +| Scorecard | `ai-spend-scorecard` (bronze/silver/gold budget compliance) | |
| 65 | +| Sync script | `scripts/sync-claude-spend.py` | |
| 66 | +| GH Actions workflow | `.github/workflows/sync-claude-spend.yaml` | |
| 67 | + |
| 68 | +## Prerequisites |
| 69 | + |
| 70 | +Before installing, create the `ai-spend` custom metric definition in your Cortex instance: |
| 71 | +**Eng Intel → Custom Metrics → New Metric**, key: `ai-spend`. |
| 72 | + |
| 73 | +## Installation |
| 74 | + |
| 75 | +``` |
| 76 | +cortex solutions install -s ai-spend |
| 77 | +``` |
| 78 | + |
| 79 | +## After Installing |
| 80 | + |
| 81 | +**Create the team-member catalog** |
| 82 | + |
| 83 | +Enable the relationship type catalog so you can browse team membership from the Cortex UI: |
| 84 | + |
| 85 | +1. Go to **Settings → Entity Relationship Types → team-member** |
| 86 | +2. Click **Edit** and enable **Create relationship type catalog** |
| 87 | +3. Save |
| 88 | + |
| 89 | +**View the AI Spend Budget Compliance scorecard** |
| 90 | + |
| 91 | +An `ai-spend-scorecard` is installed automatically and tracks whether each team's weekly spend stays within budget: |
| 92 | + |
| 93 | +- **Bronze** — team has `ai-spend` metric data in the last 8 days and a budget set |
| 94 | +- **Silver** — spend is within 25% of budget |
| 95 | +- **Gold** — spend is at or under budget |
| 96 | + |
| 97 | +The sample data is pre-loaded with budgets that produce an interesting distribution: team-platform achieves Gold, team-frontend and team-engineering achieve Silver, and team-data achieves Bronze. |
| 98 | + |
| 99 | +To set a budget for a real team, add `ai-budget-weekly` as custom data on the team entity: |
| 100 | + |
| 101 | +```bash |
| 102 | +cortex custom-data add -t <team-tag> -k ai-budget-weekly -v <weekly-budget-dollars> |
| 103 | +``` |
| 104 | + |
| 105 | +**View the Team AI Spend plugin** |
| 106 | + |
| 107 | +A `team-ai-spend` plugin is installed automatically and appears on every team entity page. It shows the team's total weekly AI spend and a per-member breakdown bar chart, pulling live data from the `ai-spend` custom metric. |
| 108 | + |
| 109 | +**Create a Tabular View for AI spend** |
| 110 | + |
| 111 | +Build a Data Explorer table to compare spend across employees and teams: |
| 112 | + |
| 113 | +1. Go to **Eng Intelligence → Data Explorer** |
| 114 | +2. Select the **Table** view |
| 115 | +3. Click **Add column**, find `ai-spend` under the **Custom** category, and click **View metric** |
| 116 | +4. Set **Group by → Team** and enable **Show hierarchy** to roll up spend to team level |
| 117 | +5. Click **Save As** to name and save the view for future use |
| 118 | + |
| 119 | +> Note: Tabular View creation is not yet available via API. It must be configured manually. |
| 120 | +
|
| 121 | +**Set up live Claude spend sync** |
| 122 | + |
| 123 | +The sample entities include fictional spend data. To push real data from your Anthropic Claude Enterprise account weekly: |
| 124 | + |
| 125 | +1. **Get an Analytics API key:** |
| 126 | + - Sign in to claude.ai as the **primary owner** of your organization |
| 127 | + - Go to **Organization settings → API** |
| 128 | + - Enable public API access and create an Analytics API key |
| 129 | + - (Only the primary owner can create this key — admin role is not sufficient) |
| 130 | + |
| 131 | +2. **Add secrets to your GitHub repo:** |
| 132 | + - `ANTHROPIC_ANALYTICS_KEY` — the Analytics API key from step 1 |
| 133 | + - `CORTEX_API_KEY` — your Cortex API key |
| 134 | + |
| 135 | +3. **Copy the workflow** to your repo's `.github/workflows/` directory: |
| 136 | + ```bash |
| 137 | + cp .github/workflows/sync-claude-spend.yaml <your-repo>/.github/workflows/ |
| 138 | + ``` |
| 139 | + |
| 140 | +4. **Copy the script** to your repo's `scripts/` directory: |
| 141 | + ```bash |
| 142 | + cp scripts/sync-claude-spend.py <your-repo>/scripts/ |
| 143 | + ``` |
| 144 | + |
| 145 | +The workflow runs every Monday at 06:00 UTC and can be triggered manually from the GitHub Actions tab. |
| 146 | + |
| 147 | +**Customize the email domain** |
| 148 | + |
| 149 | +The sync script maps `first.last@cortex.io` → `employee-first-last`. Set `EMAIL_DOMAIN` in the workflow env to match your company's domain: |
| 150 | + |
| 151 | +```yaml |
| 152 | +env: |
| 153 | + EMAIL_DOMAIN: yourcompany.com |
| 154 | +``` |
| 155 | +
|
| 156 | +**Add your real employees** |
| 157 | +
|
| 158 | +The sample entities are fictional. Add your real employees as catalog entities with `x-cortex-type: employee` and tag them `employee-<first>-<last>` to match the email mapping. |
| 159 | + |
| 160 | +**Notes** |
| 161 | + |
| 162 | +- Users who authenticate Claude Code with a personal API key (not Enterprise OAuth) show $0 spend in the Analytics API and are skipped automatically. |
| 163 | +- Cost data may take up to 24 hours to appear; dates at least 30 days old are considered final for billing purposes. |
| 164 | +- The `ai-spend` custom metric definition must currently be created manually before installing. A future release will support auto-creation of custom metric definitions as part of `cortex solutions install`. |
| 165 | +- The scorecard's Bronze rule uses a `P1Y` lookback to accommodate sample data. Once your weekly sync is running consistently, consider tightening it to `P8D` to ensure the rule only passes when data is fresh. The Silver and Gold rules use `P8D` and can similarly be adjusted to match your sync frequency. |
0 commit comments