Problem → Solution7 min read2026-11-23

Why Browserbase Contexts and Saved Cookies Still Hit 2FA at 2 AM (And How to Auto-Recover)

Learn why persistent Chromium Contexts, OS fingerprint shifts, residential proxy rotation, and Authenticated Fetch token expiry still trigger 2FA—and add unattended self-healing recovery in Stagehand.

By Sentry Relay Engineering · Browser Automation Architecture
BrowserbaseStagehandContextsHeadless Browsers

The Cookie Persistence Trap: Why Contexts Don’t Eliminate 2FA

Teams building autonomous agents on Browserbase, Steel, Kernel, or Playwright Profiles routinely start with the same plan: log into the portal once manually via Session Live View, save the Chromium user data directory with Browserbase Contexts (persist: true), and assume 2FA is solved forever.

Within weeks of running unattended nightly cron jobs, teams hit four documented production failure modes:

  • Server-Side Forced Logouts & Concurrency Collisions: As Browserbase’s own Contexts documentation notes, Contexts do not expire—but target sites still force logouts. Running two concurrent sessions against the same Context causes websites to detect simultaneous logins and revoke the session immediately.
  • Read-Only Authenticated Fetch Token Drift: Browserbase Authenticated Fetch is read-only: cookies or rolling JWTs rotated by the portal during a request are not written back to the Context. Once the short-lived token expires (often every 24h or 7 days), the saved Context drops straight back to the login + 2FA screen.
  • OS & Viewport Fingerprint Mismatches: Developers record initial Context cookies on local macOS/Windows laptops, then deploy to cloud Linux containers (or switch between Verified Windows and non-Verified Linux Browserbase runners), instantly triggering "Unrecognized Device" risk challenges.
  • Residential Proxy ASN Shifts: Rotating residential proxies to prevent blocks changes the originating ISP subnet between runs. Banking, insurance, and ERP portals treat a new ASN as an unauthorized device and mandate an Email or SMS 6-digit code.

Why Falling Back to Session Live View Breaks Unattended Economics

When a Context expires at 2:00 AM, alerting a human operator over Slack with a Session Live View link (or pausing on a mobile device approval prompt) destroys batch automation SLAs and inflates labor costs.

Instead of tearing out Browserbase Contexts, keep Contexts as your L1 fast-path—and wire Sentry Relay in as your automated L2 self-healing recovery layer whenever a portal redirects to /login or prompts for 2FA.

Copy-Paste Pattern: Self-Healing Browserbase Context + Stagehand Recovery

Below is the complete production pattern combining Browserbase Context persistence with Sentry Relay unattended Email/SMS/TOTP recovery:

typescript
import { Stagehand } from '@browserbasehq/stagehand';
import { SentryRelay } from '@sentryrelay/sdk';

const relay = new SentryRelay({ apiKey: process.env.SENTRY_RELAY_API_KEY! });

export async function runWithSelfHealingContext(contextId: string) {
  const stagehand = new Stagehand({
    env: 'BROWSERBASE',
    browserbaseSessionCreateParams: {
      browserSettings: {
        context: { id: contextId, persist: true },
      },
    },
  });

  await stagehand.init();
  const page = stagehand.page;

  await page.goto('https://portal.vendor-erp.com/invoices');

  // Detect whether saved Context cookies were invalidated or flagged for new-IP 2FA
  const needsAuth = /login|signin|verify/i.test(page.url());
  if (needsAuth) {
    await page.act({ action: 'Submit corporate email and password to sign in' });

    // Intercept Email OTP, Carrier SMS, or Magic Link in <200ms without human Live View
    const auth = await relay.wait('vendor-erp-bot', {
      sender: 'vendor-erp.com',
      timeout: 35,
    });

    if (auth.event?.otpCode) {
      await page.act({ action: `Type verification code ${auth.event.otpCode} and submit` });
    } else if (auth.event?.magicLink) {
      await page.goto(auth.event.magicLink);
    }
  }

  // Context persists refreshed session cookies automatically when the session closes
  await page.act({ action: 'Download this month invoice PDF' });
  await stagehand.close();
}

Frequently Asked Questions

Should we stop using Browserbase Contexts if we adopt Sentry Relay?
No! Keep using Browserbase Contexts (persist: true) so warm sessions log in immediately. Sentry Relay acts as the automated safety net whenever cookies expire or a portal flags an unrecognized proxy IP.
What if the portal only sends 2FA via SMS to a US phone number?
Paid Sentry Relay tiers provision dedicated US carrier Telnyx DIDs (not shared VoIP numbers) per Identity, so SMS challenges resolve inside the exact same relay.wait() call.
Does this work with Steel, Kernel, and Playwright-Python too?
Yes. Any persistent browser profile (Steel sessions, Kernel profiles, or Playwright storageState) pairs cleanly with @sentryrelay/sdk (TypeScript) or sentryrelay-sdk (Python).
Test Unattended 2FA in 3 Minutes with Simulated SMS

Start on the free Sandbox tier ($0/mo, 250 email extractions, Cloud KMS TOTP, Simulated SMS tester) or attach dedicated US carrier Telnyx DIDs on paid plans.