Repository navigation
Count Daily AI Gateway Usage by Agent UID, User, and Copilot Route - #668
Merged
Merged
Conversation
GatewayDailyUsage groups by the Agent's name and the owner's alias. Both can repeat: a new Agent can take the name of a deleted one, and users from two domains can have the same alias. It also counts the calls that an owner's GitHub Copilot route answered together with the shared deployments of the same model name. Kusto cannot change how a materialized view groups its rows, so the cluster template now creates GatewayDailyUsageV2 next to it. The new view adds the Agent UID, the AI Gateway user ID of the key, the provider and provider model, CopilotRoute, and the cache creation tokens. It is backfilled from the rows that the Gateway table still keeps. The script leaves the earlier view and its rows alone; the docs say how to drop it. GatewayRequests() gains CopilotRoute. The upgrade page says that make deploy does not change the ADX scripts.
…n existing databases A redeployment of the cluster template over the logs database of a test cluster failed at the view: Kusto refuses to create a materialized view with backfill while rows of its table are outside the hot cache, and a Gateway table older than the seven days of the hot cache has such rows. The script would fail at every deployment of an existing installation. The view now counts the calls from its creation. infrastructure.md describes how a database admin creates it again once with backfill, with the Gateway table in the hot cache for the time; on the test cluster this gave the same totals as GatewayDailyUsage.
Pengfei Ni (feiskyer)
requested changes
Nov 25, 2026
Pengfei Ni (feiskyer)
pushed a commit
that referenced
this pull request
Nov 27, 2026
…Group (#669) * Rebuild the daily usage view only while the Gateway table has all its days The backfill procedure that #668 documents drops GatewayDailyUsageV2 and creates it again from the Gateway table. The view keeps 400 days by default, the table 90, so dropping a view that already has older days erases them, and a warm cache does not bring back deleted rows. The procedure now applies only while the table still has every day of the view, with a query that compares the oldest row of the table with the first day of the view. * Keep the daily usage view until a backfilled copy accounts for every day The comparison of the oldest Gateway row with the first day of the view does not prove that the table still has every counted request: the timestamp covers all Gateway logs, and the table deletes rows by the time of their ingestion. The documented backfill now never drops GatewayDailyUsageV2. It builds GatewayDailyUsageV2Backfill, compares the calls and the spend of each day and cluster, and renames the views only when the copy has at least the totals of every day. The previous view stays until an admin drops it. * Compare every group of the daily usage view, and stage under an unused name Review of e15c260: - Merged extents keep older rows after newer, unmerged rows expire, so day and cluster totals can match while one Agent's calls are lost. The comparison now covers each group of GatewayDailyUsageV2 (its 12 keys) and all its counters, as the second view minus the current one. - With .create ifnotexists, an existing GatewayDailyUsageV2Backfill was left as it was, and the next steps changed and dropped it. The second view now gets a dated name that the database does not have, is created with .create async (without ifnotexists, so the creation fails when the name exists), and the steps continue only when .show operations reports the operation as Completed. The current view is kept under a dated name too, and the page says what to do when a rename fails. * Keep deployments out of the swap, and recover after an interrupted one Review of 0ea5eb4: a deployment of the cluster template between the two renames creates GatewayDailyUsageV2 again without backfill. Promotion and rollback then both target an occupied name, and reports read the new view without its history. - The steps say not to deploy the cluster template while they run. - Step 5 runs both renames in one .execute database script with ThrowOnErrors, so no command runs between them. The script stops at the first failure and does not undo the earlier rename. - After a failed script, .show materialized-views lists the views with their EffectiveDateTime. Without the previous view, nothing was replaced. With it, a GatewayDailyUsageV2 next to it was created by a deployment (its EffectiveDateTime is after the start of step 5) and counts only calls that the other two views also count: drop it, then give the name back to the second view or to the previous one. * Recover by the views that exist, and run one backfill at a time Review of c66b21c: - When both renames ran but the response was lost, a second run of the script fails at its first rename. The recovery then took the promoted view for a deployment's view and dropped it. The recovery now decides by the views that exist: without the second view and with the previous one, both renames ran, and GatewayDailyUsageV2 stays when its EffectiveDateTime is the one noted in step 3 (step 3 now notes it). GatewayDailyUsageV2 is dropped only while the second view still exists. Step 5 says to run the script only once. - Two backfill runs with different staging names can both pass the comparison against the same view, and the later one can replace the earlier, more complete view. The page now says to run one backfill at a time and to list the views first (another GatewayDailyUsageV2Backfill view shows a run that has not ended), and step 5 follows the comparison right away. --------- Co-authored-by: Scott Zhu <mengzezhu@microsoft.com>
8 tasks done
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
The daily AI Gateway usage view
GatewayDailyUsagecannot answer two questions that a usage report needs:cetirun_userin the key metadata). Both can repeat: a new Agent can take the name of a deleted Agent, and users from two domains can have the same alias. The view has no Agent UID and no user ID.Kusto cannot change how a materialized view groups its rows (
.alter materialized-viewcannot change the group-by keys), and the template creates the view with.create ifnotexists. This PR therefore adds a new view,GatewayDailyUsageV2, next to the earlier one.Changes
infra/cluster/modules/adx/cetirun-gateway-usage.kql: createsGatewayDailyUsageV2with the same retention parameter and hot cache. Its keys are the earlier ones plus:AgentUid, from the key metadatacetirun_agent_uid.UserId, the AI Gateway user of the key. Since v0.2.0 (Use Verified Owner IDs for Portal and Agent Access #628) this is<tenant>/<object>of the owner, oragent:<Agent UID>for an Agent without owner IDs. Before v0.2.0 it was the alias; those rows keep it.ProviderModelandProvider. For a router call,Modelis the router andProviderModelis the tier model that answered.CopilotRoute:truewhen the deployment ID starts withcetirun-copilot-. The operator registers every Copilot route of an Agent with this prefix (copilotRegistrationID), and the request log records LiteLLM's model ID asdeployment_id.CacheCreationTokens, whichGatewayRequests()already has.backfill = truefails the script on every existing database. The first deployment of this branch over a test cluster's logs database failed this way; the second commit removes it.infrastructure.mddescribes how a database admin creates the view again once with backfill, with theGatewaytable in the hot cache for the time.GatewayDailyUsage. Where an earlier template created it, it keeps its rows and keeps counting. The docs show how a database admin drops it.GatewayRequests()gets the columnCopilotRoute, with the same rule.observability.md), the ADX resources, the earlier view and the optional backfill (infrastructure.md), the Copilot column in the spend query (budgets.md), the retention and the identities that the view keeps (PRIVACY.md), and a note thatmake deploydoes not apply ADX script changes (upgrade.md).scripts/tests/test_cluster_deploy.py: the new view name and policies, no backfill, that the script never names the earlier view, the new keys and aggregations, and that the Copilot prefix in both scripts matches the operator's prefix, its route ID format and the request log'sdeployment_id.Upgrade
make deploydoes not run the ADX scripts. An existing installation gets the new view when the cluster template is deployed again, or when an administrator runscetirun-logs.kqlandcetirun-gateway-usage.kqlin the logs database. The new view then counts the calls from that time. The earlier days stay inGatewayDailyUsage, and inGatewayRequests()for the logs retention, unless an admin backfills the new view asinfrastructure.mddescribes.Validation
Offline:
python3 -m unittest scripts.tests.test_cluster_deployin a Linux container with Bicep 0.47.16 and PyYAML: 28 tests OK, none skipped.bicep build --no-restoreofmain.bicepand every module: OK, no warnings.site/tests: 157 tests OK. Source-level site checks: 0 content errors, no new style findings in the changed pages.scripts/ci/validate.py license-checkandpython-check,git diff --check: OK.Live, on a test cluster with CetiRun v0.2.2 from MCR, ADX-Mon v0.4.1, and a Dev SKU Kusto cluster whose
Gatewaytable had rows since 2026-11-06:GatewayDailyUsageV2is healthy with 400 days of retention and 31 hot days,GatewayDailyUsageis unchanged, andGatewayRequests()hasCopilotRoute.infrastructure.md, run as written: the recreated view counted 26,701 calls and 4.154529 USD from 2026-11-06, the same asGatewayDailyUsage; the table's caching policy was removed again.GatewayDailyUsageshows one owner with 6 calls and one Agent name with 3 calls.GatewayDailyUsageV2shows the two users with 3 calls each, and the three Agent UIDs with 2, 1 and 3 calls. The real Agents of the cluster show the three forms ofUserId: the alias before the upgrade,agent:<UID>for Agents without owner IDs, and<tenant>/<object>for an Agent with them.gpt-6-astrawith the sharedgpt-6-astraand registeredclaude-opus-5.5as Copilot only. Of 34 requests forgpt-6-astra, 21 first went to the Copilot route, failed, and the shared deployment answered.GatewayDailyUsageshowsgpt-6-astrawith 55 calls, 21 of them failed.GatewayDailyUsageV2shows 34 successful calls withCopilotRoute = falseand 21 failed tries withCopilotRoute = true, and the 2claude-opus-5.5tries withCopilotRoute = true.claude-haiku-5.5(2),gemini-3.8-flash(1) and the mergedgpt-6-astra(8) were all answered by its Copilot routes, at the list price. For the client calls ofgpt-6-astrathat day,GatewayDailyUsagehas one row: 77 calls, 25 failed, 0.02032 USD.GatewayDailyUsageV2has 44 calls of the shared deployment (0.0166 USD, none failed) and 33 of Copilot routes (8 answered, 0.00372 USD at the list price; 25 failed tries of the synthetic sign-in).Questions for review
GatewayDailyUsageV2. Another name works the same way.Gatewaytable's hot cache for the creation and removing it after. This PR leaves that to an admin, because it changes a caching policy at every deployment.GatewayDailyUsageitself. This PR keeps it, because a drop would erase days that are older than the request log.