Skip to content

fix(agents): use neutral billing copy for subscription auth - #93763

Merged
vincentkoc merged 3 commits into
openclaw:mainfrom
eldar702:fix/80877-billing-oauth-copy
Jun 16, 2026
Merged

vincentkoc merged 3 commits into
openclaw:mainfrom
eldar702:fix/80877-billing-oauth-copy

Conversation

@eldar702

Copy link
Copy Markdown
Contributor

What and why

Anthropic Max (and other OAuth/token-subscription users) hit a billing limit and see:

⚠️ Anthropic (claude-sonnet-4-5) returned a billing error — your API key has run out of
credits or has an insufficient balance. Check your Anthropic billing dashboard and top up or
switch to a different API key.

Max users authenticate via OAuth — they have no API key to top up. The copy is misleading and
creates support confusion. The recommended Anthropic onboarding path (/api-key → Max plan) makes
this a common case.

This PR adds an optional authMode parameter to formatBillingErrorMessage and threads it from
credential.type (already computed at the call sites) through formatAssistantErrorText opts.
When authMode is "oauth" or "token", the message becomes:

⚠️ Anthropic (claude-sonnet-4-5) returned a billing error — check your account for subscription
or usage limits, then try again.

API-key and aws-sdk modes are unchanged (regression-guarded by test).

The diff is intentionally minimal to avoid conflicting with the string rewording in the open i18n
PR #90611 — only the new auth-mode branch is added; no existing api-key strings are reworded.

Fixes #80877

Changes

File Change
src/agents/embedded-agent-helpers/sanitize-user-facing-text.ts Add optional authMode? param to formatBillingErrorMessage; emit neutral copy for "oauth" / "token"
src/agents/embedded-agent-helpers/errors.ts Add authMode? to formatAssistantErrorText + formatUserFacingAssistantErrorText opts; thread to all 3 formatBillingErrorMessage call sites
src/agents/embedded-agent-runner/run/assistant-failover.ts Add authMode? to handleAssistantFailover params + resolveAssistantFailoverErrorMessage params; pass to both formatter calls
src/agents/embedded-agent-runner/run/payloads.ts Add authMode? to buildEmbeddedRunPayloads params; pass to both error-text formatters
src/agents/embedded-agent-runner/run.ts Pass attemptAuthProfileStore.profiles?.[lastProfileId]?.type as authMode at both formatter call sites
src/agents/embedded-agent-helpers.formatassistanterrortext.test.ts 5 new tests: oauth neutral copy, token neutral copy, api_key regression guard, undefined regression guard, no-provider regression guard

Real behavior proof

Behavior or issue addressed: formatBillingErrorMessage("Anthropic", "claude-sonnet-4-5", "oauth") must NOT
contain "API key" or "top up" language. API-key mode must still emit the original copy.

Real environment tested: main 3d05da9a, Node 24, pnpm 11.2.2, macOS; real formatter module — not
mocked. Tests import and call the actual formatBillingErrorMessage function from
embedded-agent-helpers.js.

Exact steps or command run after this patch:

node scripts/run-vitest.mjs run src/agents/embedded-agent-helpers.formatassistanterrortext.test.ts

