Context
Now that sub-agents and long-running work are becoming more reliable, CodeWhale should audit whether its money/cost calculations are logically correct. Users should also have a simple way to ask the active provider what their real account balance/credits are, where the provider exposes an official endpoint.
Do not assume every provider supports this. Implement /balance as a provider capability: supported providers return structured balance data; unsupported providers return a clear "balance check not supported for this provider" message.
Official endpoints verified so far
- DeepSeek documents
GET https://api.deepseek.com/user/balance, returning is_available plus balance_infos[] with currency, total_balance, granted_balance, and topped_up_balance.
- OpenRouter documents
GET https://openrouter.ai/api/v1/credits, returning total credits purchased and total usage. Docs note a management key is required.
- Novita AI's official API index lists
Get User Balance under Basic APIs, for account balance queries.
Do not ship other providers until their official docs are checked. Some providers may expose billing only through dashboards or organization-level APIs.
Scope
Add a provider-account/billing capability layer and a /balance slash command.
The command should support:
- active provider by default:
/balance
- explicit provider:
/balance deepseek, /balance openrouter, /balance novita
- clear unsupported state for providers without a known balance endpoint
- safe error handling for missing API keys, auth failures, insufficient scopes, and network errors
- no API key leakage in logs, errors, or copied receipts
Suggested architecture
Introduce a provider balance capability, separate from chat completions:
trait ProviderBalanceClient {
fn provider_id(&self) -> ProviderKind;
async fn fetch_balance(&self, auth: ProviderAuth) -> Result<ProviderBalance, BalanceError>;
}
struct ProviderBalance {
provider: ProviderKind,
is_available: Option<bool>,
currency: Option<String>,
total_balance: Option<String>,
granted_balance: Option<String>,
topped_up_balance: Option<String>,
total_credits: Option<f64>,
total_usage: Option<f64>,
remaining_credits: Option<f64>,
source: BalanceSource,
checked_at: SystemTime,
}
This can be shaped to match existing provider abstractions; the important part is that balance/credits support is a capability, not assumed for every OpenAI-compatible provider.
Cost accounting audit
In the same workset or a tightly linked follow-up, audit CodeWhale's estimated spend logic:
- prompt/input token cost
- output token cost
- reasoning token cost, if billed/reported separately
- cache hit vs cache miss input pricing
- provider-specific pricing tables
- sub-agent child usage aggregation
- RLM/agent child calls
- failed/cancelled/stream-interrupted calls
- retries and duplicate sends
- currency display and CNY/USD conversion assumptions
The audit should distinguish:
- provider-reported usage
- CodeWhale-estimated usage
- provider account balance/credits from
/balance
Do not present estimated cost as exact provider billing unless we can reconcile it with provider-reported data.
DeepSeek-specific acceptance criteria
OpenRouter-specific acceptance criteria
Generic UX acceptance criteria
Tests
Documentation
Update docs/settings or a provider diagnostics doc with:
/balance examples.
- Which providers are supported.
- Why unsupported providers cannot be checked programmatically.
- Difference between estimated session cost and provider account balance.
Related
Context
Now that sub-agents and long-running work are becoming more reliable, CodeWhale should audit whether its money/cost calculations are logically correct. Users should also have a simple way to ask the active provider what their real account balance/credits are, where the provider exposes an official endpoint.
Do not assume every provider supports this. Implement
/balanceas a provider capability: supported providers return structured balance data; unsupported providers return a clear "balance check not supported for this provider" message.Official endpoints verified so far
GET https://api.deepseek.com/user/balance, returningis_availableplusbalance_infos[]withcurrency,total_balance,granted_balance, andtopped_up_balance.GET https://openrouter.ai/api/v1/credits, returning total credits purchased and total usage. Docs note a management key is required.Get User Balanceunder Basic APIs, for account balance queries.Do not ship other providers until their official docs are checked. Some providers may expose billing only through dashboards or organization-level APIs.
Scope
Add a provider-account/billing capability layer and a
/balanceslash command.The command should support:
/balance/balance deepseek,/balance openrouter,/balance novitaSuggested architecture
Introduce a provider balance capability, separate from chat completions:
This can be shaped to match existing provider abstractions; the important part is that balance/credits support is a capability, not assumed for every OpenAI-compatible provider.
Cost accounting audit
In the same workset or a tightly linked follow-up, audit CodeWhale's estimated spend logic:
The audit should distinguish:
/balanceDo not present estimated cost as exact provider billing unless we can reconcile it with provider-reported data.
DeepSeek-specific acceptance criteria
/balance deepseekcallsGET https://api.deepseek.com/user/balancewith bearer auth.is_available, currency, total balance, granted balance, and topped-up balance.OpenRouter-specific acceptance criteria
/balance openroutercalls the official credits endpoint if the configured key has the required management scope.Generic UX acceptance criteria
/balancedefaults to the active provider.Tests
Documentation
Update docs/settings or a provider diagnostics doc with:
/balanceexamples.Related
/balancereceipts should follow the same inspect/copy pattern.