Skip to content

cloudflare-workers-ai 0.0.6: a plan option counts the free allowance for routing - #1079

Open
duchanghexuhe wants to merge 1 commit into
magpie-community:mainfrom
duchanghexuhe:cloudflare-plan-option
Open

duchanghexuhe wants to merge 1 commit into
magpie-community:mainfrom
duchanghexuhe:cloudflare-plan-option

Conversation

@duchanghexuhe

Copy link
Copy Markdown

Problem

Cloudflare's GET /client/v4/accounts/{account_id} response for a Workers AI token carries no plan field (verified against a real account: it returns only id, name, type, settings, legacy_flags, created_on). So info?.result?.plan is always empty, plan stays at the fallback text, planIsFree stays false, and the daily Neurons window is always emitted with aside: true.

magpie's routing treats an aside window as informational, so such a key's remaining allowance is permanently unknown to routing: it is sorted behind every account whose allowance is known, with "remaining allowance unknown" on the Routing page. This happens even when usage itself reads perfectly — GraphQL Analytics returns today's totalNeurons fine; it is the plan that can't be confirmed.

Change

New plugin option plan: free or paid.

  • free → plan is "Workers Free", planIsFree = true, the Neurons window is not aside, so routing counts the 10,000/day allowance and stops the key when it is spent.
  • paid → plan is "Workers Paid", the window stays aside (past the free allowance a Paid account is billed, not refused).
  • Unset → exactly the current behaviour (best-effort account answer, otherwise unconfirmed).

The declaration wins over the best-effort account answer, so it works for tokens that are told no plan.

magpie plugin options opencode-cloudflare-workers-ai-auth '{"accountId": "…", "plan": "free"}'

Also:

  • README (en / zh-CN / ja) documents the option and updates the "quota row shows the fallback plan" troubleshooting row.
  • Three tests: a declared Free plan counts when the account names no plan; a declared Paid plan stays informational; the declaration wins over a conflicting account answer. All 26 tests pass with node --test.

Why an option rather than detection

The account object has no plan to read, and GET /accounts/{id}/subscriptions (the other place a Workers plan shows) needs Account Billing → Read — a Workers AI token gets 403 Authentication error there. Asking the user, who knows their plan, is the only reliable confirmation; magpie's side already requires a confirmed Free plan before a free allowance may bench an account, which is what this option supplies.

No behaviour changes when the option is unset; existing tests are untouched and still pass.

@yetone

yetone commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Thanks @duchanghexuhe. The diagnosis is right: when Cloudflare's account answer has no plan, the Neurons window stays aside forever, and routing calls the key "remaining allowance unknown". I ran it at a7cf7c6 on Linux: node --test 26 pass, bun scripts/check.mjs cloudflare-workers-ai ok.

The problem is where the switch lives. plan is read from the plugin's load-time options, and a magpie user has no place to set that. magpie only hands a plugin the on/off switches it declares in auth.options (settings.json pluginOptions, booleans). It never passes free-form option values to a provider plugin. So the README's "set plan … in the plugin options" points users at something they can't reach, and the change only works for someone loading the plugin by hand in OpenCode. It is also one value for every account, while two Cloudflare accounts can be on different plans.

Could you ask for it at sign-in instead, next to Account ID, and keep it per account?

  • add a select prompt with the key plan, options "Let Cloudflare say" (the default, empty), "Workers Free" (free) and "Workers Paid" (paid). magpie renders select prompts in its sign-in sheet;
  • in authorize, save it in metadata the way accountId is (metadata.plan);
  • in the usage read, use auth.metadata.plan first, then the plugin option if you want to keep it for OpenCode, then Cloudflare's answer;
  • test that two accounts, one free and one paid, get aside: false and aside: true respectively.

An account signed in before this has no metadata.plan, so it keeps today's behaviour until it is signed in again. Say that in the README's troubleshooting row.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants