caelum0x 3764a2fd44 feat(rago): add idempotent Relay subscription provisioning script
Relay's cost-feed -> Rago publisher (RagoCostEventPublisher) and durable
Postgres outbox were already built and tested, but RELAY_COST_FEED_URL was
never set in production, and no Rago subscription existed for any Relay API
key's default subscription id (RELAY_RAGO_SUBSCRIPTION_PREFIX + key id) --
flipping the feed on without this would have had every event fail delivery
and dead-letter.

apps/rago/seeds/provision-relay-subscriptions.sh creates the Rago customer +
one subscription per Relay API key (idempotent: GET before POST), so a
consumer org is ready before RELAY_COST_FEED_FORMAT=rago is enabled.

Ops (this session, railyard-box production): ran the script against the
existing autogtm Rago org for all 5 live Relay API keys, set
RELAY_COST_FEED_URL/FORMAT/BEARER/SUBSCRIPTION_PREFIX in relay.env, restarted
relay. Result: 514 backlogged cost events drained with 0 dead-letters, Rago's
relay_llm_usage sum matches Relay's outbox exactly (12,075,595 micros / $12.08
real historical LLM spend). Verified a fresh live inference call also feeds
Rago within seconds. Installed the existing reconcile-relay-rago nightly
timer (was built, never installed) and set RECON_EXPECT_FEED=1.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-17 02:28:21 +03:00
..

Rago consumer seeds

Catalog fixtures — billable metrics + a plan — for each product that meters usage through the shared rago billing plane. One seed file == one consumer == one rago organization (tenant).

