{"record":{"id":"629c0c6762d05bb1","repo":"xtekky/gpt4free","slug":"cannot-fetch-usage-without-valid-authentication","errorCode":null,"errorMessage":"Cannot fetch usage without valid authentication","messagePattern":"Cannot fetch usage without valid authentication","errorType":"exception","errorClass":"MissingAuthError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/needs_auth/Antigravity.py","lineNumber":1535,"sourceCode":"\n            return models\n        except Exception as e:\n            debug.log(f\"Failed to fetch models: {e}\")\n            return []\n\n    @classmethod\n    async def get_quota(cls, api_key: Optional[str] = None) -> dict:\n        \"\"\"\n        Fetch usage/quota information from the Antigravity API.\n        \"\"\"\n        if cls.auth_manager is None:\n            cls.auth_manager = AntigravityAuthManager(env=os.environ)\n        await cls.auth_manager.initialize_auth()\n\n        access_token = cls.auth_manager.get_access_token()\n        project_id = cls.auth_manager.get_project_id()\n        if not access_token or not project_id:\n            raise MissingAuthError(\"Cannot fetch usage without valid authentication\")\n\n        return await cls.auth_manager.call_endpoint(\n            method=\"fetchAvailableModels\",\n            body={\"project\": cls.auth_manager.get_project_id()},\n        )\n\n    @classmethod\n    async def create_async_generator(\n        cls,\n        model: str,\n        messages: Messages,\n        stream: bool = False,\n        media: MediaListType = None,\n        tools: Optional[list] = None,\n        **kwargs,\n    ) -> AsyncResult:\n        \"\"\"Create an async generator for streaming responses.\"\"\"\n        if cls.auth_manager is None:","sourceCodeStart":1517,"sourceCodeEnd":1553,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/Antigravity.py#L1517-L1553","documentation":"get_quota() requires both a valid access token AND a resolved project ID; this MissingAuthError fires when either is absent after initialize_auth(). Quota/usage queries are per-project, so both pieces of state are mandatory. It usually means login was never completed or credentials were deleted.","triggerScenarios":"Calling Antigravity.get_quota() on a machine without saved credentials, or where initialize_auth() could not restore a token (refresh failed) or where the project ID never got cached/discovered.","commonSituations":"Monitoring/usage-dashboards calling get_quota() before any chat request has forced login; credentials revoked; project discovery never ran.","solutions":["Complete the login flow (which caches token + project) before calling get_quota().","Set ANTIGRAVITY_PROJECT_ID if the token is valid but the project is unknown.","Catch MissingAuthError and re-prompt for authentication in long-running services."],"exampleFix":"from g4f.errors import MissingAuthError\n\ntry:\n    quota = await Antigravity.get_quota()\nexcept MissingAuthError:\n    await Antigravity.login()  # or refresh credentials interactively\n    quota = await Antigravity.get_quota()","handlingStrategy":"try-catch","validationCode":"await Antigravity.auth_manager.initialize_auth()\nif not (Antigravity.auth_manager.get_access_token() and Antigravity.auth_manager.get_project_id()):\n    raise MissingAuthError(\"login required before get_quota()\")","typeGuard":null,"tryCatchPattern":"from g4f.errors import MissingAuthError\n\ntry:\n    quota = await Antigravity.get_quota()\nexcept MissingAuthError:\n    await Antigravity.login()\n    quota = await Antigravity.get_quota()  # single retry after fresh auth","preventionTips":["Perform a successful chat request or login before querying quota","Cache quota responses — no need to call get_quota on every request","Treat MissingAuthError as a signal to re-run the interactive login, not a crash"],"tags":["authentication","quota","antigravity","project-id"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}