text
stringlengths
0
1.47k
import httpx
CDP_URL = os.environ.get("HR_BROWSER_CDP_URL", "").strip() # the operator's own browser, instead of the vendor
API_KEY = os.environ.get("BROWSER_USE_API_KEY", "")
# The key as a mounted secret file, read at every use: a rotated Key Vault secret reaches a
# mounted file without a new revision, where an environment variable holds the value the process
# started with.
API_KEY_FILE = os.environ.get("BROWSER_USE_API_KEY_FILE", "")
API_BASE = os.environ.get("BROWSER_USE_API_BASE", "https://api.browser-use.com/api/v4").rstrip("/")
SESSION_CAP_MIN = int(os.environ.get("HR_BROWSER_SESSION_MINUTES", "20")) # per harness session
# Fair share of the vendor's concurrency (10 sessions until $1,000 lifetime spend, then 250):
# no org holds more than its share, and this process stops one below the vendor's ceiling so the
# refusal an agent reads is ours, in a sentence, not the vendor's 429. Per process: a second
# gateway replica has its own counts.
ORG_SESSIONS = int(os.environ.get("HR_BROWSER_ORG_SESSIONS", "3"))
MAX_SESSIONS = int(os.environ.get("HR_BROWSER_MAX_SESSIONS", "9"))
CALL_CAP_S = float(os.environ.get("HR_BROWSER_CALL_SECONDS", "60")) # per tool call
IDLE_S = float(os.environ.get("HR_BROWSER_IDLE_SECONDS", "120")) # no call for this long: stopped
TEXT_CAP = 20000 # characters of extracted text
SNAPSHOT_CAP = 150 # elements in a snapshot
# The vendor's list prices, no markup (browser-use.com/pricing and the v4 API docs, read
# 2026-09-24): browser time $0.02 per hour, rounded up to whole minutes, one-minute minimum;
# residential proxy $5 per GB (this slice runs with no proxy, so the unit never appears);
# screenshots are Playwright's and cost nothing. The meter posts dollars under one unit.
PRICES = {"browser.minute": 0.02 / 60, "browser.proxy_mb": 0.005, "browser.screenshot": 0.0}
UNIT = "browser.usd"
VENDOR = "browser-use"
transport: httpx.BaseTransport | None = None # tests: the vendor's API
connector = None # tests: async (cdp_url) -> a browser-like object
def api_key() -> str:
if API_KEY_FILE:
try:
with open(API_KEY_FILE, encoding="utf-8") as f:
key = f.read().strip()
if key:
return key
except OSError:
pass
return API_KEY
def own_browser() -> bool:
"""Whether the operator's own browser is the browser here."""
return bool(CDP_URL)
def configured() -> bool:
return own_browser() or bool(api_key())
def pricing() -> dict:
"""The browser's price line, for the page that shows it before a workspace turns it on."""
if own_browser():
return {"type": "browser", "unit": "browser hour", "usd_per_unit": 0.0, "markup": 0.0,
"source": "your own browser", "vendor": "own", "billed_as": UNIT,
"rounding": "none", "session_cap_minutes": SESSION_CAP_MIN, "session_estimate_usd": 0.0}
return {"type": "browser", "unit": "browser hour", "usd_per_unit": round(PRICES["browser.minute"] * 60, 6),
"markup": 0.0, "source": "vendor list price", "vendor": VENDOR, "billed_as": UNIT,
"rounding": "up to the minute, one-minute minimum", "session_cap_minutes": SESSION_CAP_MIN,
"session_estimate_usd": session_estimate_usd()}
def session_estimate_usd() -> float:
"""What a session may cost at most, before it starts: the cap in minutes at the list price;
nothing on the operator's own browser."""
return 0.0 if own_browser() else round(SESSION_CAP_MIN * PRICES["browser.minute"], 6)
class BrowserRefused(RuntimeError):
"""A call that must not run, with the sentence the agent reads."""
def __init__(self, code: str, message: str):
super().__init__(message)
self.code = code
class BrowserToolError(RuntimeError):
"""A call that ran and failed in a way the agent can act on."""
# ── the vendor ───────────────────────────────────────────────────────────────────────────────
def _vendor_client() -> httpx.AsyncClient:
return httpx.AsyncClient(transport=transport, timeout=60,
headers={"X-Browser-Use-API-Key": api_key(), "content-type": "application/json"})
async def vendor_create(timeout_min: int, metadata: dict) -> dict:
"""A browser in the vendor's cloud: no proxy, no CAPTCHA solving, no recording, so the only
unit that can bill is time. 402 is the vendor's wallet, 429 its concurrency. The screen is the
vendor's default, landscape: what the live view streams is that screen, whatever the shape of
the card showing it (measured on hr-test, 2026-09-28: neither an emulated viewport over CDP
nor allowResizing changed the streamed frame), so the console fits the frame inside the card.
The operator's own browser is already running: its record names the CDP address and no
vendor session (an empty id is what marks it), and there is no live view of it."""
if own_browser():