OpenCode plugin that fetches subscription usage for OpenAI, Google, and z.ai.
/usagecommand for all providers/usage <provider>for a single provider- Returns normalized JSON for provider usage
- Reads tokens from OpenCode and auth plugins
Create or edit ~/.config/opencode/opencode.json:
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-usage-plugin@0.0.1"]
}For local development, use a relative path:
{
"plugin": ["./path/to/opencode-usage-plugin"]
}- Install the
opencode-openai-codex-authplugin, or ensureopenaiauth exists in~/.local/share/opencode/auth.json(aliases:codex,chatgpt). - The plugin also checks
~/.opencode/auth/openai.jsonas a fallback.
- Install an Antigravity auth plugin (for example,
opencode-antigravity-auth) or ensuregoogleauth exists in~/.local/share/opencode/auth.json(alias:antigravity). - Ensure accounts are stored in
~/.config/opencode/antigravity-accounts.json. - The
activeIndexaccount is used for usage checks (falls back to the first account).
- Add a
zai-coding-planentry in~/.local/share/opencode/auth.jsonwith the API key (aliases:zai,z.ai), or setZAI_API_KEY.
/usage
/usage openai
/usage google
/usage zai-coding-planusagetool returns a JSON array (string) of provider usage resultscommand/usage.mdformats a human-readable summary
npm install
mise run build
npm test
mise run lintTests use vitest with a provider-specific auth configuration system.
Run all tests (mocks only, default):
npm testRun tests with real auth:
# Edit .env.test and set desired provider(s) to 1
TEST_REAL_OPENAI_AUTH=1
TEST_REAL_GOOGLE_AUTH=1
TEST_REAL_ZAI_CODING_PLAN_AUTH=1
npm testRun specific provider tests:
npm test -- src/providers/openai/fetch.test.ts
npm test -- src/providers/google/fetch.test.ts
npm test -- src/providers/zai-coding-plan/fetch.test.tsTest structure:
src/providers/common/test-helpers.ts- Shared test utilities and fixturessrc/providers/*/*.test.ts- Provider-specific tests with mocks and real auth.env.test- Auth configuration flags (committed to repo)vitest.config.ts- Test configuration
Tests are organized into two categories per provider:
- Mock tests - Run by default, test all edge cases and error handling
- Real auth tests - Skipped by default, enabled via
.env.testflags
Adding new providers:
- Implement provider function in
src/providers/ - Add
TEST_REAL_NEWPROVIDER_AUTH=0to.env.test - Create
src/providers/newprovider.test.tsfollowing existing patterns - Add helpers to
src/test-helpers.tsif needed
Nelson Pires nelsonpires.sn@gmail.com
https://github.com/nelsonPires5/opencode-usage-plugin
MIT License. See the LICENSE file for details.