Skip to content

usage: find the caller's org without a remote, and show credits - #219

Open
jschatz1 wants to merge 1 commit into
mainfrom
feat/usage-org-credits
Open

jschatz1 wants to merge 1 commit into
mainfrom
feat/usage-org-credits

Conversation

@jschatz1

@jschatz1 jschatz1 commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

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.

🤖 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 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>
@jschatz1
jschatz1 marked this pull request as ready for review October 26, 2026 11:48

@kaicontext kaicontext Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewView in KaiKai 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".

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.

1 participant