{"record":{"id":"ede78343f747e992","repo":"Significant-Gravitas/AutoGPT","slug":"str-e-ede783","errorCode":null,"errorMessage":"str(e)","messagePattern":"str\\(e\\)","errorType":"http","errorClass":"InsufficientBalanceError","httpStatus":402,"severity":"error","filePath":"autogpt_platform/backend/backend/api/external/v1/routes.py","lineNumber":117,"sourceCode":"    # consistent with chat / internal block / internal graph routes.\n    await enforce_payment_paywall(auth.user_id)\n\n    obj = backend.blocks.get_block(block_id)\n    if not obj:\n        raise HTTPException(status_code=404, detail=f\"Block #{block_id} not found.\")\n    if obj.disabled:\n        raise HTTPException(status_code=403, detail=f\"Block #{block_id} is disabled.\")\n\n    user = await user_db.get_user_by_id(auth.user_id)\n    if not user:\n        raise HTTPException(status_code=404, detail=\"User not found.\")\n\n    try:\n        await charge_for_direct_block_execution(\n            user_id=auth.user_id, block=obj, input_data=data, source=\"external\"\n        )\n    except InsufficientBalanceError as e:\n        raise HTTPException(\n            status_code=status.HTTP_402_PAYMENT_REQUIRED, detail=str(e)\n        ) from e\n\n    # Direct block execution has no graph; build a minimal ExecutionContext\n    # carrying the caller's identity + timezone so blocks that depend on\n    # those (e.g. time blocks) get correct data.\n    execution_context = ExecutionContext(\n        user_id=auth.user_id,\n        user_timezone=get_user_timezone_or_utc(user.timezone),\n    )\n\n    output = defaultdict(list)\n    async for name, data in obj.execute(data, execution_context=execution_context):\n        output[name].append(data)\n    return output\n\n\n@v1_router.post(","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/Significant-Gravitas/AutoGPT/blob/9c8bb5550f446ba5d3046b78896578742495b3cf/autogpt_platform/backend/backend/api/external/v1/routes.py#L99-L135","documentation":"This is the HTTP 402 detail produced when `charge_for_direct_block_execution` raises `InsufficientBalanceError`: `str(e)` carries the billing message (typically the required credit amount vs. remaining balance). Direct external block executions are metered upfront; if the user's credit balance can't cover the block's cost, the request fails with Payment Required before execution.","triggerScenarios":"POST `/blocks/{block_id}/execute` when the API key owner's credit balance is lower than the execution cost of the block (LLM blocks, metered API blocks).","commonSituations":"Exhausted free-tier credits on a hosted deployment; long-running automation draining balance mid-run; new expensive model selected on a block raising per-run cost.","solutions":["Top up the user's credit balance in the platform, then retry the execution.","Switch the block/model configuration to a cheaper option to fit the current balance.","Monitor balance via the platform's user/credit endpoints and alert before it hits zero for unattended automations."],"exampleFix":"# before\nPOST /blocks/{llm_block_id}/execute  # 402 {\"detail\": \"Insufficient balance: need 10, have 2\"}\n\n# after: top up credits, then retry\nPOST /blocks/{llm_block_id}/execute  # 200","handlingStrategy":"try-catch","validationCode":"balance = client.get(\"/users/me/credits\").json()[\"balance\"]\nif balance < estimated_block_cost(block_id):\n    raise InsufficientCredits(f\"need ~{estimated_block_cost(block_id)}, have {balance}\")","typeGuard":null,"tryCatchPattern":"try:\n    client.post(f\"/blocks/{block_id}/execute\", json=data)\nexcept HTTPError as e:\n    if e.response.status_code == 402:\n        top_up_credits(min_amount=parse_needed(e.response.text))\n        client.post(f\"/blocks/{block_id}/execute\", json=data)  # retry after top-up\n    else:\n        raise","preventionTips":["Monitor credit balance and set low-balance alerts for unattended automations.","Estimate per-run cost from block pricing and pre-check balance before executing.","Treat HTTP 402 as a retryable-after-topup condition, distinct from 4xx client errors."],"tags":["billing","credits","payment-required","blocks"],"backgroundTag":null,"analyzedSha":"9c8bb5550f446ba5d3046b78896578742495b3cf","analyzedAt":"2026-08-14T17:17:21.957Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}