OpenAI Agents SDK + Anchor: Browser Agents in Python

Hands On
Jun 23
by Idan Raman
OpenAI Agents SDK + Anchor: Browser Agents in Python

The OpenAI Agents SDK is a lightweight, production-ready framework for multi-agent workflows in Python. Its @function_tool decorator, built-in tracing, and clean Runner API make it a natural fit for teams who want structured agentic behavior without a heavyweight orchestration framework.

Pair it with Anchor's cloud browser infrastructure—instant sessions, real IP fingerprints, built-in CAPTCHA solving, and a Playwright-compatible CDP endpoint—and you get a browser agent that navigates the live web with zero browser management on your side.

This post walks through building a research agent that:

  • Spins up an Anchor browser session on demand
  • Navigates to URLs and extracts structured information
  • Falls back to Google search when a page lacks detail

What We're Building

A web research assistant powered by the OpenAI Agents SDK that uses Anchor's cloud browser as its eyes. Give it a URL and a question; it browses the page and returns a structured answer.

Setup

pip install openai-agents anchor-browser playwright
playwright install chromium
export ANCHOR_API_KEY="your_anchor_api_key"
export OPENAI_API_KEY="your_openai_api_key"

Imports and Client Initialization

import os
from agents import Agent, function_tool, Runner
from anchor_browser import AnchorClient
from playwright.sync_api import sync_playwright

anchor = AnchorClient(api_key=os.environ["ANCHOR_API_KEY"])

Define Browser Tools

The @function_tool decorator converts plain Python functions into tools the agent can invoke. The SDK reads type hints and the docstring to auto-generate the JSON schema—no manual schema definitions needed.

@function_tool
def browse_and_extract(url: str, focus: str = "") -> str:
    """Navigate to a URL and return the page title and body text.

    Args:
        url: The fully-qualified URL to visit.
        focus: Optional hint about what information to extract.
    """
    session = anchor.sessions.create(
        proxy_country="us",
        options={"adblock": True},
    )
    pw = sync_playwright().start()
    browser = pw.chromium.connect_over_cdp(session.ws_endpoint)
    page = browser.contexts[0].pages[0]

    page.goto(url, wait_until="domcontentloaded")
    title = page.title()
    body  = page.locator("body").inner_text()[:6000]

    browser.close()
    pw.stop()
    anchor.sessions.stop(session.id)

    hint = f"

(Focus: {focus})" if focus else ""
    return f"Title: {title}

Content:
{body}{hint}"


@function_tool
def google_search(query: str) -> str:
    """Search Google and return the top result snippets.

    Args:
        query: The search query.
    """
    session = anchor.sessions.create(
        proxy_country="us",
        options={"adblock": True},
    )
    pw = sync_playwright().start()
    browser = pw.chromium.connect_over_cdp(session.ws_endpoint)
    page = browser.contexts[0].pages[0]

    page.goto(
        f"https://www.google.com/search?q={query.replace(' ', '+')}",
        wait_until="domcontentloaded",
    )
    results = page.locator("body").inner_text()[:4000]

    browser.close()
    pw.stop()
    anchor.sessions.stop(session.id)

    return results

Build the Agent

research_agent = Agent(
    name="Web Research Agent",
    model="gpt-4o",
    instructions=(
        "You are a research assistant with access to a real web browser. "
        "When given a URL and a question, navigate to the page, extract the relevant "
        "content, and return a concise, well-structured answer. Always cite the URL you "
        "visited. If the first page lacks enough information, use the search tool to "
        "find better sources."
    ),
    tools=[browse_and_extract, google_search],
)

Run It

if __name__ == "__main__":
    result = Runner.run_sync(
        research_agent,
        "Visit https://anchorbrowser.io/pricing and summarize "
        "the available plans and their key features.",
    )
    print(result.final_output)

Running this gives you a structured summary pulled from a live browser session—no scraping workarounds, no Cloudflare blocks, no headless detection issues.

Why OpenAI Agents SDK + Anchor Works Well Together

  • Zero-boilerplate tools: @function_tool handles JSON schema generation automatically. Write a typed Python function with a docstring and the SDK does the rest.
  • Session isolation: Each tool call creates a fresh Anchor session—different IP, fresh cookies, no cross-contamination between concurrent tasks.
  • Built-in tracing: The Agents SDK ships with OpenAI's tracing dashboard so you can replay every tool call and see exactly what the agent browsed.
  • No infrastructure ops: Anchor manages browser provisioning, Chromium versions, and proxy rotation. You never touch a headless Chrome config.

Production Tips

  • Pool sessions: For high-frequency agents, create sessions ahead of time and reuse them across tool calls rather than spinning up a new session per call.
  • Set timeouts: Pass timeout=30 to @function_tool so a slow page load doesn't block the agent loop indefinitely.
  • Use OmniConnect for authenticated sites: Anchor's OmniConnect lets agents authenticate with OAuth providers using stored credentials—no need to re-implement login flows inside your tools.
  • Geo-target with VPN: Pass proxy_country="gb" (any ISO-3166-1 code) to reach region-restricted content or test localized pages.
  • Parallelize browsing: For tasks that visit multiple URLs, move to async Playwright and asyncio.gather() to run Anchor sessions concurrently.

What's Next

  • Multi-agent handoffs: Use the SDK's handoff mechanism to build a pipeline where a coordinator agent dispatches to specialized sub-agents (news, pricing pages, documentation).
  • Structured outputs: Combine @function_tool with Pydantic return types to get typed, validated data back from every page visit.
  • Human in the loop: The Agents SDK has built-in support for pausing execution and requesting human approval—useful when an agent needs to submit a form on a live site.

Get Started

Try Anchor free and run your first OpenAI Agents SDK browser agent in minutes →

Stay ahead in browser automation

We respect your inbox. Privacy policy

Welcome aboard! Thanks for signing up
Oops! Something went wrong while submitting the form.