fix(errors): a key that hit its credit cap was told the account is out of credits and to buy a top-up - #24
Merged
Conversation
…t of credits and to buy a top-up AUTH014 (Fetch, Extract, Browser) and Batch api_key_cap_reached now map to KEY_CREDIT_CAP_REACHED: other keys still work, wait for the reset or raise the cap. `zenrows usage` prints the calling key's caps. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lr2QdqhUNrX6q4DRLGeTMY
…, so report it as failed with its reason status, wait and create --wait now print latest_run.failure_reason and failure_detail (human and --json) and exit 1 with ok:false when the run ended failed. api_key_cap_reached reuses the KEY_CREDIT_CAP_REACHED guidance; other failures map to BATCH_FAILED. stopped/deleted stay exit 0 but print as a warning. Also moves zrErrorProblemDetail out from between zrErrorDetail and its JSDoc, and teaches trace-debug and batch-jobs the cap, out-of-credits and failed-run codes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lr2QdqhUNrX6q4DRLGeTMY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Zenrows is adding per-API-key credit caps. A request from a key that reached its cap gets a 402:
AUTH014from Fetch, Extract and Browser, andapi_key_cap_reachedfrom the Batch API. The CLI treated every 402 as "account out of credits" and told the user to buy a credit pack or upgrade. The account still has credits, so that advice is wrong.A Batch run the cap stops part-way ends
failedwithfailure_reason: api_key_cap_reached. The CLI printed that with a green check, exited 0, saidok: true, and dropped the reason.What
KEY_CREDIT_CAP_REACHEDfor those 402s inhttp.ts(scrape/extract),batch-api.tsandbrowser-api.ts. It passes the API'sdetailthrough, which names the cap and its reset date. The next action is to wait for the reset or raise/remove the cap at https://app.zenrows.com/settings/api-keys. Other 402s are unchanged.zenrows usageprints one line per cap on the calling key, for exampleThis key: daily cap 200, 200 used, 0 left, resets 2026-10-03T00:00:00Z. It prints nothing for an uncapped key.--jsonalready passesapi_keythrough.batch status,batch waitandbatch create --wait(alsocancel/retry-failed, which share the printer):failure_reasonandfailure_detail, and include them in--json;failedexits 1 withok: falseand anerror.api_key_cap_reachedgets theKEY_CREDIT_CAP_REACHEDguidance (cause names the job, not a 402, since the status call itself succeeded). Any other reason getsBATCH_FAILEDwith the reason in the cause andresults --status failed/retry-failedas next steps;completedstays exit 0 /ok: true, even with failed tasks: per-task failures are results, not a failed run (unchanged);stoppedanddeletedstay exit 0 /ok: truebut print as a warning, not a check. They come from someone cancelling or removing the run, andbatch cancelreturning non-zero on success would be wrong;createrecordsstatus: "error"and the error for a failed run.JobRuntypesfailure_reason/failure_detail.zrErrorProblemDetailmoved out from betweenzrErrorDetailand its JSDoc.trace-debugmapsKEY_CREDIT_CAP_REACHED,POLICY_MAX_CREDITS_EXCEEDED(out of credits) andBATCH_FAILEDto an action;batch-jobsdocuments the terminal states, the exit codes and what to do onapi_key_cap_reached.Verification
npm run typecheck: clean.npm test: 206/206.batch.tschange (4 of them, 201/205), so they catch a regression. The 402 tests from the first commit fail with cap detection disabled.Against prod (
api.zenrows.com,async.api.zenrows.com), built from this branch, isolatedHOME:fetch https://example.com(human,--json)KEY_CREDIT_CAP_REACHED, cause carries the API's AUTH014 detail with reset date,ok:falseextract https://example.comKEY_CREDIT_CAP_REACHEDbrowser open https://example.com(human,--json)KEY_CREDIT_CAP_REACHEDonPOST /browser/sessionsfetch/extract --autoparse/browser run(1 step)usage(human,--json)api_key.caps: []usageThis key: daily cap 200, 200 used, 0 left, resets 2026-10-03T00:00:00Zusage--jsonshows bothbatch create one.jsonl(human,--json)KEY_CREDIT_CAP_REACHEDonPOST /jobsbatch status/batch waiton a run the weekly cap stopped (human,--json)✗ status: failed, 45/80 completed,failure_reason: api_key_cap_reached,failure_detailprinted,ok:false,error.code: KEY_CREDIT_CAP_REACHEDbatch create one.jsonl --wait, then--json batch status✓ status: completed, 1/1,ok:trueNot changed here, seen while testing:
extracton a domain Extract has not prepared (REQS007, HTTP 403) surfaces asFETCH_FAILEDand suggests--manual --js-render --premium-proxy, the most expensive retry, which cannot help. Worth its own fix.🤖 Generated with Claude Code
https://claude.ai/code/session_01Lr2QdqhUNrX6q4DRLGeTMY