Skip to content

[Bug] CLI 3.15.1 built from source: every model request fails with 401 — api.z.ai/api/auth/z/login returns "Z.ai user information is invalid" right after successful zcode login #1001

Open

Description

@timur-ai

Environment

  • OS: Ubuntu 24.04 x64 (VPS, no GUI session for CLI; browser OAuth opened via desktop)
  • ZCode CLI: 3.15.1, built from source (main @ aac4755666d09fdcd70272fcf063c077a639015f, same commit as tag v3.15.1), installed via the generated install.sh → ~/.zcode/runtime + ~/.local/bin/zcode
  • Node v24.14.0, pnpm 10.33.2 (per mise.toml)
  • Model channel: Z.ai Coding Plan — account:zai-individual-coding-plan/GLM-5.3

Steps to reproduce

  1. Build the CLI from source per README and install with install.sh.
  2. zcode login zai (browser OAuth) → completes successfully:
    Login successful as <name>. Model: account:zai-individual-coding-plan/GLM-5.3
    The credentials store ~/.zcode/v2/credentials.json gets oauth:zai:access_token, identity, auth-source and encrypted account api-keys (verified by listing keys; values redacted).
  3. zcode --prompt "Reply with one word: ok" --cwd /tmp (TUI and --web behave identically — UI renders, model calls fail).

Actual

With ZCODE_DEBUG=1:

[project-access-token] { family: 'zai', stage: 'resolve', code: 'project_token_login_failed' }
Error: Turn execution failed

The API call itself:

APICallError [AI_APICallError]: Unauthorized
  url: 'https://api.z.ai/api/anthropic/v1/messages'
  statusCode: 401
  responseBody: '{"error":{"message":"Invalid Authentication Token, please confirm the correct transmission of the Authentication Token","type":"1002"}}'

The failing step is the business-token exchange. Reproduced directly (token value redacted; taken from the store seconds after a successful login):

curl -s -X POST https://api.z.ai/api/auth/z/login \
  -H 'Content-Type: application/json' \
  -d '{"token":"<redacted oauth access_token>"}'
# → {"code":500,"msg":"Z.ai user information is invalid","data":null,"success":false}

The same endpoint returns the same 500 "Z.ai user information is invalid" for:

  • the coding-plan API key used by a working legacy provider instance in ~/.zcode/cli/config.json (zai-api template, access type zhipu-coding-plan-api-key),
  • the zcodejwttoken entry from the credentials store.

Expected

After a successful browser login, CLI model requests should authenticate; or zcode login should fail visibly if the account/plan is not entitled for the CLI token flow.

Notes

  • 4 separate successful logins over ~1 hour (fresh OAuth token each time) — identical result; the token was seconds old when /api/auth/z/login rejected it.
  • On the same machine and account, ZCode Desktop 3.14.4/3.14.5 (agent core 0.16.9) works fine — GUI and its bundled headless runtime (node zcode.cjs --prompt ... from the AppImage mount) both complete model requests using the same coding-plan subscription.
  • zcode --version, TUI rendering and zcode --web all work; only model calls fail.
  • Nothing sensitive attached; all token values redacted.

Question

Is POST /api/auth/z/login expected to accept the OAuth access token issued by zcode login zai for coding-plan accounts? From the outside this looks either server-side (account rejected on the business-login endpoint) or a mismatch between the 3.15.1 CLI token flow and the endpoint contract. Happy to test a fix or provide more diagnostics.

