Page summary

Check provider quota, subscription cycle, balance, and current usage status.

2.5 Usage Query

CC Switch's quota / balance display is split into two categories: Auto Display (OAuth account types, works out of the box) and Manual Enable (built-in templates + custom scripts, shown only after you configure them on the provider card).

This page covers the quota / balance shown on provider cards. Statistics for requests, tokens, and spend are on the "Usage" page in the left sidebar; see 4.4 Usage Statistics.

CategoryScopeUser Enable Required
Auto DisplayGitHub Copilot, Codex OAuth reverse proxy, xAI OAuthNo
Manual Enable (built-in templates)Official subscriptions (Claude / Codex / Gemini / Grok Build official providers), Token Plan, third-party balance queryYes (see below)
Manual Enable (custom script)Proxies, private deployments, special APIs not covered by built-in templatesYes (see below)

Auto Display (OAuth Account Types)

The following account-type providers automatically display the quota at the bottom of the provider card once enabled — no additional configuration required:

CategoryCovered ProvidersDisplayed Content
GitHub CopilotCopilot provider cardPremium interactions remaining
Codex OAuthCodex OAuth reverse proxy card (Claude provider)ChatGPT account Codex quota
xAI OAuthxAI (Grok) OAuth provider cardGrok account quota

Auto Display Interactions


Manual Enable (Built-in Templates + Custom Scripts)

Besides the auto-display account types above, all other providers (including official subscriptions, Token Plan, third-party balance queries, and various relay services) need "Enable usage query" to be manually turned on in the provider card before any quota is displayed.

Official Subscription Quota

The subscription quota of the Claude / Codex / Gemini / Grok Build official providers is a built-in template that is off by default:

  1. On the official provider card, click the "More" (⋯) button on the right and choose Configure usage query
  2. Turn on "Enable usage query" and choose the Official Subscription template
  3. After saving, the subscription quota shows at the bottom of the card (for Grok Build, the SuperGrok quota). The official subscription quota reads the local CLI's OAuth credentials and calls the official API, and it only makes requests once enabled

Exception: an OpenAI Official provider bound to a ChatGPT account in Accounts has it on by default.

Why do these need manual enabling?

One important reason: the same request URL (same vendor) may expose multiple query modes — for example, both plan-based quota queries and account-level balance queries. CC Switch cannot automatically infer which one you want, so the built-in query for such providers is disabled by default, leaving you to pick the right template.

Built-in Template Coverage

There are ready-to-use built-in templates for the following categories — no script writing required:

CategoryCovered ProvidersTemplate Type
Official subscriptionClaude / Codex / Gemini / Grok Build official providersOfficial subscription quota
Token PlanKimi / Zhipu GLM / MiniMax / Volcengine Ark / OpenCode Go / Command CodePlan quota (with usage progress)
Third-party balanceDeepSeek / StepFun / SiliconFlow / OpenRouter / Novita AIOfficial balance query

💡 Tip: Beyond these built-in templates, for uncovered providers you can use the custom script approach (see below) to write your own query logic.

Enable Steps

  1. Hover over the provider card and click the "More" (⋯) button on the right
  2. Choose Configure usage query (the first time, a confirmation appears; click "I understand, configure")
  3. At the top of the configuration panel, toggle on Enable usage query
  4. Select the right built-in template (e.g., Token Plan, third-party balance) or choose "Custom"
  5. Fill in API Key / Base URL / Access Token as needed (most cases can be left blank, reusing the provider's own credentials)
  6. Click Test script to verify the query returns successfully
  7. Save — the quota will show up at the bottom of the card

⚠️ Note: The auto-refresh interval after enabling is controlled by the "Auto Query Interval" field (set to 0 to disable auto-refresh). Background queries only trigger when the provider is in the "In use" state.


Custom Script Query (Advanced)

Overview

When a provider is not covered by the built-in templates, you can write a custom JavaScript query script. Suitable for relay services, private deployments, special API formats, etc.

Use cases:

Open Configuration

  1. Hover over the provider card and click the "More" (⋯) button on the right
  2. Choose "Configure usage query"
  3. Opens the usage query configuration panel

Enable Usage Query

At the top of the configuration panel, enable the "Enable usage query" toggle.

Preset Templates

The preset template dropdown offers: Custom, General, NewAPI, Sub2API, plus the built-in templates above such as Token Plan, Official, and Official Subscription. The four most commonly used for custom scripts:

Custom Template

Fully customizable request and extraction logic, suitable for special API formats.

Generic Template

Suitable for most providers with standard API formats:

({
  request: {
    url: "{{baseUrl}}/user/balance",
    method: "GET",
    headers: {
      "Authorization": "Bearer {{apiKey}}",
      "User-Agent": "cc-switch/1.0"
    }
  },
  extractor: function(response) {
    return {
      isValid: response.is_active || true,
      remaining: response.balance,
      unit: "USD"
    };
  }
})

Configuration parameters:

ParameterDescription
API KeyAuthentication key (optional, uses provider's key if empty)
Base URLAPI base URL (optional, uses provider's endpoint if empty)

New API Template

Designed specifically for New API-type relay services:

({
  request: {
    url: "{{baseUrl}}/api/user/self",
    method: "GET",
    headers: {
      "Content-Type": "application/json",
      "Authorization": "Bearer {{accessToken}}",
      "New-Api-User": "{{userId}}"
    },
  },
  extractor: function (response) {
    if (response.success && response.data) {
      return {
        planName: response.data.group || "Default Plan",
        remaining: response.data.quota / 500000,
        used: response.data.used_quota / 500000,
        total: (response.data.quota + response.data.used_quota) / 500000,
        unit: "USD",
      };
    }
    return {
      isValid: false,
      invalidMessage: response.message || "Query failed"
    };
  },
})

Configuration parameters:

ParameterDescription
Base URLNew API service URL
Access TokenAccess token
User IDUser ID

Sub2API Template

For relay sites built on Sub2API. It queries /v1/usage with the provider's own API Key, so no access token is needed, and the provider URL works with or without /v1.

({
  request: {
    url: "{{baseUrl}}".replace(/\/+$/, "").replace(/\/v1$/, "") + "/v1/usage",
    method: "GET",
    headers: {
      "Authorization": "Bearer {{apiKey}}",
      "User-Agent": "cc-switch/1.0"
    }
  },
  extractor: function (response) {
    var quota = response.quota || {};
    var result = {
      isValid: response.isValid !== false,
      unit: response.unit || quota.unit || "USD"
    };
    if (typeof response.planName === "string") result.planName = response.planName;
    if (typeof response.remaining === "number" && response.remaining >= 0) {
      result.remaining = response.remaining;
    }
    if (quota.limit > 0) {
      result.used = quota.used;
      result.total = quota.limit;
    }
    if (!result.isValid) {
      result.invalidMessage = response.status
        ? String(response.status)
        : "Query failed";
    }
    return result;
  },
})

Configuration parameters:

ParameterDescription
API KeyKey used for authentication (optional, falls back to the provider's key)
Base URLSite URL (optional, falls back to the provider endpoint)

General Configuration

Timeout

Request timeout in seconds, default 10 seconds.

Auto Query Interval

Interval for automatically refreshing usage data (minutes):

Extractor Return Format

The extractor function returns an object containing the following fields. All fields are optional:

FieldTypeRequiredDescription
isValidbooleanNoWhether the account is valid, defaults to true
invalidMessagestringNoMessage when invalid
remainingnumberNoRemaining balance
unitstringNoUnit (e.g., USD, CNY, times)
planNamestringNoPlan name (supports multi-plan)
totalnumberNoTotal balance
usednumberNoUsed amount
extrastringNoAdditional display text

Test Script

After configuration, click the "Test script" button to verify:

  1. Sends a request to the configured URL
  2. Executes the extractor function
  3. Displays the returned result or error message

Display

After successful configuration, the provider card displays:

Variable Placeholders

The following placeholders can be used in scripts and are automatically replaced at runtime:

PlaceholderDescription
{{apiKey}}Configured API Key
{{baseUrl}}Configured Base URL
{{accessToken}}Configured Access Token (New API)
{{userId}}Configured User ID (New API)

Common Provider Configuration Examples

Troubleshooting

Quota Not Displayed for Official Subscription or Account-Type Providers

Check:

  1. Official providers (Claude / Codex / Gemini / Grok Build): make sure "Enable usage query" is turned on under "Usage Query" and the Official Subscription template is selected (off by default)
  2. The provider is in the "In use" state (providers not in use do not trigger background queries)
  3. For OAuth account types (Copilot / Codex OAuth / xAI OAuth), check whether the token is still valid; if the card shows "Session expired", log in again under Accounts in the left sidebar
  4. Network access to the official quota endpoint

Manual Enable Still Not Showing Quota

Check:

  1. Whether the Enable usage query toggle at the top of the "Usage Query" panel is on
  2. Whether a suitable built-in template (Token Plan / third-party balance / custom) is selected
  3. Click Test script to see the specific error
  4. Required fields such as API Key / Base URL are filled correctly
  5. Network access to the provider's quota endpoint
  6. Background auto-refresh only triggers when the provider is in the "In use" state

Query Failed

Check:

  1. Is the API Key correct
  2. Is the Base URL correct
  3. Is the network accessible
  4. Is the timeout sufficient

Empty Response Data

Check:

  1. Does the extractor function have a return statement
  2. Does the response data structure match the extractor
  3. Use "Test script" to view the raw response

Format Failed

When there is a script syntax error, clicking the "Format" button will indicate the error location.

Notes