Skip to content

[AISAGE-503] feat(gateway): enqueue finalized usage in background shadow mode - #7296

Merged
johnnyeric merged 1 commit into
johnnyeric/usage-shadow-publisherfrom
johnnyeric/usage-shadow-after
Oct 8, 2026
Merged

johnnyeric merged 1 commit into
johnnyeric/usage-shadow-publisherfrom
johnnyeric/usage-shadow-after

Conversation

@johnnyeric

Copy link
Copy Markdown
Contributor

Summary

With USAGE_SHADOW_PUBLISH_ENABLED=true, the gateway sends its finalized usage event to the queue-backed ingestion endpoint in both Frankfurt and SFO. It reuses the existing event and ID while current billing remains authoritative and independent of enqueue acceptance. The switch defaults off.

Enqueue runs in its own tracked after promise, including when earlier request-cleanup callbacks have finished. Publishing starts only after lifetime registration succeeds; missing request scope or lifetime support performs no network work. Acceptance and failure logs contain safe identifiers and categories. Staging enables the ingestion Worker's workers.dev URL for testing after deployment; production public URLs and preview URLs remain disabled.

Verification

  • Exercised synthetic gateway usage through the actual publisher, HTTP, configured Worker entry and local queue in both regions. Verified matching usage IDs, disabled zero network calls, real unauthorized response, and bounded HTTP timeout with billing unaffected.
  • Exercised a real HTTP stall after earlier request cleanup had drained: billing completed in milliseconds while the tracked background lifetime waited approximately two seconds for timeout. Fixture ports were released.

Visual Changes

N/A

Reviewer Notes

Depends on #7295 (usage enqueue helper); this PR contains only gateway integration, its configuration/tests, and staging endpoint configuration/docs.

Validation: 214 focused tests and 43 selected processUsage tests pass; 22 DB-backed cases excluded. Both typechecks, scoped lint, formatting and diff checks pass. The real Next lifecycle regression fails on the previous callback implementation in both regions and passes with independent promise registration. Staging and production deployment dry runs passed.

Database writes, authoritative writer transport and provider behavior are mocked/synthetic, and request lifecycle hooks are synthetic. Worker/HTTP/local queue components run in the actual local runtime with a temporary receipt consumer. No hosted gateway verification or real billing/inference was performed.

The existing Worker schema strips redundant runtime metadata.requested_model; core.requested_model is retained. The gateway sends the unchanged event, and queue receipts match its validated contract fields. HTTP 202 means enqueued, not processed or billed.

@johnnyeric johnnyeric changed the title feat(gateway): enqueue finalized usage in background shadow mode [AISAGE-503] feat(gateway): enqueue finalized usage in background shadow mode Oct 8, 2026
@johnnyeric
johnnyeric added this pull request to stack #7298 October 8, 2026 13:48
@johnnyeric
johnnyeric marked this pull request as ready for review October 8, 2026 13:50
@kilo-code-bot

kilo-code-bot Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (6 files)
  • ENVIRONMENT.md
  • packages/web-shared/src/lib/ai-gateway/processUsage.ts
  • packages/web-shared/src/lib/ai-gateway/usage-shadow-integration.test.ts
  • packages/web-shared/src/lib/config.server.ts
  • services/usage-ingest/README.md
  • services/usage-ingest/wrangler.jsonc

Reviewed by deepseek-v4.1-flash · Input: 80.5K · Output: 21.4K · Cached: 1.1M

Review guidance: REVIEW.md from base branch johnnyeric/usage-shadow-publisher

@joshburt
joshburt self-requested a review October 8, 2026 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants