Skip to content

feat(console): add go usage endpoint - #16513

Merged
vimtor merged 3 commits into
anomalyco:devfrom
peculiarnewbie:go-usage-api
Aug 11, 2026
Merged

vimtor merged 3 commits into
anomalyco:devfrom
peculiarnewbie:go-usage-api

Conversation

@peculiarnewbie

@peculiarnewbie peculiarnewbie commented Mar 7, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #16017

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

this PR adds an API endpoint at /zen/go/v1/usage that will provide the same opencode go usage data as in the zen workspace dashboard.

If you paste a large clearly AI generated description here your PR may be IGNORED or CLOSED!

How did you verify your code works?

unfortunately i don't think this is a change i can verify as an outside contributor. i did try running bun dev:remote but met with cloudflare api key error.
I did copy the pattern in /zen/v1/models and copied the logic from /workspace/[id]/billing/lite-section.tsx

Screenshots / recordings

not a ui change

Checklist

  • I have tested my changes locally
    i guess running dev server and building counts as testing? but yea i don't think i can test this feature locally
  • I have not included unrelated changes in this PR

If you do not follow this template your PR will be automatically rejected.

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Mar 7, 2026
@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Mar 7, 2026
@github-actions

github-actions Bot commented Mar 7, 2026

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@hendrixfreire

Copy link
Copy Markdown

Hey, any news on merging this?

@peculiarnewbie

Copy link
Copy Markdown
Contributor Author

no clue

@n-osennij

Copy link
Copy Markdown

+++

@patrezp

patrezp commented May 4, 2026

Copy link
Copy Markdown

I would like to see this implemented

@donrami

donrami commented May 9, 2026

Copy link
Copy Markdown

+1 for implementing this. In the meantime I'm using this hacky solution https://pi.dev/packages/pi-go-bars

@OmSoft-tech

Copy link
Copy Markdown

I would like to see this implemented

@slkiser

slkiser commented May 14, 2026

Copy link
Copy Markdown

Official support would be nice.

If you need usage and quota tracking in opencode, https://github.com/slkiser/opencode-quota

@n-osennij

Copy link
Copy Markdown

up

@m-saghir

Copy link
Copy Markdown

I'd employ this feature.

@matiasict

Copy link
Copy Markdown

+1

@Ksld154

Ksld154 commented May 19, 2026

Copy link
Copy Markdown

+1
really need this to check cross-device quota in OpenUsage

@hendrixfreire

hendrixfreire commented May 19, 2026 •

Copy link
Copy Markdown

Did they truly forget about this?

@robinebers

Copy link
Copy Markdown

Would really love this for OpenUsage. A lot of users keep asking, and the local SQL is unreliable.

@edsonboldrini

Copy link
Copy Markdown

+1

@igcodinap

Copy link
Copy Markdown

+1 pls merge this

@stevenup7

Copy link
Copy Markdown
  • 1 for this one, adding my name to the list of people wanting this, using @timm-u pi plugin but screen scraping feels pretty yuck

@rcambrj

rcambrj commented May 27, 2026

Copy link
Copy Markdown

This pull request is now second most wanted, by measure of 👍 reactions

Screenshot 2026-05-27 at 08 50 58

Opencode continues to see improvements and bugfixes, which I'm eternally grateful for, but I do wonder what's holding this up. It seems like a quick QOL improvement for your paying subscribers. Is there something which needs to be fixed, opencode maintainers?

Adam-Dalloul added a commit to Adam-Dalloul/codeg that referenced this pull request Aug 22, 2026
Shipped 2026-08-11 in anomalyco/opencode#16513. percent is used
0-100 across rolling/weekly/monthly. No invented numbers.
JaminYe pushed a commit to JaminYe/pane that referenced this pull request Aug 24, 2026
anomalyco/opencode#16513 shipped GET /zen/go/v1/usage (2026-08-11) —
the endpoint the code comment said to swap to when it existed. The
card now shows Session/Weekly/Monthly percentages and resets counted
on OpenCode servers (same numbers as the Zen dashboard, other devices
and shared-subscription participants included), authenticated with the
Go key Pane already reads from auth.json.

The live wire shape differs from the merged PR draft — verified
against the deployed endpoint with a real key and pinned in a test:
usage.{rolling,weekly,monthly}.{status,percent,resetsAt}. The old
local-window computation stays as the fallback when the API is
unreachable; dollar spend remains local. README + providers.md drop
the "no public usage API yet" story.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
bibhubhatta pushed a commit to bibhubhatta/ClaudeBar that referenced this pull request Aug 25, 2026
The local-DB probe only sees this machine's messages and guesses at the
server's cost accounting, so its numbers diverged from the opencode.ai
dashboard. opencode now exposes GET /zen/go/v1/usage (anomalyco/opencode#16513),
which returns the same rolling/weekly/monthly percentages and reset times
the dashboard shows.

- Add OpenCodeCredentialLoader: reads OPENCODE_API_KEY or the
  `opencode-go` / `opencode` entry from $XDG_DATA_HOME/opencode/auth.json
- Add OpenCodeAPIUsageProbe: calls the endpoint with a bearer key, maps
  used% -> remaining%, 401 -> sessionExpired, 403 -> subscriptionRequired
- Keep OpenCodeUsageProbe (local DB) as a fallback when no key is present

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CdVhUZgY4SKaYkuxdqR3AY
1056674754 added a commit to 1056674754/opencode that referenced this pull request Aug 31, 2026
Upstream v1.18.16 (a3647eb) -> v1.18.17 (02546df), 32 commits.

Functional changes:
- fix(compaction): clearer instructions for smaller models like dsv4 flash (anomalyco#42045)
- fix(provider): add Merge Gateway reasoning variants (anomalyco#41867)
- fix(opencode): cap session retries with jitter (anomalyco#41939) - retry policy
  gains RETRY_MAX_RETRIES=5 global cap and 25% jitter
- fix(opencode): detect Copilot PDF input support (anomalyco#41522)
- fix(provider): scope DeepSeek V4 Flash sampling defaults (anomalyco#41620)
- fix(session): route all Muse family models to the Meta system prompt (anomalyco#41581)
- fix(app): use current default model (anomalyco#38603)
- feat(console): add go usage endpoint (anomalyco#16513) and r2 data catalog stats
- fix(ui): correct OC-2 weak icon color (anomalyco#41504)
- fix(i18n): use widely recognized developer terminology (anomalyco#41532)

Conflicts resolved: 30 package.json version bumps (rebranded 1.18.17-sscity),
bun.lock (took upstream, rebranded workspace entries via bun install), and
session/retry.ts (kept fork per-reason maxAttempts cap alongside upstream
RETRY_MAX_RETRIES global cap and jitter).
androidand pushed a commit to androidand/opencode-skein that referenced this pull request Sep 18, 2026
Co-authored-by: vimtor <vn4varro@gmail.com>
androidand pushed a commit to androidand/opencode-skein that referenced this pull request Sep 19, 2026
Co-authored-by: vimtor <vn4varro@gmail.com>
androidand pushed a commit to androidand/opencode-skein that referenced this pull request Sep 19, 2026
Co-authored-by: vimtor <vn4varro@gmail.com>
rldona pushed a commit to rldona/FlupCode that referenced this pull request Sep 24, 2026
Co-authored-by: vimtor <vn4varro@gmail.com>
eliahilse added a commit to eliahilse/kyora that referenced this pull request Sep 29, 2026
…ows (#25)

## What

`kyora-switch` can now save, load, clear and show usage for OpenCode
subscription logins, the same way it does for Claude Code and Codex.

```bash
kyora-switch opencode save work
kyora-switch opencode load private
kyora-switch opencode usage       # or plain `kyora-switch usage` for every provider
```

## Usage source

OpenCode Go has a real usage endpoint that you call with the Go key. No
local estimate is needed.

- `GET https://opencode.ai/zen/go/v1/usage` with `Authorization: Bearer
<key>`. Source:
[`packages/console/app/src/routes/zen/go/v1/usage.ts`](https://github.com/anomalyco/opencode/blob/dev/packages/console/app/src/routes/zen/go/v1/usage.ts),
added in anomalyco/opencode#16513.
- It returns `{ usage: { rolling, weekly, monthly } }`. Each window has
a `status` (`ok` or `rate-limited`), a `percent` (a floored whole number
from 0 to 100) and `resetsAt` (an ISO timestamp). The server computes
these in `Subscription.analyze{Rolling,Weekly,Monthly}Usage`.
- Errors: 401 `AuthError` for a missing or unknown key, 403
`EntitlementError` ("OpenCode Go subscription required.").
- The limits are dollar amounts. The Go docs set the 5-hour window at
20% of the monthly limit, the weekly at 50% and the monthly at 100%. The
month runs from the subscription date.
- Other tools read the same route: oh-my-pi, Orca, CodexBar and
OmniRoute.
- Zen (`opencode`) is pay-as-you-go. It has no windows and no usage
route, so a Zen-only slot shows `quotaHint`.

## Design notes

- **A slot holds only the `opencode-go` and `opencode` entries** of
`$XDG_DATA_HOME/opencode/auth.json` (default
`~/.local/share/opencode/auth.json`). A load merges them back in with
`writeFileAtomic` at 0600, as two-space JSON, which is how opencode
writes the file itself. Other providers in the file (openai,
zai-coding-plan, xai, …) are left alone. `forget()` removes only those
two keys and never creates the file.
- **When a slot has no entry for one of the two:** load removes the live
entry only if it holds the outgoing login's own key. The common case is
one key connected as both Go and Zen. If that entry stayed, the old
account would still be half signed in, and the next sync would save its
key into the slot you just loaded. A Zen key that differs from the
outgoing Go key is treated as a separate login and kept. The outgoing
entries are backed up first either way. This is documented in the README
and in the `mergeSubscriptions` doc comment.
- **Identity:** the usage route returns no workspace or account name, so
a login is named `key …abcd`. That's the last four characters of the
key, which are also the four the OpenCode console shows. The Go key
names the login if there is one, otherwise the Zen key does. The plan
shows as `Go`, `Zen` or `Go + Zen`. The name stays the same for a given
key, so sync matches the right slot.
- There's no `refresh` or `credentialExpiry`, because API keys don't
expire.
- `doctor` warns when `OPENCODE_AUTH_CONTENT` is set, because opencode
then reads that variable instead of the file.
- **@kyora-sh/usage** now also exports `opencodeKey(authText,
"opencode-go" | "opencode")` and `opencodeAuthPath()`. A follow-up can
use them to add a `usageProbe` to the review `opencode` engine. This PR
doesn't touch `packages/review`.
- **Test preload:** `src/test-setup.ts` now points `KYORA_SWITCH_DIR`,
`XDG_DATA_HOME`, `CODEX_HOME` and `CLAUDE_CONFIG_DIR` at an empty
sandbox before any test runs. A test that forgets to set its own
directory can't reach a real login.
- The store format is unchanged, so existing claude and codex slots load
exactly as before.

## Verification

- `bun test`: switch 69 pass, usage 28 pass. Running from the repo root
with the root preload gives 97 pass.
- `bunx tsc --noEmit` is clean in `packages/switch/cli`,
`packages/shared/usage`, `packages/review/cli` and
`packages/council/mcp`.
- I ran save, load, list, status, usage, clear, load and doctor end to
end in a full sandbox, with temp HOME, XDG, CODEX and CLAUDE dirs, the
keychain off, fake keys and a mock usage server.
- I made one read-only probe of the real Go key with a temp
`KYORA_SWITCH_DIR`. It returned `5h 0% used · weekly 0% used · monthly
0% used`. auth.json kept the same mtime, size and mode afterwards, and
no slot files were written. The zeros fit the local history: the last
opencode-go message in opencode's db was 13 days ago.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

[FEATURE]: Add Go plan usage/balance API endpoint (rolling/weekly/monthly windows)