{"record":{"id":"2f6c0b2529718f91","repo":"Significant-Gravitas/AutoGPT","slug":"threshold-must-be-greater-than-0","errorCode":null,"errorMessage":"Threshold must be greater than 0","messagePattern":"Threshold must be greater than 0","errorType":"http","errorClass":"HTTPException","httpStatus":422,"severity":"warning","filePath":"autogpt_platform/backend/backend/api/features/v1.py","lineNumber":751,"sourceCode":"\n@v1_router.post(\n    path=\"/credits/auto-top-up\",\n    summary=\"Configure auto top up\",\n    tags=[\"credits\"],\n    dependencies=[Security(requires_user)],\n)\nasync def configure_user_auto_top_up(\n    request: AutoTopUpConfig,\n    user_id: Annotated[str, Security(get_user_id)],\n    ctx: Annotated[RequestContext, Security(get_request_context)],\n) -> str:\n    \"\"\"Configure auto top-up settings and perform an immediate top-up if needed.\n\n    Raises HTTPException(422) if the request parameters are invalid or if\n    the credit top-up fails.\n    \"\"\"\n    if request.threshold < 0:\n        raise HTTPException(status_code=422, detail=\"Threshold must be greater than 0\")\n    if request.amount < 500 and request.amount != 0:\n        raise HTTPException(\n            status_code=422, detail=\"Amount must be greater than or equal to 500\"\n        )\n    if request.amount != 0 and request.amount < request.threshold:\n        raise HTTPException(\n            status_code=422, detail=\"Amount must be greater than or equal to threshold\"\n        )\n\n    credit_model = await get_credit_model(user_id, ctx.org_id)\n    current_balance = await credit_model.get_credits(user_id)\n\n    try:\n        if current_balance < request.threshold:\n            await credit_model.top_up_credits(user_id, request.amount)\n        else:\n            await credit_model.top_up_credits(user_id, 0)\n    except ValueError as e:","sourceCodeStart":733,"sourceCodeEnd":769,"githubUrl":"https://github.com/Significant-Gravitas/AutoGPT/blob/9c8bb5550f446ba5d3046b78896578742495b3cf/autogpt_platform/backend/backend/api/features/v1.py#L733-L769","documentation":"HTTP 422 from POST /credits/auto-top-up when AutoTopUpConfig.threshold is negative. The route requires threshold >= 0 (0 disables the trigger); a negative threshold would make the top-up condition meaningless, so it is rejected before any credit model call. Note the message says 'greater than 0' but the actual check is < 0, so 0 itself is accepted.","triggerScenarios":"Sending {\"threshold\": -1, \"amount\": 500} (or any negative threshold) in the auto top-up config request body.","commonSituations":"Sentinel values like -1 intended to mean 'always top up'; unit tests passing edge values; a frontend form allowing negative numbers without validation; serializing an unset/None field as -1.","solutions":["Send threshold >= 0; use 0 to trigger a top-up whenever the balance is at or below zero","Add client-side validation to the settings form so the threshold field cannot be negative","If you intended 'always top up', set threshold to 0 and amount to your desired top-up size"],"exampleFix":"// before\n{ threshold: -1, amount: 500 }\n\n// after\n{ threshold: 0, amount: 500 }","handlingStrategy":"validation","validationCode":"if (threshold < 0) throw new RangeError(\"threshold must be >= 0\");","typeGuard":"function isValidThreshold(t: unknown): t is number {\n  return typeof t === \"number\" && t >= 0;\n}","tryCatchPattern":null,"preventionTips":["Use a numeric input with min=0 in settings forms","Treat 0 as 'always top up', never -1"],"tags":["validation","credits","auto-top-up","http-422"],"backgroundTag":null,"analyzedSha":"9c8bb5550f446ba5d3046b78896578742495b3cf","analyzedAt":"2026-08-14T17:17:21.957Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}