Seed Consumer Metric code(s) Plan Charge model
autogtm.json Rally AutoGTM (vcglass cold email + Relay LLM spend + Rally plan/seat mirror) gtm_email_sent, gtm_draft, gtm_topup, relay_llm_usage, rally_plan_event, rally_seats gtm_usage ($0 metering mirror, vcglass default) + autogtm_growth ($49/mo) + rally_plan_mirror ($0 plan/seat mirror) standard at $0 (mirror); package (emails) + standard (drafts) on growth
menivor.json Menivor (video/reel + LLM) menivor_media_spend menivor_studio ($29/mo) dynamic (cost pass-through)
menivor-reels.json Menivor (reel generation + Relay LLM calls) reel_generated, reel_render_seconds, reel_ai_credits, menivor_llm_call menivor_reels ($0 base) standard (per-unit; matches RAGO_REEL_PRICING defaults; LLM calls at $0)
menivor-reels-shadow.json Menivor, shadow mirror (Menivor bills through its own Creem credit packs) same as menivor-reels.json menivor_reels ($0 base) standard at $0 on every charge: usage visibility only, can never produce a non-zero invoice
opentypesafe.json opentypesafe / OpenJev (System One / Jev + Relay LLM spend) systemone_call, relay_llm_usage opentypesafe_pro ($0 base) graduated (per-call tiers); relay_llm_usage passed through at cost
scribase.json Scribase (backend cloud usage mirror, env-gated in control-api) scribase_database_gb, scribase_storage_gb, scribase_backup_gb, scribase_egress_gb, scribase_compute_hours, scribase_function_invocations scribase_usage ($0 metering mirror; Scribase's own flat per-environment billing stays the charge of record) max_agg gauges (hourly samples) + sum_agg increments, all standard at $0

Each file is an envelope:

{
  "consumer": "autogtm",
  "billable_metrics": [ { "code": "...", "aggregation_type": "count_agg", ... } ],
  "plans":            [ { "code": "...", "charges": [ { "billable_metric_code": "...", "charge_model": "...", "properties": {...} } ] } ]
}

The field names match rago's REST request bodies exactly (Lago root-wrapped shape): a metric maps to POST /billable_metrics { "billable_metric": {...} } and a plan (with nested charges) maps to POST /plans { "plan": {...} }. See crates/rago-api/src/handlers.rs (create_billable_metric, create_plan) and the enums in crates/rago-core/src/enums.rs (AggregationType, ChargeModel, PlanInterval).

relay_llm_usage is the metric Railyard Relay's cost feed emits (RELAY_COST_FEED_FORMAT=rago): a sum_agg over properties.charged_micros (retail spend in micro-USD). Each consumer that routes LLM calls through Relay gets it in its own org. Point the consumer's gateway key at that org with RELAY_RAGO_ROUTES_JSON (see apps/relay/README.md), and create the Rago subscription whose external id matches the route's subscription_id.

0. One-time: create the consumer org + API key

Rago has no public signup; the Bearer token in api_keys.value selects the org. Create one org + key per consumer directly in Postgres (run against the rago database — see ../../DEPLOY-SHARED-PLANE.md):

-- AutoGTM tenant (repeat per consumer with a fresh org id + token).
WITH org AS (
  INSERT INTO organizations (id, name, slug, created_at, updated_at)
  VALUES (gen_random_uuid(), 'AutoGTM', 'autogtm', now(), now())
  RETURNING id
)
INSERT INTO api_keys (id, organization_id, value, name, created_at, updated_at)
SELECT gen_random_uuid(), org.id, 'rago_live_autogtm_REPLACE_ME', 'autogtm-seed', now(), now()
FROM org;

Use a long random value (openssl rand -hex 24) in real deploys; keep it in the consumer's secret store.

1. Import — script

RAGO_API_KEY='rago_live_autogtm_REPLACE_ME' ./import.sh autogtm.json
RAGO_API_KEY='rago_live_menivor_...'         ./import.sh menivor.json
RAGO_API_KEY='rago_live_opentypesafe_...'    ./import.sh opentypesafe.json
RAGO_API_KEY='rago_live_scribase_...'        ./import.sh scribase.json

# Remote plane:
RAGO_API_URL=https://rago.example.com/api/v1 RAGO_API_KEY=... ./import.sh menivor.json

The script is re-run safe: a 422 (already exists / validation) is reported as skip, not a failure. Requires curl + jq.

2. Import — raw curl (no script)

API=http://localhost:3000/api/v1
KEY='rago_live_autogtm_REPLACE_ME'

# a metric
curl -fsS -X POST "$API/billable_metrics" \
  -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' \
  -d '{"billable_metric":{"name":"GTM Emails Sent","code":"gtm_email_sent","aggregation_type":"count_agg"}}'

# a plan with nested charges
curl -fsS -X POST "$API/plans" \
  -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' \
  -d '{"plan":{"name":"AutoGTM Growth","code":"autogtm_growth","interval":"monthly","amount_cents":4900,"amount_currency":"USD","pay_in_advance":true,"charges":[{"billable_metric_code":"gtm_email_sent","charge_model":"package","properties":{"amount":"8.0","package_size":1000,"free_units":2000}},{"billable_metric_code":"gtm_draft","charge_model":"standard","properties":{"amount":"0.02"}}]}}'

3. Verify

curl -fsS -H "Authorization: Bearer $KEY" "$API/plans"            | jq '.plans[].code'
curl -fsS -H "Authorization: Bearer $KEY" "$API/billable_metrics" | jq '.billable_metrics[].code'

4. Emit usage (downstream, per consumer)

After a customer + subscription exist for the plan, meter usage with POST /events:

curl -fsS -X POST "$API/events" -H "Authorization: Bearer $KEY" -H 'Content-Type: application/json' \
  -d '{"event":{"transaction_id":"'"$(uuidgen)"'","external_subscription_id":"sub_123","code":"gtm_email_sent"}}'

For Menivor's dynamic charge, carry the real provider cost on each event:

-d '{"event":{"transaction_id":"...","external_subscription_id":"sub_123","code":"menivor_media_spend","properties":{"units":12},"precise_total_amount_cents":"340"}}'