Playwright + AI Agent Hybrid Architecture (Part 1): Where's the Line Between Scripts and AI

Navigation, auth, repeated extraction should be scripts. Structural adaptation, exception handling, rare parameter combinations need AI. Getting the boundary wrong doubles your cost.

16Yun Engineering TeamApr 14, 20262 min read

A Wrong Default Assumption

Many teams starting an AI browser automation project default to: let the AI agent handle everything.

This is an expensive assumption. Every LLM call processing a page's DOM (3000-5000 tokens) costs ~$0.0045-$0.0075. If an agent needs 10 steps to complete a task: $0.045-$0.075 per task. At 10,000 tasks/day: $450-$750/day. Over $10,000/month.

Compare to deterministic scripts: write once, execute at zero marginal cost (ignoring infrastructure).

But scripts fail when page structure changes — and that's where AI provides real value.

The correct boundary: stable paths execute as scripts. Unstable paths use AI.

Boundary 1: What Should Be Scripts

No decision space. No AI needed.

# Bad: AI navigation: token cost every time
# "Open example.16yun.cn's login page"
# → AI inference → page.goto → ~500 tokens consumed
 
# Good: Script navigation: zero inference cost
page = await browser.new_page()
await page.goto("https://example.16yun.cn/login")

Regular Authentication

Fixed form, fixed credentials. Scripts are 3-5x faster than AI.

# Good: Deterministic login
await page.fill("#username", "user@example.16yun.cn")
await page.fill("#password", "mypassword")
await page.click("#login-button")
await page.wait_for_selector("#dashboard")

If CAPTCHA or changing form structure is involved, AI should step in.

Repeated Data Extraction

If CSS selectors are stable, use scripts.

Performance Comparison

OperationScript TimeAI TimeAI Cost (approx)
page.goto0.5-2s2-5s$0.003-0.007
Form fill (3 fields)0.3s5-15s$0.01-0.03
Extract (10 fields)0.1s3-10s$0.005-0.02
CAPTCHA handlingN/ADepends on modelUnpredictable

Boundary 2: What Should Be AI

Structural Adaptation

Site redesign broke all CSS selectors. Script returns empty data. AI understands the new layout and finds target info.

Exception Handling

Script fails at step 5 — "element not found." An unexpected privacy dialog might have appeared. AI recognizes the dialog and dismisses it.

try:
    await page.click("#checkout-button")
except Exception as e:
    task = f"""
    Click checkout button failed: {str(e)}
    Check page state. Handle any dialogs or warnings.
    """
    await agent.run(task)

Rare Parameter Combinations

A form with 20 fields, different combinations each time. AI interprets natural language goals directly.

Boundary 3: The Switching Mechanism

The Baseline Pattern

class HybridAutomator:
    async def extract_price(self, url):
        try:
            return await self.script_engine.extract_price(url)
        except SelectorNotFoundError:
            price = await self.ai_agent.extract_price(url)
            selector = await self.ai_agent.discover_selector(url, "price")
            self.selector_cache[url] = selector
            return price

When to Switch

  • Failure rate < 5%: keep scripts
  • Page changes weekly: consider permanent AI
  • AI cost < script maintenance cost: switch
  • Irreversible operations: AI for pre-execution confirmation

Summary

AI handles uncertainty and exceptions. Scripts handle precision, speed, low cost for deterministic operations.

Getting the boundary wrong doubles your cost — whether in runtime (all AI) or maintenance (all scripts).

Need an enterprise proxy plan?

We can tailor architecture to your target domains, concurrency, and reliability goals.