Before evidence (RED — 2 failures):

 FAIL  |unit-fast| src/agents/embedded-agent-helpers.formatassistanterrortext.test.ts
  > formatBillingErrorMessage — authMode neutral copy (#80877)
    > returns neutral copy for oauth authMode — no 'API key' text

AssertionError: expected '⚠️ Anthropic (claude-sonnet-4-5) returned a billing error — your API key
has run out of credits or has an insufficient balance. Check your Anthropic billing dashboard and
top up or switch to a different API key.' not to match /api key/i

 FAIL  |unit-fast| src/agents/embedded-agent-helpers.formatassistanterrortext.test.ts
  > formatBillingErrorMessage — authMode neutral copy (#80877)
    > returns neutral copy for token authMode — no 'API key' text

AssertionError: expected '⚠️ Anthropic (claude-sonnet-4-5) returned a billing error — your API key
has run out of credits...' not to match /api key/i

 Test Files  1 failed (1)
       Tests  2 failed | 84 passed (86)
    Start at  20:45:10
    Duration  1.07s

Evidence after fix (GREEN — all 86 pass):

cd /tmp/oc-wt-80877 && corepack pnpm exec vitest run src/agents/embedded-agent-helpers.formatassistanterrortext.test.ts

 RUN  v4.1.8 /private/tmp/oc-wt-80877

 Test Files  1 passed (1)
      Tests  86 passed (86)
   Start at  20:55:03
   Duration  1.17s (transform 695ms, setup 0ms, import 1.08s, tests 18ms, environment 0ms)

Observed result: OAuth users see neutral copy ("check your account for subscription or usage
limits, then try again") — no "API key" or "top up" language. API-key mode is unchanged (regression
guards pass).

What was not tested: A live Anthropic Max OAuth account hitting a real 402/429 billing limit
is the gold reproduction — that path is not available in CI. The formatter branch (authMode === "oauth") is the testable proof, and the credential threading (run.ts → handleAssistantFailover →
resolveAssistantFailoverErrorMessage / buildEmbeddedRunPayloads) is statically verifiable. The 5
new tests cover the formatter contract exhaustively.


AI-assisted: This fix was drafted with Claude Code (Sonnet 4.6). All code reviewed for
correctness and minimal diff. Allow edits by maintainers: yes.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: M proof: supplied External PR includes structured after-fix real behavior proof. labels Jun 16, 2026
@clawsweeper

clawsweeper Bot commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed June 16, 2026, 7:12 PM ET / 23:12 UTC.

Summary
The PR adds auth-mode-aware billing error copy for OAuth/token credentials and threads authMode through failover, fallback, and reply payload formatting with regression tests.

PR surface: Source +153, Tests +198. Total +351 across 19 files.

Reproducibility: yes. at source level: current main and the latest release format all billing-shaped errors with API-key/top-up copy and lack credential-mode input. I did not run a live Anthropic Max OAuth or Telegram reproduction in this read-only review.

Review metrics: none identified.

Root-cause cluster
Relationship: same_root_cause
Canonical: #80877
Summary: The canonical issue is the Anthropic Max OAuth billing-copy bug; this PR is the current open fix candidate, with older closed-unmerged PRs and an i18n PR partially overlapping the same copy surface.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge readiness
Overall: 🧂 unranked krab
Proof: 🧂 unranked krab
Patch quality: 🦐 gold shrimp
Result: blocked until real behavior proof from a real setup is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P1] Add a focused fix and regression coverage for lifecycle terminal error formatting with OAuth/token auth mode.
  • [P1] Add real behavior proof from an actual OpenClaw runtime path, such as a redacted terminal run or gateway/channel transcript that shows the neutral billing copy after the patch.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR body supplies before/after Vitest output against the real formatter module, but unit tests are supplemental and do not show an actual OpenClaw runtime, gateway, CLI, or channel path after the patch. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] Lifecycle terminal error events can still contain API-key/top-up billing copy for OAuth/token runs and those events are rendered by gateway chat/TUI surfaces.
  • [P1] The supplied proof is only failing/passing Vitest output against formatter tests; it does not show an actual OpenClaw run, gateway, CLI, or channel path after the patch.

Maintainer options:

  1. Decide the mitigation before merge
    Carry the selected auth profile mode through every user-facing billing-error surface, including lifecycle terminal events, while preserving API-key copy for API-key callers and adding non-Vitest proof from an actual OpenClaw runtime path.
  2. Pause or close
    Do not merge this PR until maintainers decide whether the risk is worth taking.

Next step before merge

  • [P1] Human follow-up is needed because the branch has a real review finding and the missing real behavior proof must come from the contributor or maintainer environment, not an automated repair marker.

Security
Cleared: No concrete security or supply-chain concern was found; the diff carries auth-mode metadata through TypeScript runtime/test paths without dependency, CI, secret, install, or code-loading changes.

Review findings

  • [P2] Carry auth mode into lifecycle errors — src/agents/embedded-agent-runner/run.ts:3263-3265
Review details

Best possible solution:

Carry the selected auth profile mode through every user-facing billing-error surface, including lifecycle terminal events, while preserving API-key copy for API-key callers and adding non-Vitest proof from an actual OpenClaw runtime path.

Do we have a high-confidence way to reproduce the issue?

Yes at source level: current main and the latest release format all billing-shaped errors with API-key/top-up copy and lack credential-mode input. I did not run a live Anthropic Max OAuth or Telegram reproduction in this read-only review.

Is this the best way to solve the issue?

No, not yet: passing the active credential type into the central formatter is the right fix shape, but the PR must also cover lifecycle terminal error events and provide real behavior proof outside Vitest before merge.

Full review comments:

  • [P2] Carry auth mode into lifecycle errors — src/agents/embedded-agent-runner/run.ts:3263-3265
    This threads authMode into the final payload path, but the subscription lifecycle path still formats lastAssistant without auth mode and emits that text as lifecycle error data. Gateway chat and TUI render lifecycle errors, so an OAuth/token billing-shaped assistant error can still surface API-key/top-up copy outside the payload path; pass the active profile mode into subscription/lifecycle formatting or otherwise use the neutral copy there too.
    Confidence: 0.86

Overall correctness: patch is incorrect
Overall confidence: 0.82

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 8db66b416bb8.

Label changes

