You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
usage: find the caller's org without a remote, and show credits - #219
Outside a repo with an origin remote, kai usage fell back to the placeholder tenant "default" and failed with org not found.
Org choice, in order:--org (new) or KAI_ORG, then the origin remote's tenant, then the caller's personal org from /api/v1/orgs, which falls back to the first org. That is the same picker kai init uses. An --org the caller doesn't belong to fails with the list of orgs they do belong to, and the usage error now names the org it asked about.
Credits: a new line shows the balance that pays for usage past the caps, and says whether it is the personal pot or the org pool. Under org scope, any personal balance gets its own line.
Draft until kai-engine#129 is merged and tagged. This branch's go.mod still pins engine v0.6.86, so it won't build until the bump is pushed here.
Verified
Built against the engine branch with a scratch go.work and run against production on a single-org free account:
No remote: the personal org is picked, and the credit line prints
Remote whose tenant is that org: the remote's tenant is used
Remote with an unknown tenant: the error names that org
--org and KAI_ORG with an unknown slug: the error lists the caller's orgs
go vet ./cmd/kai and go test ./cmd/kai -run 'Usage|Org' pass
Not checked live: the org-scope (seated) credit line.
Written by Kai from the change itself. Kai only edits this section; everything above it is the author's.
kai usage now resolves the org via --org/KAI_ORG, the origin remote's tenant, or the caller's personal org from /api/v1/orgs instead of falling back to the placeholder "default", and prints a credit-balance line indicating whether the pot is personal or org-scoped.
Changes
Added --org flag (usageOrgFlag) to usageCmd in cmd/kai/main.go, also readable via KAI_ORG
Added resolveUsageOrg in cmd/kai/main.go: checks --org then KAI_ORG, then returns client.Tenant when an origin remote exists, otherwise calls ListOrgs and picks the personal org via pickPersonalOrg; an explicit slug that doesn't match produces an error listing the caller's orgs
runUsage now calls resolveUsageOrg instead of using client.Tenant directly, and the error message names the org queried
runUsage prints a new Credits: line: under BillingScope == "org" it shows the org-pool balance and, if PersonalCreditCents > 0, a second personal line; otherwise it shows the personal balance
Updated usageCmd.Long help text to document the org-selection order and the --org example
Testing
go vet ./cmd/kai and go test ./cmd/kai -run 'Usage|Org' pass
Verified against production on a single-org free account: no-remote personal-org fallback, remote-tenant selection, unknown-tenant error naming the org, and --org/KAI_ORG with an unknown slug listing the caller's orgs
Outside a repo with an origin remote, `kai usage` asked about the
placeholder tenant "default" and failed with "org not found". It now
takes --org / KAI_ORG, then the origin remote's tenant, then the
caller's personal org from /api/v1/orgs. It also prints the credit
balance that pays for usage past the caps, so a capped account with
credits no longer reads as blocked.
Needs the UsageResponse credit fields from kai-engine#129.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
Kai review · Your call, then merge (8/10)
Read the whole thing — it does what it says, and nothing jumped out at me.
Next: read the reviewer's notes below before you merge — it had reservations it didn't list as findings.
To get to 10/10
−1 · the reviewer has reservations it did not list as findings — read its notes
−1 · add or update a test: cmd/kai/main.go changed and no test did
Important files changed
File
Change
cmd/kai/main.go
modified · +64 −4
How I reviewed it — 1 file, 6 turns, 1m5s
0 confirmed findings, 7 refuted. Intent verified.
What I opened
cmd/kai/main.go
Full read-through
Scope
cmd/kai/main.go: runUsage and resolveUsageOrg
findOrgBySlug, orgSlugs and pickPersonalOrg helpers
the kai init org-selection code path, for comparison
cmd/kai/main.go resolveUsageOrg and runUsage
pickPersonalOrg, findOrgBySlug and orgSlugs helpers
No proposed defect was confirmed by this check within the reviewed scope.
Size S · Risk S · +64 −4 · 1 file · reaches 0 · the full analysis · Reply to any of my comments and I'll answer, or say @kaicontext anywhere on this PR — a question, or "take another look at the retry logic".
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
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.
Outside a repo with an
originremote,kai usagefell back to the placeholder tenant"default"and failed withorg not found.Org choice, in order:
--org(new) orKAI_ORG, then the origin remote's tenant, then the caller's personal org from/api/v1/orgs, which falls back to the first org. That is the same pickerkai inituses. An--orgthe caller doesn't belong to fails with the list of orgs they do belong to, and the usage error now names the org it asked about.Credits: a new line shows the balance that pays for usage past the caps, and says whether it is the personal pot or the org pool. Under org scope, any personal balance gets its own line.
Draft until kai-engine#129 is merged and tagged. This branch's
go.modstill pins engine v0.6.86, so it won't build until the bump is pushed here.Verified
Built against the engine branch with a scratch
go.workand run against production on a single-org free account:--organdKAI_ORGwith an unknown slug: the error lists the caller's orgsgo vet ./cmd/kaiandgo test ./cmd/kai -run 'Usage|Org'passNot checked live: the org-scope (seated) credit line.
🤖 Generated with Claude Code
Kai context
Written by Kai from the change itself. Kai only edits this section; everything above it is the author's.
kai usagenow resolves the org via--org/KAI_ORG, the origin remote's tenant, or the caller's personal org from/api/v1/orgsinstead of falling back to the placeholder"default", and prints a credit-balance line indicating whether the pot is personal or org-scoped.Changes
--orgflag (usageOrgFlag) tousageCmdincmd/kai/main.go, also readable viaKAI_ORGresolveUsageOrgincmd/kai/main.go: checks--orgthenKAI_ORG, then returnsclient.Tenantwhen anoriginremote exists, otherwise callsListOrgsand picks the personal org viapickPersonalOrg; an explicit slug that doesn't match produces an error listing the caller's orgsrunUsagenow callsresolveUsageOrginstead of usingclient.Tenantdirectly, and the error message names the org queriedrunUsageprints a newCredits:line: underBillingScope == "org"it shows the org-pool balance and, ifPersonalCreditCents > 0, a second personal line; otherwise it shows the personal balanceusageCmd.Longhelp text to document the org-selection order and the--orgexampleTesting
go vet ./cmd/kaiandgo test ./cmd/kai -run 'Usage|Org'pass--org/KAI_ORGwith an unknown slug listing the caller's orgs