Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(scrape): bill llm on fail

Gergő Móricz0e027fe43014
diff --git a/apps/api/src/controllers/v1/scrape.ts b/apps/api/src/controllers/v1/scrape.ts
index 779e1896b..3567809db 100644
--- a/apps/api/src/controllers/v1/scrape.ts
+++ b/apps/api/src/controllers/v1/scrape.ts
@@ -74,6 +74,35 @@ export async function scrapeController(
scrapeId: jobId,
startTime,
});
+
+ let creditsToBeBilled = 0;
+
+ if (req.body.agent?.model?.toLowerCase() === "fire-1" || req.body.extract?.agent?.model?.toLowerCase() === "fire-1" || req.body.jsonOptions?.agent?.model?.toLowerCase() === "fire-1") {
+ if (process.env.USE_DB_AUTHENTICATION === "true") {
+ // @Nick this is a hack pushed at 2AM pls help - mogery
+ const job = await supabaseGetJobById(jobId);
+ if (!job?.cost_tracking) {
+ logger.warn("No cost tracking found for job", {
+ jobId,
+ });
+ }
+ creditsToBeBilled = Math.ceil((job?.cost_tracking?.totalCost ?? 1) * 1800);
+ } else {
+ creditsToBeBilled = 150;
+ }
+ }
+
+ if (creditsToBeBilled > 0) {
+ billTeam(req.auth.team_id, req.acuc?.sub_id, creditsToBeBilled).catch(
+ (error) => {
+ logger.error(
+ `Failed to bill team ${req.auth.team_id} for ${creditsToBeBilled} credits: ${error}`,
+ );
+ // Optionally, you could notify an admin or add to a retry queue here
+ },
+ );
+ }
+
if (
e instanceof Error &&
(e.message.startsWith("Job wait") || e.message === "timeout")