OpenCode plugin to display subscription usages for OpenAI, Google, and z.ai
npm install opencode-usage-pluginOpenCode plugin that fetches subscription usage for OpenAI, Google, and z.ai.
- /usage command for all providers
- /usage for a single provider
- Returns normalized JSON for provider usage
- Reads tokens from OpenCode and auth plugins
Create or edit ~/.config/opencode/opencode.json:
``json`
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-usage-plugin@0.0.1"]
}
For local development, use a relative path:
`json`
{
"plugin": ["./path/to/opencode-usage-plugin"]
}
- Install the opencode-openai-codex-auth plugin, or ensure openai auth exists in~/.local/share/opencode/auth.json
(aliases: codex, chatgpt).~/.opencode/auth/openai.json
- The plugin also checks as a fallback.
- Install an Antigravity auth plugin (for example, opencode-antigravity-auth) or ensuregoogle
auth exists in ~/.local/share/opencode/auth.json (alias: antigravity).~/.config/opencode/antigravity-accounts.json
- Ensure accounts are stored in .activeIndex
- The account is used for usage checks (falls back to the first account).
- Add a zai-coding-plan entry in ~/.local/share/opencode/auth.json with the API keyzai
(aliases: , z.ai), or set ZAI_API_KEY.
`bash`
/usage
/usage openai
/usage google
/usage zai-coding-plan
- usage tool returns a JSON array (string) of provider usage resultscommand/usage.md
- formats a human-readable summary
`bash`
npm install
mise run build
npm test
mise run lint
Tests use vitest with a provider-specific auth configuration system.
Run all tests (mocks only, default):
`bash`
npm test
Run tests with real auth:
`bash`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 test
Run specific provider tests:
`bash`
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.ts
Test 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.test flags
Adding new providers:
1. Implement provider function in src/providers/TEST_REAL_NEWPROVIDER_AUTH=0
2. Add to .env.testsrc/providers/newprovider.test.ts
3. Create following existing patternssrc/test-helpers.ts` if needed
4. Add helpers to
Nelson Pires
https://github.com/nelsonPires5/opencode-usage-plugin
MIT License. See the LICENSE file for details.