Skip to content

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

Merge main into firebill-per-org-routing

Rakshith Ramprakash75316e2d9271
diff --cc apps/api/src/controllers/v2/browser.ts
index 2e0f9c3f2,31985d76f..794ef8a15
--- a/apps/api/src/controllers/v2/browser.ts
+++ b/apps/api/src/controllers/v2/browser.ts
@@@ -604,27 -656,24 +656,30 @@@ export async function browserDeleteCont
clearBrowserSessionPromptFlag(session.id).catch(() => {});
- updateBrowserSessionCreditsUsed(session.id, creditsBilled).catch(error => {
- logger.error("Failed to update credits_used on browser session", {
- error,
- sessionId: session.id,
- creditsBilled,
- });
- });
-
- billTeam(req.auth.team_id, creditsBilled, req.acuc?.api_key_id ?? null, {
- endpoint: usedPrompt ? "interact" : "browser",
- jobId: session.id,
- // Per-path suffix: the webhook teardown below bills the same session id
- // through a different path; a shared key would silently drop one charge.
- chargeId: `${session.id}:destroy`,
- }).catch(error => {
- logger.error("Failed to bill team for browser session", {
- error,
- creditsBilled,
- durationMs,
+ await updateBrowserSessionCreditsUsed(session.id, creditsBilled);
+
+ if (session.should_bill) {
+ const agentRequestId =
+ session.request_id && session.request_id !== session.id
+ ? session.request_id
+ : null;
+ billTeam(req.auth.team_id, creditsBilled, req.acuc?.api_key_id ?? null, {
+ endpoint: agentRequestId ? "agent" : usedPrompt ? "interact" : "browser",
+ jobId: agentRequestId ?? session.id,
++ // Keyed on the session rather than on jobId, deliberately: one agent
++ // request can drive several sessions, and each is its own charge — a key
++ // built from the shared agent id would collapse them into one. The
++ // per-path suffix guards the other direction: the webhook teardown below
++ // bills the same session through a different path.
++ chargeId: `${session.id}:destroy`,
+ }).catch(error => {
+ logger.error("Failed to bill team for browser session", {
+ error,
+ creditsBilled,
+ durationMs,
+ });
});
- });
+ }
logger.info("Browser session destroyed", {
sessionDurationMs: durationMs,
@@@ -745,35 -808,26 +814,30 @@@ export async function browserWebhookDes
clearBrowserSessionPromptFlag(session.id).catch(() => {});
- updateBrowserSessionCreditsUsed(session.id, creditsBilled).catch(error => {
- logger.error(
- "Failed to update credits_used on browser session via webhook",
- {
+ await updateBrowserSessionCreditsUsed(session.id, creditsBilled);
+
+ if (session.should_bill) {
+ const agentRequestId =
+ session.request_id && session.request_id !== session.id
+ ? session.request_id
+ : null;
+ billTeam(session.team_id, creditsBilled, null, {
+ endpoint: agentRequestId ? "agent" : usedPrompt ? "interact" : "browser",
+ jobId: agentRequestId ?? session.id,
++ // Same reasoning as the destroy path above: keyed on the session, not on
++ // jobId, and suffixed per path so the two teardown routes cannot dedupe
++ // each other's charge away.
++ chargeId: `${session.id}:webhook`,
+ }).catch(error => {
+ logger.error("Failed to bill team for browser session via webhook", {
error,
+ teamId: session.team_id,
sessionId: session.id,
creditsBilled,
- },
- );
- });
-
- billTeam(
- session.team_id,
- creditsBilled,
- null, // api_key_id not available in webhook context
- {
- endpoint: usedPrompt ? "interact" : "browser",
- jobId: session.id,
- chargeId: `${session.id}:webhook`,
- },
- ).catch(error => {
- logger.error("Failed to bill team for browser session via webhook", {
- error,
- teamId: session.team_id,
- sessionId: session.id,
- creditsBilled,
- durationMs,
+ durationMs,
+ });
});
- });
+ }
logger.info("Session marked as destroyed via webhook", {
sessionId: session.id,