Label changes:

  • add P2: This is a normal-priority user-facing auth/provider bug fix with limited blast radius, but the PR still has a concrete remaining surface gap.
  • add rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body supplies before/after Vitest output against the real formatter module, but unit tests are supplemental and do not show an actual OpenClaw runtime, gateway, CLI, or channel path after the patch. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Label justifications:

  • P2: This is a normal-priority user-facing auth/provider bug fix with limited blast radius, but the PR still has a concrete remaining surface gap.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦐 gold shrimp.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body supplies before/after Vitest output against the real formatter module, but unit tests are supplemental and do not show an actual OpenClaw runtime, gateway, CLI, or channel path after the patch. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +153, Tests +198. Total +351 across 19 files.

View PR surface stats
Area Files Added Removed Net
Source 13 165 12 +153
Tests 6 208 10 +198
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 19 373 22 +351

What I checked:

Likely related people:

  • vincentkoc: Current blame in this checkout for the billing helper/error formatter points at recent refactor commit ae99ce7, and this PR also has two follow-up commits plus assignment/review-request activity from this login. (role: recent area contributor and PR branch follow-up owner; confidence: high; commits: ae99ce729a66, 0767584ba786, 1daff3f514b1; files: src/agents/embedded-agent-helpers/sanitize-user-facing-text.ts, src/agents/embedded-agent-helpers/errors.ts, src/agents/embedded-agent-runner/run.ts)
  • steipete: Prior ClawSweeper review history for the same bug connected this login to assistant error formatting, sanitizer, and assistant-failover files that share the invariant this PR changes. (role: recent formatter/failover area contributor; confidence: medium; commits: 0eb50f9f8d00, 9180173f9a79, 936c02e22c98; files: src/agents/embedded-agent-helpers/sanitize-user-facing-text.ts, src/agents/embedded-agent-helpers/errors.ts, src/agents/embedded-agent-runner/run/assistant-failover.ts)
  • shakkernerd: Prior related review history tied recent auth profile rotation work to the assistant failover path that needs credential-mode-aware billing copy. (role: adjacent auth/failover contributor; confidence: medium; commits: efa8c83200ab; files: src/agents/embedded-agent-runner/run/assistant-failover.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@vincentkoc vincentkoc self-assigned this Jun 16, 2026
@vincentkoc
vincentkoc requested a review from a team as a code owner June 16, 2026 21:41
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. labels Jun 16, 2026
@vincentkoc

Copy link
Copy Markdown
Member

Maintainer review and repair complete. Subscription/OAuth auth mode now survives profile cooldown, prompt and assistant failover, structured failover coercion, model fallback summaries, and final user-facing delivery, so billing failures no longer tell subscription users to top up an API key.

Verification:

  • node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/auth-controller.test.ts src/agents/failover-error.test.ts src/agents/model-fallback.test.ts src/agents/embedded-agent-runner/run/assistant-failover.test.ts src/agents/embedded-agent-helpers.formatassistanterrortext.test.ts src/auto-reply/reply/agent-runner-execution.test.ts — 495 tests passed
  • node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run.overflow-compaction.test.ts via Crabbox Blacksmith Testbox — 46 tests passed
  • Current-head GitHub checks-node-agentic-agents-embedded shard — passed
  • oxfmt --check on touched files and git diff --check — passed
  • Fresh maintainer autoreview after the runtime repair and final test-fixture correction — clean, no accepted/actionable findings
  • pnpm check:changed via Crabbox Blacksmith Testbox — passed
    • provider: blacksmith-testbox
    • id: tbx_01kv9d67y9pvvghjk5vyrekxhm
    • slug: blue-shrimp
    • duration: 7m42s

Known proof gap: the linked local worktree could not execute the overflow-compaction file because its shared install lacks @openclaw/llm-core; the exact file passed on Testbox and in the current-head GitHub shard.

@vincentkoc vincentkoc changed the title fix(agents): neutral billing-error copy for OAuth/subscription auth instead of "top up API key" [AI-assisted] fix(agents): use neutral billing copy for subscription auth Jun 16, 2026
@vincentkoc
vincentkoc merged commit 7c97c6d into openclaw:main Jun 16, 2026
192 of 196 checks passed
@eldar702

Copy link
Copy Markdown
Contributor Author

Thanks for the review and merge, @vincentkoc! 🙏

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jun 17, 2026
…#93763)

* fix(agents): neutral billing-error copy for OAuth/subscription auth

Fixes openclaw#80877

AI-assisted (Claude Code).

* fix(agents): preserve subscription billing guidance

* test(agents): use active auth store in prompt failover

---------

Co-authored-by: Vincent Koc <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling P2 Normal backlog priority with limited blast radius. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: M status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

formatBillingErrorMessage misfires for Anthropic Max OAuth — broadcasts "top up your API key" when there is no API key

2 participants