Activity

  1. github-actions commented on Oct 11, 2026

    @github-actions

    👋 感谢你的反馈,我们已经收到。

    • 维护者看到后会尽快回复你。
    • 状态保持为 status: 待评估,你可以随时补充信息。
    • 信息不全时我们会打上 needs: 更多信息 标签并 @ 你。

    👋 Thanks — we've received your issue.

    • A maintainer will get back to you as soon as we can.
    • Status stays at status: 待评估 (Triage); feel free to add context.
    • If we need more details, we'll add needs: 更多信息 and ping you.
  2. dooee commented on Oct 11, 2026

    @dooee

    Independently reproduced on macOS arm64 (v3.15.1 built from source), so this
    does not look Linux specific. Quick repro with a clean credential store:

    mkdir -p /tmp/fresh-home
    HOME=/tmp/fresh-home zcode login     # completes: "Login successful as <user>"
    HOME=/tmp/fresh-home zcode -p "hi"   # 401 on every request

    Analysis (from the shipped bundle): this looks like a silent fallback in the credential exchange path. The OAuth token is supposed to be exchanged at api.z.ai/api/auth/z/login for the token the model API actually accepts:

    const business = ... request(`${ZAI_API_HOST}/api/auth/z/login`, {
      method: "POST", body: JSON.stringify({ token: oauth })
    }).catch(() => { /* exchange failure swallowed */ });
    const loginToken = business?.token ?? oauth;   // falls back to the raw OAuth token

    When the exchange fails, the raw OAuth token is sent to /api/anthropic/v1/messages, which rejects it:

    401 {"error":{"message":"Invalid Authentication Token, please confirm the correct
    transmission of the Authentication Token","type":"1002"}}
    

    Two fingerprints of this state: metadata.user_id contains "account_uuid":"", and sending the stored token directly to the API (both Authorization: Bearer and x-api-key) also returns 401, so the token itself is not accepted regardless of header style.

    Workaround that works today: the coding plan API key credential issued by the desktop app (~/.zcode/v2/credentials.json, with account-provider:...:auth-source set to manual). With that credential in place, model calls work normally.

    Documented in more detail here (an unofficial installer that builds the CLI from official source): https://github.com/dooee/zcode-cli-install-helper#account-setup-and-the-current-one-time-desktop-requirement

    Possible fixes: accept CLI-issued tokens at the exchange endpoint, and/or make the CLI fail loudly (project_token_login_failed) instead of silently falling back to the unusable token.

  3. bhanuharya commented on Oct 13, 2026

    @bhanuharya

    Independent reproduction on the same commit, plus a question for the maintainers.

    Environment

    • Ubuntu 24.04 x64
    • ZCode v3.15.1 built from source at aac4755666d09fdcd70272fcf063c077a639015f (pnpm build:zcode, Node 24.14.0, pnpm 10.33.2)
    • Run as the web server (server/entry-http.js, what zcode --web starts)
    • Account with an active Coding Plan subscription

    What happens

    The UI and sign-in work. Every turn on account:zai-individual-coding-plan fails:

    Turn execution failed
    provider=account:zai-individual-coding-plan model=GLM-5.3-Flash request=fb43eb17-5561-4060-8af9-00d08849cddc reason=auth_failed status=401 retryable=false
    Unauthorized
    

    (TraceID be49e84e-d7d8-41df-b346-37ead58ebbe4, 2026-10-13 01:37:53 UTC.) The agent log shows model.project_token.refresh_retry — "Model PAT rejected; refreshing once before retry", statusCode: 401 — and then the same 401 on the retry. Signing out and in again (fresh OAuth) changes nothing.

    Another occurrence the same day, with GLM-5.3:

    TraceID: 17a7d56b-119c-4a06-9d40-8542aadc6280
    provider=account:zai-individual-coding-plan model=GLM-5.3 request=e5a330c1-bd65-4d80-98b3-c2f8250691f5 reason=auth_failed status=401 retryable=false
    Unauthorized
    

    Same machine, same account, same day

    • The official desktop build (3.10.2 AppImage) completes turns on the plan normally.
    • A Coding Plan API key for this account is accepted (HTTP 200) at https://api.z.ai/api/anthropic/v1/messages, at https://api.z.ai/api/coding/paas/v4/chat/completions, and at the gateway path the client reroutes to, https://zcode.z.ai/api/v1/ultra-zai/anthropic/v1/messages.

    So the account and the plan are fine; it is the account-login token route that is refused.

    Workaround confirmed

    Adding the Coding Plan API key manually as a provider (the "Z.ai Coding Plan" template, provider id zai-api) works in the source build. Prompt caching is applied on that route: one GLM-5.3-Flash turn reported 20,612 input tokens, of which 17,920 were cache reads.

    Possibly related

    In the local model-io records, the official build's requests for this account carry x-aliyun-captcha-verify-param and x-aliyun-captcha-verify-region. The source build's requests carry neither, and packages/shared/src/request-security-edition/ in the public tree is stubs (requiresRequestVerification() returns false). I can't tell from outside whether that is the cause or just another difference.

    Questions

    1. Is signing in with a Coding Plan account expected to work in builds made from this repository (zcode, zcode --web)? If it is not supported, could the client say so instead of returning a bare 401?
    2. With the API-key workaround, is usage treated the same as signing in? The in-app Entitlement Rules say: "When you use GLM through Coding Plan in ZCode, quota consumption is converted at a 0.67 coefficient throughout the campaign period." Does that coefficient apply when the same plan is used through a manually added API key in ZCode, or only through the account sign-in of the official build? (Same question as [Q] 自行编译的开源版 ZCode CLI/TUI 是否享受 Coding Plan 1.5 倍使用额度(67% 扣减)? #749; related request: [建议 / Feature] 官方 zcode 命令行版发行包稳定发布:zcode --web 浏览器工作台开箱即用,Coding Plan 权益对齐桌面版 #865.)

    Happy to run more diagnostics.

  4. djbclark commented on Oct 17, 2026

    @djbclark

    Same failure on the official CLI, not a source build: zcode-app-cli 3.15.3-36 / runtime 0.16.9 on macOS 27 (arm64), Z.ai Coding Plan, account login.

    It recurs about once a day. zcode login fixes it immediately, then roughly 20-24 h later every turn fails with project_token_login_failed (RuntimeHeadersRefreshError -> CodingPlanApiKeyError, provider account:zai-individual-coding-plan, errorPhase prepare, retryable false). First failure each day from our logs: 2026-10-14 15:06Z, 2026-10-15 12:35Z, 2026-10-16 08:42Z; 248 failed turns over the three days. Nothing changed on our side between the working and failing periods, and the 3.15.3 release note about login failures did not change this.

    So the business-token exchange looks like it is rejected once the stored OAuth token ages past some limit and the refresh path does not recover. A loud "login expired, run zcode login" error instead of a generic turn failure would already help; a working refresh would be the fix.

  5. djbclark commented on Oct 18, 2026

    @djbclark

    Workaround that holds on the official CLI (zcode-app-cli 3.15.3-36, runtime 0.16.9, macOS arm64, no desktop app installed), confirming @dooee's diagnosis:

    The account provider (account:zai-individual-coding-plan) has two auth modes, chosen by the credential account-provider:account:zai-individual-coding-plan:auth-source in ~/.zcode/v2/credentials.json:

    1. oauth: what zcode login always writes. Every turn exchanges the OAuth token at /api/auth/z/login, and that is what starts failing about a day after each login.
    2. manual: what the desktop app writes. Requests use the Coding Plan API key stored under account-provider:coding-plan:account:zai-individual-coding-plan:account:<user_id>:api-key, and the OAuth token is never used.

    So: run zcode login once (that creates the identity entry), create a Coding Plan API key at https://z.ai/manage-apikey/apikey-list, then set auth-source to manual and put the key in that api-key entry. The values are enc:v1:<iv>.<tag>.<ciphertext> (base64url, AES-256-GCM, key = sha256 of ZCODE_CREDENTIAL_SECRET, or of zcode-credential-fallback:<platform>:<homedir>:<username>; see createZCodeCredentialCipher in the bundle).

    Verified by running a turn with a HOME whose credentials.json held only the three account-provider:* entries, with no oauth:* and no zcodejwttoken: the turn completed. Don't run zcode login afterwards; it writes oauth back.

    The script we use: https://github.com/djbclark/djbclark-ade/blob/master/bin/zcode-key-auth

    Suggested fix: have zcode login keep a manual setup (or offer zcode login --api-key), and refresh or fail loudly when the business-token exchange is rejected.

    (The separate builtin:zai API-key provider is still not selectable, #1067.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: P2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions