feat: webhook alert when virtual key token threshold is crossed (Story 3.4)#136
Open
farovictor wants to merge 1 commit intomainfrom
Open
feat: webhook alert when virtual key token threshold is crossed (Story 3.4)#136farovictor wants to merge 1 commit intomainfrom
farovictor wants to merge 1 commit intomainfrom
Conversation
…y 3.4) - Two new fields on VirtualKey: alert_threshold (int) and alert_webhook (URL) - After each proxied request, proxy checks if TotalTokens just crossed the threshold (prevTotal < threshold <= newTotal) and fires a non-blocking POST - Alert fires once — subsequent requests already above threshold are ignored - Payload: event, key_id, service, threshold, total_tokens, timestamp - Migration 012_add_virtual_key_alert.sql - 4 tests: fires on crossing, fires via proxy with token tracking, not fired below threshold, not fired when already above threshold Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
VirtualKey:alert_threshold(int, tokens) andalert_webhook(URL)POSTto the webhook URL in a background goroutineprevTotal < threshold <= newTotal. Subsequent requests already above threshold are silent012_add_virtual_key_alert.sqlWebhook payload
{ "event": "token_threshold_crossed", "key_id": "vk-abc", "service": "openai", "threshold": 100, "total_tokens": 101, "timestamp": "2026-04-06T19:00:00Z" }Test plan
TestWebhookAlert_FiredByProxyWhenTrackTokensOn— crossing fires exactly 1 webhook call with correct payloadTestWebhookAlert_NotFiredWhenBelowThreshold— no call when total stays below thresholdTestWebhookAlert_NotFiredWhenAlreadyOver— no call when prevTotal already >= thresholdTestWebhookAlert_FiredWhenThresholdCrossed— proxy request succeeds regardless of webhook outcome🤖 Generated with Claude Code