Skip to content

Commit 6df8a81

Browse files
DanielB945claude
andcommitted
feat: Add enterprise token consumption schema documentation
## Changes ### New Documentation - **shared/enterprise-token-pools.md**: Comprehensive guide to shared vs non-shared token pools - Shared token pool logic (is_shared_token_pool flag) - Non-shared pool logic (McCann_NY, Cylndr, code users) - Enterprise organization classification - Complete credit limit monitoring queries - Code redemption tracking - Future user cap feature documentation ### Schema Updates (shared/bq-schema.md) - **backend_griffin_market_purchased_consumables**: Token purchases and initial balances - Key fields: consumable_id, ltid, organization_id, is_shared_token_pool, quantity - Shared vs non-shared pool logic - Example queries for credit limit monitoring - **backend_griffin_market_consumed_consumables**: Token consumption transactions - Key field: consumed_by_ltid (critical for shared pool attribution) - Consumption tracking by user - **backend_griffin_market_unified_payment_source**: Payment sources and code redemptions - complimentary_code field for tracking code users - Code redemption query examples - **backend_griffin_shared_subscription_group**: Shared subscription groups - has_shared_token_pool flag - Seat configurations and consumable offerings ### Reference Updates (CLAUDE.md) - Added enterprise-token-pools.md to shared knowledge table - Read before any enterprise token/billing analysis ## Context These tables are critical for: - Credit limit monitoring (Report 4 in daily reports) - Enterprise account health tracking - Token consumption analysis - Code redemption tracking ## Key Insights **Shared Token Pools (most enterprise orgs):** - Records at ORG level: ltid = group_id - Use consumed_by_ltid to track individual user consumption - Examples: Indegene, HearWell_BeWell, Miroma, Deriv, McCann_Paris **Non-Shared Pools:** - Records at USER level: ltid = individual user - Examples: McCann_NY, Cylndr Studios (SSO), code users **McCann Special Case:** - McCann_NY: Separate entity, non-shared pool - McCann_Paris: Catch-all for other McCann offices, shared pool Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent c4a2ebf commit 6df8a81

5 files changed

Lines changed: 955 additions & 0 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
-- Credit Limit Alerts by Organization (80%+)
2+
-- Uses backend_griffin_market tables with shared/non-shared pool logic
3+
-- See shared/enterprise-token-pools.md for complete documentation
4+
5+
-- Shared pool orgs (most enterprise)
6+
with shared_pool as (
7+
select
8+
p.ltid as group_id,
9+
p.organization_id as org,
10+
'shared' as pool_type,
11+
sum(p.quantity) as total_purchased,
12+
sum(case when c.quantity is not null then c.quantity else 0 end) as total_consumed,
13+
count(distinct c.consumed_by_ltid) as users_at_risk,
14+
timestamp_millis(max(p.created_at_ms)) as last_purchase_ts
15+
from `ltx-dwh-prod-processed`.base.backend_griffin_market_purchased_consumables p
16+
left join `ltx-dwh-prod-processed`.base.backend_griffin_market_consumed_consumables c
17+
on p.consumable_id = c.consumable_id
18+
where p.is_shared_token_pool = true
19+
and p.revoked_at_ms is null
20+
group by 1, 2
21+
),
22+
23+
-- Non-shared pool users (McCann_NY, Cylndr SSO, code users with orgs)
24+
non_shared_pool as (
25+
select
26+
p.ltid as group_id,
27+
p.organization_id as org,
28+
'non_shared' as pool_type,
29+
sum(p.quantity) as total_purchased,
30+
sum(case when c.quantity is not null then c.quantity else 0 end) as total_consumed,
31+
1 as users_at_risk,
32+
timestamp_millis(max(p.created_at_ms)) as last_purchase_ts
33+
from `ltx-dwh-prod-processed`.base.backend_griffin_market_purchased_consumables p
34+
left join `ltx-dwh-prod-processed`.base.backend_griffin_market_consumed_consumables c
35+
on p.consumable_id = c.consumable_id
36+
where p.is_shared_token_pool = false
37+
and p.revoked_at_ms is null
38+
and p.organization_id is not null -- Only enterprise users, exclude self-serve code users
39+
group by 1, 2
40+
),
41+
42+
-- Combine both pool types
43+
combined_pools as (
44+
select * from shared_pool
45+
union all
46+
select * from non_shared_pool
47+
),
48+
49+
-- Add enterprise classification and calculate metrics
50+
enterprise_orgs as (
51+
select
52+
*,
53+
case
54+
-- McCann split
55+
when org = 'McCann_NY' then 'McCann_NY'
56+
when org like '%McCann%' then 'McCann_Paris'
57+
58+
-- Contracted enterprise
59+
when org in (
60+
'Indegene',
61+
'HearWell_BeWell',
62+
'Novig',
63+
'Cylndr Studios',
64+
'Miroma',
65+
'Deriv',
66+
'McCann_Paris'
67+
) then 'Enterprise'
68+
69+
-- Enterprise pilot (all others)
70+
else 'Enterprise Pilot'
71+
end as ent_type,
72+
73+
-- Calculate metrics
74+
total_purchased - total_consumed as current_balance,
75+
safe_divide(total_consumed, total_purchased) * 100 as pct_used
76+
from combined_pools
77+
where org not in ('Lightricks', 'Popular Pays', 'None')
78+
),
79+
80+
-- Filter for 80%+ usage
81+
alerts as (
82+
select
83+
ent_type,
84+
org,
85+
pool_type,
86+
users_at_risk,
87+
total_purchased,
88+
current_balance,
89+
total_consumed,
90+
round(pct_used, 1) as avg_pct_used,
91+
last_purchase_ts
92+
from enterprise_orgs
93+
where pct_used >= 80 -- At or above 80% usage
94+
),
95+
96+
-- Aggregate summary
97+
org_summary as (
98+
select
99+
ent_type,
100+
org as enterprise_org,
101+
max(pool_type) as pool_type,
102+
sum(users_at_risk) as users_at_risk,
103+
sum(total_purchased) as org_total_purchased,
104+
sum(current_balance) as org_current_balance,
105+
sum(total_consumed) as org_consumed,
106+
round(safe_divide(sum(total_consumed), sum(total_purchased)) * 100, 1) as org_avg_pct_used
107+
from alerts
108+
group by ent_type, enterprise_org
109+
)
110+
111+
select
112+
date_sub(current_date(), interval 1 day) as report_date,
113+
114+
-- Summary counts
115+
countif(ent_type = 'Enterprise') as total_enterprise_orgs_near_limit,
116+
countif(ent_type = 'Enterprise Pilot') as total_enterprise_pilot_orgs_near_limit,
117+
118+
-- Enterprise orgs list with details
119+
array_agg(
120+
if(ent_type = 'Enterprise',
121+
struct(
122+
enterprise_org as org,
123+
pool_type,
124+
users_at_risk,
125+
org_total_purchased as total_purchased,
126+
org_current_balance as current_balance,
127+
org_consumed as consumed,
128+
org_avg_pct_used as avg_pct_used
129+
),
130+
null)
131+
ignore nulls
132+
order by org_avg_pct_used desc
133+
) as enterprise_orgs_at_risk,
134+
135+
-- Enterprise Pilot orgs list with details
136+
array_agg(
137+
if(ent_type = 'Enterprise Pilot',
138+
struct(
139+
enterprise_org as org,
140+
pool_type,
141+
users_at_risk,
142+
org_total_purchased as total_purchased,
143+
org_current_balance as current_balance,
144+
org_consumed as consumed,
145+
org_avg_pct_used as avg_pct_used
146+
),
147+
null)
148+
ignore nulls
149+
order by org_avg_pct_used desc
150+
) as enterprise_pilot_orgs_at_risk
151+
152+
from org_summary
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
-- Credit Limit Alerts by Organization (80%+) - Active in Last Week
2+
-- Uses backend_griffin_market tables with shared/non-shared pool logic
3+
-- See shared/enterprise-token-pools.md for complete documentation
4+
5+
-- Shared pool orgs (most enterprise)
6+
with shared_pool as (
7+
select
8+
p.ltid as group_id,
9+
p.organization_id as org,
10+
'shared' as pool_type,
11+
sum(p.quantity) as total_purchased,
12+
sum(case when c.quantity is not null then c.quantity else 0 end) as total_consumed,
13+
count(distinct c.consumed_by_ltid) as users_at_risk,
14+
timestamp_millis(max(p.created_at_ms)) as last_purchase_ts
15+
from `ltx-dwh-prod-processed`.base.backend_griffin_market_purchased_consumables p
16+
left join `ltx-dwh-prod-processed`.base.backend_griffin_market_consumed_consumables c
17+
on p.consumable_id = c.consumable_id
18+
where p.is_shared_token_pool = true
19+
and p.revoked_at_ms is null
20+
group by 1, 2
21+
),
22+
23+
-- Non-shared pool users (McCann_NY, Cylndr SSO, code users with orgs)
24+
non_shared_pool as (
25+
select
26+
p.ltid as group_id,
27+
p.organization_id as org,
28+
'non_shared' as pool_type,
29+
sum(p.quantity) as total_purchased,
30+
sum(case when c.quantity is not null then c.quantity else 0 end) as total_consumed,
31+
1 as users_at_risk,
32+
timestamp_millis(max(p.created_at_ms)) as last_purchase_ts
33+
from `ltx-dwh-prod-processed`.base.backend_griffin_market_purchased_consumables p
34+
left join `ltx-dwh-prod-processed`.base.backend_griffin_market_consumed_consumables c
35+
on p.consumable_id = c.consumable_id
36+
where p.is_shared_token_pool = false
37+
and p.revoked_at_ms is null
38+
and p.organization_id is not null -- Only enterprise users, exclude self-serve code users
39+
group by 1, 2
40+
),
41+
42+
-- Combine both pool types
43+
combined_pools as (
44+
select * from shared_pool
45+
union all
46+
select * from non_shared_pool
47+
),
48+
49+
-- Add enterprise classification and calculate metrics
50+
enterprise_orgs as (
51+
select
52+
*,
53+
case
54+
-- McCann split
55+
when org = 'McCann_NY' then 'McCann_NY'
56+
when org like '%McCann%' then 'McCann_Paris'
57+
58+
-- Contracted enterprise
59+
when org in (
60+
'Indegene',
61+
'HearWell_BeWell',
62+
'Novig',
63+
'Cylndr Studios',
64+
'Miroma',
65+
'Deriv',
66+
'McCann_Paris'
67+
) then 'Enterprise'
68+
69+
-- Enterprise pilot (all others)
70+
else 'Enterprise Pilot'
71+
end as ent_type,
72+
73+
-- Calculate metrics
74+
total_purchased - total_consumed as current_balance,
75+
safe_divide(total_consumed, total_purchased) * 100 as pct_used
76+
from combined_pools
77+
where org not in ('Lightricks', 'Popular Pays', 'None')
78+
),
79+
80+
-- Filter for 80%+ usage
81+
alerts as (
82+
select
83+
ent_type,
84+
org,
85+
pool_type,
86+
users_at_risk,
87+
total_purchased,
88+
current_balance,
89+
total_consumed,
90+
round(pct_used, 1) as avg_pct_used,
91+
last_purchase_ts
92+
from enterprise_orgs
93+
where pct_used >= 80 -- At or above 80% usage
94+
),
95+
96+
-- Build enterprise user list for activity filter
97+
ent_users_list as (
98+
select distinct
99+
lt_id,
100+
case
101+
when coalesce(enterprise_name_at_purchase, current_enterprise_name, organization_name) = 'McCann_NY' then 'McCann_NY'
102+
when coalesce(enterprise_name_at_purchase, current_enterprise_name, organization_name) like '%McCann%' then 'McCann_Paris'
103+
else coalesce(enterprise_name_at_purchase, current_enterprise_name, organization_name)
104+
end as org
105+
from `ltx-dwh-prod-processed`.web.ltxstudio_users
106+
where coalesce(enterprise_name_at_purchase, current_enterprise_name, organization_name) not in ('Lightricks', 'Popular Pays', 'None')
107+
),
108+
109+
-- Orgs with generation activity in last 7 days
110+
active_orgs_last_week as (
111+
select distinct
112+
e.org
113+
from `ltx-dwh-prod-processed`.web.ltxstudio_user_all_actions a
114+
inner join ent_users_list e on a.lt_id = e.lt_id
115+
where date(a.action_ts) >= date_sub(current_date(), interval 7 day)
116+
and a.action_category = 'generations'
117+
),
118+
119+
-- Aggregate summary (only active orgs)
120+
org_summary as (
121+
select
122+
a.ent_type,
123+
a.org as enterprise_org,
124+
max(a.pool_type) as pool_type,
125+
sum(a.users_at_risk) as users_at_risk,
126+
sum(a.total_purchased) as org_total_purchased,
127+
sum(a.current_balance) as org_current_balance,
128+
sum(a.total_consumed) as org_consumed,
129+
round(safe_divide(sum(a.total_consumed), sum(a.total_purchased)) * 100, 1) as org_avg_pct_used
130+
from alerts a
131+
inner join active_orgs_last_week aw
132+
on a.org = aw.org
133+
group by a.ent_type, a.org
134+
)
135+
136+
select
137+
date_sub(current_date(), interval 1 day) as report_date,
138+
139+
-- Summary counts
140+
countif(ent_type = 'Enterprise') as total_enterprise_orgs_near_limit,
141+
countif(ent_type = 'Enterprise Pilot') as total_enterprise_pilot_orgs_near_limit,
142+
143+
-- Enterprise orgs list with details
144+
array_agg(
145+
if(ent_type = 'Enterprise',
146+
struct(
147+
enterprise_org as org,
148+
pool_type,
149+
users_at_risk,
150+
org_total_purchased as total_purchased,
151+
org_current_balance as current_balance,
152+
org_consumed as consumed,
153+
org_avg_pct_used as avg_pct_used
154+
),
155+
null)
156+
ignore nulls
157+
order by org_avg_pct_used desc
158+
) as enterprise_orgs_at_risk,
159+
160+
-- Enterprise Pilot orgs list with details
161+
array_agg(
162+
if(ent_type = 'Enterprise Pilot',
163+
struct(
164+
enterprise_org as org,
165+
pool_type,
166+
users_at_risk,
167+
org_total_purchased as total_purchased,
168+
org_current_balance as current_balance,
169+
org_consumed as consumed,
170+
org_avg_pct_used as avg_pct_used
171+
),
172+
null)
173+
ignore nulls
174+
order by org_avg_pct_used desc
175+
) as enterprise_pilot_orgs_at_risk
176+
177+
from org_summary

CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Every agent reads from these files. They are the single source of truth — do n
3838
| `shared/bq-schema.md` | BQ tables, columns, joins, segmentation queries | Writing ANY SQL |
3939
| `shared/event-registry.yaml` | Known events per feature, types, status | Referencing ANY event |
4040
| `shared/metric-standards.md` | How every metric is calculated (with SQL) | Defining ANY metric |
41+
| `shared/enterprise-token-pools.md` | Token pool logic, shared vs non-shared, credit limits | Enterprise token/billing analysis |
4142
| `shared/gpu-cost-query-templates.md` | 11 GPU cost queries (DoD, WoW, anomaly detection, breakdowns) | Analyzing GPU/infrastructure costs |
4243
| `shared/gpu-cost-analysis-patterns.md` | Cost analysis workflows, benchmarks, investigation playbooks | Interpreting GPU cost data |
4344

0 commit comments

Comments
 (0)