{"article":{"slug":"how-we-optimized-browser-use-for-deep-agents","title":"How We Optimized Browser Use for Deep Agents","subtitle":null,"summary":"LangChain explains how it made browser use efficient for Managed Deep Agents: context-tuned Stagehand tools, selective observation, and a human handoff path so agents can browse without drowning in page noise.","content_type":"blog_post","language":"en","canonical_url":"https://www.langchain.com/blog/how-we-optimized-browser-use-for-deep-agents","author":{"name":"Sydney Runkle","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"LangChain","url":"https://www.langchain.com/","listing_slug":"langchain","listing":{"slug":"langchain","name":"LangChain","listing_type":"company","url":"https://listedstartups.com/companies/langchain"}},"topics":[{"name":"AI Agents","slug":"ai-agents","url":"https://listedarticles.com/topics/ai-agents"},{"name":"LLMs","slug":"llms","url":"https://listedarticles.com/topics/llms"},{"name":"Engineering","slug":"engineering","url":"https://listedarticles.com/topics/engineering"},{"name":"Developer Tools","slug":"developer-tools","url":"https://listedarticles.com/topics/developer-tools"}],"about_listings":[],"cover_image_url":"https://cdn.prod.website-files.com/65c81e88c254bb0f97633a71/6ae92cbfdaa7db704c3d4a3d_6ae92ca005e2086fc1f08eb4_browser-use-deep-dive-webflow-cover-1200x675.png","license":"all-rights-reserved","word_count":1820,"reading_minutes":8,"published_at":"2026-11-03T00:00:00.000Z","added_at":"2026-11-07T02:18:03.223Z","updated_at":"2026-11-07T02:18:03.223Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/how-we-optimized-browser-use-for-deep-agents","markdown_url":"https://listedarticles.com/articles/how-we-optimized-browser-use-for-deep-agents.md","example":false,"citation":"Sydney Runkle, LangChain. \"How We Optimized Browser Use for Deep Agents.\" 3 Nov 2026. https://www.langchain.com/blog/how-we-optimized-browser-use-for-deep-agents (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://www.langchain.com/blog/how-we-optimized-browser-use-for-deep-agents"},"body_markdown":"## Key Takeaways\n\n- Browser use is an important capability for agents doing autonomous work, letting them act on sites built for people.\n- The tools an agent uses to drive the browser have a huge impact on its cost and trajectory, and Stagehand's are built for agents.\n- Optimizing tools is an iterative process: trace the agent's behavior to find where it's inefficient, update the harness, and test every change with evals before keeping it.\n\nBrowser agents can now take on multi-step work that's painful and time-consuming for people. Personal agents like [Instinct](https://instinct.com/) and Meta's [Muse](https://ai.meta.com/muse/) plan cross-country road trips, cancel unused subscriptions, and list cars for sale, all on sites with no public APIs. [DataDome](https://finance.yahoo.com/technology/ai/articles/datadome-report-agentic-browser-traffic-130000558.html) saw agentic browser requests jump 4.7x in September alone.\n\nBrowser use helps in two places: actions an API can't cover, like using a website the agent wasn't set up for, and end-to-end testing of the web apps coding agents build. Either way, the agent needs two things:\n\n1. **Tools** to drive the browser: open pages, read them, and act on them. Most toolsets control the browser over the[Chrome DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/) (CDP), the protocol that lets tools inspect and control Chrome.\n2. **A browser** to drive, either local or hosted.[Managed Deep Agents](https://docs.langchain.com/langsmith/python/managed-deep-agents) lets you build and deploy agents in LangSmith without running any infrastructure, and it can[run the browser](https://docs.langchain.com/langsmith/python/managed-deep-agents-browser-use) for your agent too (more in our[launch post](https://www.langchain.com/blog/browser-use-in-managed-deep-agents) and[video walkthrough](https://www.youtube.com/watch?v=r1iJzoxFBtg) ). A hosted browser provider like[Browserbase](https://www.browserbase.com) handles the hard parts of the open web for you, like CAPTCHAs, bot blocking, and running many browsers at scale.\n\nThis post focuses on the tools, and on making them good enough that the agent uses the browser well.\n\nWe set out to give agents in [Managed Deep Agents](https://docs.langchain.com/langsmith/python/managed-deep-agents) a browser. We compared seven toolsets and picked the best one, [Stagehand](https://www.stagehand.dev/) ([GitHub](https://github.com/browserbase/stagehand)) from [Browserbase](https://www.browserbase.com). Then we improved it based on what agents did in [LangSmith](https://docs.langchain.com/langsmith/observability) traces, and validated every change with evals. The biggest lesson was how much room there is to improve an agent through tool design.\n\n## Browser use poses context management challenges\n\nBrowser use toolsets come in a few forms: native tools, an [MCP server](https://docs.langchain.com/oss/python/langchain/mcp), or a CLI the agent runs in a shell. Browser use poses two unique challenges for those tools:\n\n- **Tasks are long-running** , with a lot of sequential tool calls. On Playwright MCP, a single WebVoyager task averaged 26 model turns.\n- **Web pages carry a lot of content** , and only a fraction of it is usually relevant to the task. One navigation through Browserbase's hosted MCP server returns a median of about 17,000 characters.\n\nBoth mean browser tools have to manage context efficiently.\n\n## How we tested the tools\n\nWe iterated with the [agent development lifecycle](https://www.langchain.com/blog/the-agent-development-lifecycle): run the agent on a benchmark, read its traces in LangSmith to see where it wasted steps or tokens, change the tool, and run evals to check that the change helped. Every run was a [LangSmith experiment](https://docs.langchain.com/langsmith/evaluation), so we could compare setups side by side and click from any number into the trace behind it.\n\nWe used three benchmarks of tasks on live websites:\n\n- [**WebVoyager**](https://arxiv.org/abs/2401.13919) : 100 everyday tasks across popular sites like Allrecipes, Amazon, and Google Flights, run with Claude Sonnet 5.5 and graded by a[Jev](https://www.langchain.com/blog/building-a-harness-with-jev) judge. It's fairly saturated, which made it a clean way to compare context use, cost, and speed.\n- [**HardBench**](https://github.com/browserbase/stagehand/blob/main/packages/evals/datasets/hardbenchmark/README.md) : 102 harder tasks from Browserbase, graded by its own verifier and run mostly with Claude Opus 5.5.\n- [**WebTailBench**](https://huggingface.co/datasets/microsoft/WebTailBench) : real-world \"tail\" tasks from Microsoft, each scored against a short rubric. We ran 150 of them through the[Stagehand evals](https://github.com/browserbase/stagehand/tree/main/packages/evals) .\n\nWe had every harness return structured output (a JSON answer). That way a formatting slip, like a one-line reply to a task that asks for a table, didn't count against its browsing. We [upstreamed](https://github.com/browserbase/stagehand/pull/3219) that option to Browserbase's evals.\n\n## Tool design is… so important\n\nWe first held the model and tasks fixed and changed only the toolset. The seven toolsets differ in how the agent calls them:\n\n- **Native tools:** one tool per action, like`click` or`navigate` , called one at a time ([Playwright MCP](https://github.com/microsoft/playwright-mcp) ,[browser-use](https://github.com/browser-use/browser-use) , and[Browserbase MCP](https://github.com/browserbase/mcp-server-browserbase) ).\n- **CLI:** the agent runs browser commands in a shell ([agent-browser](https://github.com/vercel-labs/agent-browser) and Browserbase's`browse` CLI).\n- **Code mode:** the agent writes code that calls the browser, so one call can run many steps (the Stagehand tools, and Browserbase MCP called from code).\n\nEvery toolset passed between 78% and 90% of tasks, but cost and speed varied widely. The Stagehand tools passed the most (90%) for $1.98 in total, at a median 17.4 s per task. Playwright MCP passed 78% for $26.71, about 13x the cost, and took three times as long per task ([full results](https://github.com/langchain-ai/deepagents/pull/6868)).\n\nStagehand gives the agent [three tools](https://docs.langchain.com/oss/python/integrations/tools/stagehand#tools):\n\n- `snapshot` reads the page's[accessibility tree](https://developer.mozilla.org/en-US/docs/Glossary/Accessibility_tree) and labels interactive elements with IDs.\n- `run` acts on the page, either with a batch of actions like`click` and`fill` or with JavaScript that uses a Playwright-style`page` object.\n- `screenshot` captures the rendered page.\n\nThe gap held on harder tasks. On HardBench's 38 core tasks, Deep Agents passed 30 with Stagehand's code-writing `run` tool and 21 with Browserbase's command-style `browse` CLI, at three times the cost per task.\n\n## Stagehand is built for agents\n\nEarly browser tooling repurposed testing SDKs like [Playwright](https://playwright.dev). As Browserbase puts it, [\"Playwright was built for testing, we built Stagehand for your agent.\"](https://www.browserbase.com/changelog/stagehand-v4) Three of the design choices from Browserbase's [Stagehand v4 announcement](https://www.browserbase.com/blog/stagehand-v4) made the most difference for us:\n\n- **Code mode** , so one tool call can run many browser steps\n- **Snapshots** , a compact text view of the page's interactive elements\n- **Fewer round trips** , by running inside the browser\n\n### Code mode keeps context lean\n\nIn code mode, the agent writes code that calls the tools instead of calling them one at a time (Browserbase makes the case in [Code mode is all you need](https://www.browserbase.com/blog/code-mode-is-all-you-need) and [Evolving computer use with code](https://www.browserbase.com/blog/evolving-computer-use-with-code)). It answers both challenges:\n\n- **No waiting on the model between calls.** Browser steps are sequential, so with`run` the agent writes the whole chain (open a page, fill a field, click, check) as one snippet, and it runs without a model decision between each step.\n- **Intermediate context stays out of the window.** Everything the snippet touches along the way stays in its variables, and only what the agent returns after the browser interaction lands in context.\n\nWe saw the effect before we picked Stagehand. Calling Browserbase's MCP tools from code in an [interpreter](https://www.langchain.com/blog/give-your-agents-an-interpreter), instead of one at a time, cut their cost 4x ($17.87 to $4.52).\n\nStagehand's `run` tool takes code natively. It averaged 10 steps per task against 26 for Playwright MCP, and a navigation sent back a median 117 characters against about 17,000 from Browserbase's hosted `navigate` tool.\n\n`run` also takes Playwright-style code. Models are trained on tons of Playwright, so that's the code they write best, and Stagehand's translator compiles it into Stagehand commands. When we tried a `run` that takes Stagehand's native API instead, it passed 5.3 points fewer tasks and cost 19% more per passed task, even with an accurate tool description.\n\n### Snapshots represent interactive elements\n\nReading a page is one of the most expensive things a browser agent does. `snapshot` returns the page's accessibility tree, trimmed to the parts an agent needs, with an ID on each interactive element. This gives the agent a view of the page that it can understand and act on directly, like `{\"op\": \"click\", \"id\": \"0-7\"}` in `run`, so it never has to read raw HTML or write a selector to click a button. Browserbase [reports](https://www.browserbase.com/changelog/stagehand-v4) v4 is about 80% more token efficient than Playwright.\n\nReading pages mostly through `snapshot` instead of `screenshot` is a big win for [context engineering](https://www.langchain.com/blog/context-engineering-for-agents). The tree is compact text the agent can act on, while a screenshot is an image the model has to interpret, at a median 89 KB per call in our WebTailBench runs. Trimming has to happen at the source, though. When we capped snapshots at 8,000 characters after the fact, agents just fetched the page again, and cost per passed task stayed flat.\n\n### Browser round trips are minimized\n\nA single Playwright `click` is five CDP calls: scroll the element into view, read its position, then mouse move, press, and release ([source](https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/chromium/crInput.ts)). Playwright waits for each response before sending the next, so one click costs five round trips between the agent and the browser. That cost adds up as the browser gets farther from the agent, from a laptop to a sandbox to the cloud.\n\n[Stagehand v4](https://www.browserbase.com/blog/stagehand-v4) runs inside the browser as an extension, so those low-level calls happen next to the page, and `run` sends a whole batch of actions in one round trip. Browserbase reports v4 runs 2x faster than Playwright in its benchmarks.\n\n## Exploring other optimizations\n\nWith Stagehand chosen, we kept tuning the harness. The biggest win came from fixing how agents wait for pages to load.\n\nReading traces in LangSmith, we saw agents writing fixed sleeps like `page.waitForTimeout(8000)` to wait for a page to load. Most pages were ready long before the timer ran out, so agents spent a lot of each run doing nothing.\n\nSo we changed `run` to end each sleep once the page has been quiet for 500 ms, and never later than the time the agent asked for. In one measurement, an 8-second sleep returned in 0.6 s. Then we checked the change with evals. On HardBench, together with structured answers and a model-call timeout, it cut median agent time from 182 s to 124 s, with accuracy within noise.\n\nStagehand has since [upstreamed](https://github.com/browserbase/stagehand/pull/3282) the fix.\n\n## Conclusion\n\nBrowser tasks stretch an agent's context more than most work. The biggest gains we found came from the tools, mostly from how much they send back to the model and how many turns they take.\n\nYou can use the same process on any agent. Understand the context your use case produces, then iterate with traces and evals. Traces show where the agent wastes steps and tokens, and evals tell you whether a change helped.\n\nThese are the tools behind the browser in Managed Deep Agents. Give your agent a [browser in Managed Deep Agents](https://docs.langchain.com/langsmith/python/managed-deep-agents-browser-use), which we [launched last week](https://www.langchain.com/blog/browser-use-in-managed-deep-agents).\n\nTo use the same tools in open source [Deep Agents](https://docs.langchain.com/oss/python/deepagents/overview), add `StagehandMiddleware` from [langchain-stagehand](https://docs.langchain.com/oss/python/integrations/tools/stagehand) to your agent:\n\n```\nfrom deepagents import create_deep_agent\nfrom langchain_stagehand import StagehandMiddleware\nfrom stagehand import Stagehand, local_browser\nbrowser = await local_browser.launch(headless=True)\nstagehand = await Stagehand.create(browser=browser)\nagent = create_deep_agent(\n    model=\"openai:gpt-5.5\",\n    middleware=[StagehandMiddleware(stagehand=stagehand)],\n)\n```\nFor agents that browse the open web at scale, launch the browser on [Browserbase](https://docs.langchain.com/oss/python/integrations/providers/browserbase) with `browserbase.launch()` instead. The [browser use guide](https://docs.langchain.com/oss/python/deepagents/browser-use) covers the rest, including browser scope and testing an app in a sandbox. Give them a try, and let us know what you build!\n\n## Acknowledgements\n\nThanks to Baihao Wang for leading the HardBench work backing the optimizations upstreamed to Stagehand, and to [Sean McGuire](https://www.linkedin.com/in/sean-p-mcguire/), [Miguel Gonzalez](https://www.linkedin.com/in/miguelgfz), and [Kyle Jeong](https://www.linkedin.com/in/kyle-jeong) from Browserbase for their help with Stagehand.","body_html":"<h2 id=\"key-takeaways\">Key Takeaways</h2>\n<ul><li>Browser use is an important capability for agents doing autonomous work, letting them act on sites built for people.</li><li>The tools an agent uses to drive the browser have a huge impact on its cost and trajectory, and Stagehand&#39;s are built for agents.</li><li>Optimizing tools is an iterative process: trace the agent&#39;s behavior to find where it&#39;s inefficient, update the harness, and test every change with evals before keeping it.</li></ul>\n<p>Browser agents can now take on multi-step work that&#39;s painful and time-consuming for people. Personal agents like <a href=\"https://instinct.com/\" rel=\"nofollow ugc noopener\">Instinct</a> and Meta&#39;s <a href=\"https://ai.meta.com/muse/\" rel=\"nofollow ugc noopener\">Muse</a> plan cross-country road trips, cancel unused subscriptions, and list cars for sale, all on sites with no public APIs. <a href=\"https://finance.yahoo.com/technology/ai/articles/datadome-report-agentic-browser-traffic-130000558.html\" rel=\"nofollow ugc noopener\">DataDome</a> saw agentic browser requests jump 4.7x in September alone.</p>\n<p>Browser use helps in two places: actions an API can&#39;t cover, like using a website the agent wasn&#39;t set up for, and end-to-end testing of the web apps coding agents build. Either way, the agent needs two things:</p>\n<ol><li><strong>Tools</strong> to drive the browser: open pages, read them, and act on them. Most toolsets control the browser over the<a href=\"https://chromedevtools.github.io/devtools-protocol/\" rel=\"nofollow ugc noopener\">Chrome DevTools Protocol</a> (CDP), the protocol that lets tools inspect and control Chrome.</li><li><strong>A browser</strong> to drive, either local or hosted.<a href=\"https://docs.langchain.com/langsmith/python/managed-deep-agents\" rel=\"nofollow ugc noopener\">Managed Deep Agents</a> lets you build and deploy agents in LangSmith without running any infrastructure, and it can<a href=\"https://docs.langchain.com/langsmith/python/managed-deep-agents-browser-use\" rel=\"nofollow ugc noopener\">run the browser</a> for your agent too (more in our<a href=\"https://www.langchain.com/blog/browser-use-in-managed-deep-agents\" rel=\"nofollow ugc noopener\">launch post</a> and<a href=\"https://www.youtube.com/watch?v=r1iJzoxFBtg\" rel=\"nofollow ugc noopener\">video walkthrough</a> ). A hosted browser provider like<a href=\"https://www.browserbase.com\" rel=\"nofollow ugc noopener\">Browserbase</a> handles the hard parts of the open web for you, like CAPTCHAs, bot blocking, and running many browsers at scale.</li></ol>\n<p>This post focuses on the tools, and on making them good enough that the agent uses the browser well.</p>\n<p>We set out to give agents in <a href=\"https://docs.langchain.com/langsmith/python/managed-deep-agents\" rel=\"nofollow ugc noopener\">Managed Deep Agents</a> a browser. We compared seven toolsets and picked the best one, <a href=\"https://www.stagehand.dev/\" rel=\"nofollow ugc noopener\">Stagehand</a> (<a href=\"https://github.com/browserbase/stagehand\" rel=\"nofollow ugc noopener\">GitHub</a>) from <a href=\"https://www.browserbase.com\" rel=\"nofollow ugc noopener\">Browserbase</a>. Then we improved it based on what agents did in <a href=\"https://docs.langchain.com/langsmith/observability\" rel=\"nofollow ugc noopener\">LangSmith</a> traces, and validated every change with evals. The biggest lesson was how much room there is to improve an agent through tool design.</p>\n<h2 id=\"browser-use-poses-context-management-challenges\">Browser use poses context management challenges</h2>\n<p>Browser use toolsets come in a few forms: native tools, an <a href=\"https://docs.langchain.com/oss/python/langchain/mcp\" rel=\"nofollow ugc noopener\">MCP server</a>, or a CLI the agent runs in a shell. Browser use poses two unique challenges for those tools:</p>\n<ul><li><strong>Tasks are long-running</strong> , with a lot of sequential tool calls. On Playwright MCP, a single WebVoyager task averaged 26 model turns.</li><li><strong>Web pages carry a lot of content</strong> , and only a fraction of it is usually relevant to the task. One navigation through Browserbase&#39;s hosted MCP server returns a median of about 17,000 characters.</li></ul>\n<p>Both mean browser tools have to manage context efficiently.</p>\n<h2 id=\"how-we-tested-the-tools\">How we tested the tools</h2>\n<p>We iterated with the <a href=\"https://www.langchain.com/blog/the-agent-development-lifecycle\" rel=\"nofollow ugc noopener\">agent development lifecycle</a>: run the agent on a benchmark, read its traces in LangSmith to see where it wasted steps or tokens, change the tool, and run evals to check that the change helped. Every run was a <a href=\"https://docs.langchain.com/langsmith/evaluation\" rel=\"nofollow ugc noopener\">LangSmith experiment</a>, so we could compare setups side by side and click from any number into the trace behind it.</p>\n<p>We used three benchmarks of tasks on live websites:</p>\n<ul><li><a href=\"https://arxiv.org/abs/2401.13919\" rel=\"nofollow ugc noopener\"><strong>WebVoyager</strong></a> : 100 everyday tasks across popular sites like Allrecipes, Amazon, and Google Flights, run with Claude Sonnet 5.5 and graded by a<a href=\"https://www.langchain.com/blog/building-a-harness-with-jev\" rel=\"nofollow ugc noopener\">Jev</a> judge. It&#39;s fairly saturated, which made it a clean way to compare context use, cost, and speed.</li><li><a href=\"https://github.com/browserbase/stagehand/blob/main/packages/evals/datasets/hardbenchmark/README.md\" rel=\"nofollow ugc noopener\"><strong>HardBench</strong></a> : 102 harder tasks from Browserbase, graded by its own verifier and run mostly with Claude Opus 5.5.</li><li><a href=\"https://huggingface.co/datasets/microsoft/WebTailBench\" rel=\"nofollow ugc noopener\"><strong>WebTailBench</strong></a> : real-world &quot;tail&quot; tasks from Microsoft, each scored against a short rubric. We ran 150 of them through the<a href=\"https://github.com/browserbase/stagehand/tree/main/packages/evals\" rel=\"nofollow ugc noopener\">Stagehand evals</a> .</li></ul>\n<p>We had every harness return structured output (a JSON answer). That way a formatting slip, like a one-line reply to a task that asks for a table, didn&#39;t count against its browsing. We <a href=\"https://github.com/browserbase/stagehand/pull/3219\" rel=\"nofollow ugc noopener\">upstreamed</a> that option to Browserbase&#39;s evals.</p>\n<h2 id=\"tool-design-is-so-important\">Tool design is… so important</h2>\n<p>We first held the model and tasks fixed and changed only the toolset. The seven toolsets differ in how the agent calls them:</p>\n<ul><li><strong>Native tools:</strong> one tool per action, like<code>click</code> or<code>navigate</code> , called one at a time (<a href=\"https://github.com/microsoft/playwright-mcp\" rel=\"nofollow ugc noopener\">Playwright MCP</a> ,<a href=\"https://github.com/browser-use/browser-use\" rel=\"nofollow ugc noopener\">browser-use</a> , and<a href=\"https://github.com/browserbase/mcp-server-browserbase\" rel=\"nofollow ugc noopener\">Browserbase MCP</a> ).</li><li><strong>CLI:</strong> the agent runs browser commands in a shell (<a href=\"https://github.com/vercel-labs/agent-browser\" rel=\"nofollow ugc noopener\">agent-browser</a> and Browserbase&#39;s<code>browse</code> CLI).</li><li><strong>Code mode:</strong> the agent writes code that calls the browser, so one call can run many steps (the Stagehand tools, and Browserbase MCP called from code).</li></ul>\n<p>Every toolset passed between 78% and 90% of tasks, but cost and speed varied widely. The Stagehand tools passed the most (90%) for $1.98 in total, at a median 17.4 s per task. Playwright MCP passed 78% for $26.71, about 13x the cost, and took three times as long per task (<a href=\"https://github.com/langchain-ai/deepagents/pull/6868\" rel=\"nofollow ugc noopener\">full results</a>).</p>\n<p>Stagehand gives the agent <a href=\"https://docs.langchain.com/oss/python/integrations/tools/stagehand#tools\" rel=\"nofollow ugc noopener\">three tools</a>:</p>\n<ul><li><code>snapshot</code> reads the page&#39;s<a href=\"https://developer.mozilla.org/en-US/docs/Glossary/Accessibility_tree\" rel=\"nofollow ugc noopener\">accessibility tree</a> and labels interactive elements with IDs.</li><li><code>run</code> acts on the page, either with a batch of actions like<code>click</code> and<code>fill</code> or with JavaScript that uses a Playwright-style<code>page</code> object.</li><li><code>screenshot</code> captures the rendered page.</li></ul>\n<p>The gap held on harder tasks. On HardBench&#39;s 38 core tasks, Deep Agents passed 30 with Stagehand&#39;s code-writing <code>run</code> tool and 21 with Browserbase&#39;s command-style <code>browse</code> CLI, at three times the cost per task.</p>\n<h2 id=\"stagehand-is-built-for-agents\">Stagehand is built for agents</h2>\n<p>Early browser tooling repurposed testing SDKs like <a href=\"https://playwright.dev\" rel=\"nofollow ugc noopener\">Playwright</a>. As Browserbase puts it, <a href=\"https://www.browserbase.com/changelog/stagehand-v4\" rel=\"nofollow ugc noopener\">&quot;Playwright was built for testing, we built Stagehand for your agent.&quot;</a> Three of the design choices from Browserbase&#39;s <a href=\"https://www.browserbase.com/blog/stagehand-v4\" rel=\"nofollow ugc noopener\">Stagehand v4 announcement</a> made the most difference for us:</p>\n<ul><li><strong>Code mode</strong> , so one tool call can run many browser steps</li><li><strong>Snapshots</strong> , a compact text view of the page&#39;s interactive elements</li><li><strong>Fewer round trips</strong> , by running inside the browser</li></ul>\n<h3 id=\"code-mode-keeps-context-lean\">Code mode keeps context lean</h3>\n<p>In code mode, the agent writes code that calls the tools instead of calling them one at a time (Browserbase makes the case in <a href=\"https://www.browserbase.com/blog/code-mode-is-all-you-need\" rel=\"nofollow ugc noopener\">Code mode is all you need</a> and <a href=\"https://www.browserbase.com/blog/evolving-computer-use-with-code\" rel=\"nofollow ugc noopener\">Evolving computer use with code</a>). It answers both challenges:</p>\n<ul><li><strong>No waiting on the model between calls.</strong> Browser steps are sequential, so with<code>run</code> the agent writes the whole chain (open a page, fill a field, click, check) as one snippet, and it runs without a model decision between each step.</li><li><strong>Intermediate context stays out of the window.</strong> Everything the snippet touches along the way stays in its variables, and only what the agent returns after the browser interaction lands in context.</li></ul>\n<p>We saw the effect before we picked Stagehand. Calling Browserbase&#39;s MCP tools from code in an <a href=\"https://www.langchain.com/blog/give-your-agents-an-interpreter\" rel=\"nofollow ugc noopener\">interpreter</a>, instead of one at a time, cut their cost 4x ($17.87 to $4.52).</p>\n<p>Stagehand&#39;s <code>run</code> tool takes code natively. It averaged 10 steps per task against 26 for Playwright MCP, and a navigation sent back a median 117 characters against about 17,000 from Browserbase&#39;s hosted <code>navigate</code> tool.</p>\n<p><code>run</code> also takes Playwright-style code. Models are trained on tons of Playwright, so that&#39;s the code they write best, and Stagehand&#39;s translator compiles it into Stagehand commands. When we tried a <code>run</code> that takes Stagehand&#39;s native API instead, it passed 5.3 points fewer tasks and cost 19% more per passed task, even with an accurate tool description.</p>\n<h3 id=\"snapshots-represent-interactive-elements\">Snapshots represent interactive elements</h3>\n<p>Reading a page is one of the most expensive things a browser agent does. <code>snapshot</code> returns the page&#39;s accessibility tree, trimmed to the parts an agent needs, with an ID on each interactive element. This gives the agent a view of the page that it can understand and act on directly, like <code>{&quot;op&quot;: &quot;click&quot;, &quot;id&quot;: &quot;0-7&quot;}</code> in <code>run</code>, so it never has to read raw HTML or write a selector to click a button. Browserbase <a href=\"https://www.browserbase.com/changelog/stagehand-v4\" rel=\"nofollow ugc noopener\">reports</a> v4 is about 80% more token efficient than Playwright.</p>\n<p>Reading pages mostly through <code>snapshot</code> instead of <code>screenshot</code> is a big win for <a href=\"https://www.langchain.com/blog/context-engineering-for-agents\" rel=\"nofollow ugc noopener\">context engineering</a>. The tree is compact text the agent can act on, while a screenshot is an image the model has to interpret, at a median 89 KB per call in our WebTailBench runs. Trimming has to happen at the source, though. When we capped snapshots at 8,000 characters after the fact, agents just fetched the page again, and cost per passed task stayed flat.</p>\n<h3 id=\"browser-round-trips-are-minimized\">Browser round trips are minimized</h3>\n<p>A single Playwright <code>click</code> is five CDP calls: scroll the element into view, read its position, then mouse move, press, and release (<a href=\"https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/chromium/crInput.ts\" rel=\"nofollow ugc noopener\">source</a>). Playwright waits for each response before sending the next, so one click costs five round trips between the agent and the browser. That cost adds up as the browser gets farther from the agent, from a laptop to a sandbox to the cloud.</p>\n<p><a href=\"https://www.browserbase.com/blog/stagehand-v4\" rel=\"nofollow ugc noopener\">Stagehand v4</a> runs inside the browser as an extension, so those low-level calls happen next to the page, and <code>run</code> sends a whole batch of actions in one round trip. Browserbase reports v4 runs 2x faster than Playwright in its benchmarks.</p>\n<h2 id=\"exploring-other-optimizations\">Exploring other optimizations</h2>\n<p>With Stagehand chosen, we kept tuning the harness. The biggest win came from fixing how agents wait for pages to load.</p>\n<p>Reading traces in LangSmith, we saw agents writing fixed sleeps like <code>page.waitForTimeout(8000)</code> to wait for a page to load. Most pages were ready long before the timer ran out, so agents spent a lot of each run doing nothing.</p>\n<p>So we changed <code>run</code> to end each sleep once the page has been quiet for 500 ms, and never later than the time the agent asked for. In one measurement, an 8-second sleep returned in 0.6 s. Then we checked the change with evals. On HardBench, together with structured answers and a model-call timeout, it cut median agent time from 182 s to 124 s, with accuracy within noise.</p>\n<p>Stagehand has since <a href=\"https://github.com/browserbase/stagehand/pull/3282\" rel=\"nofollow ugc noopener\">upstreamed</a> the fix.</p>\n<h2 id=\"conclusion\">Conclusion</h2>\n<p>Browser tasks stretch an agent&#39;s context more than most work. The biggest gains we found came from the tools, mostly from how much they send back to the model and how many turns they take.</p>\n<p>You can use the same process on any agent. Understand the context your use case produces, then iterate with traces and evals. Traces show where the agent wastes steps and tokens, and evals tell you whether a change helped.</p>\n<p>These are the tools behind the browser in Managed Deep Agents. Give your agent a <a href=\"https://docs.langchain.com/langsmith/python/managed-deep-agents-browser-use\" rel=\"nofollow ugc noopener\">browser in Managed Deep Agents</a>, which we <a href=\"https://www.langchain.com/blog/browser-use-in-managed-deep-agents\" rel=\"nofollow ugc noopener\">launched last week</a>.</p>\n<p>To use the same tools in open source <a href=\"https://docs.langchain.com/oss/python/deepagents/overview\" rel=\"nofollow ugc noopener\">Deep Agents</a>, add <code>StagehandMiddleware</code> from <a href=\"https://docs.langchain.com/oss/python/integrations/tools/stagehand\" rel=\"nofollow ugc noopener\">langchain-stagehand</a> to your agent:</p>\n<pre><code>from deepagents import create_deep_agent\nfrom langchain_stagehand import StagehandMiddleware\nfrom stagehand import Stagehand, local_browser\nbrowser = await local_browser.launch(headless=True)\nstagehand = await Stagehand.create(browser=browser)\nagent = create_deep_agent(\n    model=&quot;openai:gpt-5.5&quot;,\n    middleware=[StagehandMiddleware(stagehand=stagehand)],\n)</code></pre>\n<p>For agents that browse the open web at scale, launch the browser on <a href=\"https://docs.langchain.com/oss/python/integrations/providers/browserbase\" rel=\"nofollow ugc noopener\">Browserbase</a> with <code>browserbase.launch()</code> instead. The <a href=\"https://docs.langchain.com/oss/python/deepagents/browser-use\" rel=\"nofollow ugc noopener\">browser use guide</a> covers the rest, including browser scope and testing an app in a sandbox. Give them a try, and let us know what you build!</p>\n<h2 id=\"acknowledgements\">Acknowledgements</h2>\n<p>Thanks to Baihao Wang for leading the HardBench work backing the optimizations upstreamed to Stagehand, and to <a href=\"https://www.linkedin.com/in/sean-p-mcguire/\" rel=\"nofollow ugc noopener\">Sean McGuire</a>, <a href=\"https://www.linkedin.com/in/miguelgfz\" rel=\"nofollow ugc noopener\">Miguel Gonzalez</a>, and <a href=\"https://www.linkedin.com/in/kyle-jeong\" rel=\"nofollow ugc noopener\">Kyle Jeong</a> from Browserbase for their help with Stagehand.</p>","headings":[{"level":2,"text":"Key Takeaways","id":"key-takeaways"},{"level":2,"text":"Browser use poses context management challenges","id":"browser-use-poses-context-management-challenges"},{"level":2,"text":"How we tested the tools","id":"how-we-tested-the-tools"},{"level":2,"text":"Tool design is… so important","id":"tool-design-is-so-important"},{"level":2,"text":"Stagehand is built for agents","id":"stagehand-is-built-for-agents"},{"level":3,"text":"Code mode keeps context lean","id":"code-mode-keeps-context-lean"},{"level":3,"text":"Snapshots represent interactive elements","id":"snapshots-represent-interactive-elements"},{"level":3,"text":"Browser round trips are minimized","id":"browser-round-trips-are-minimized"},{"level":2,"text":"Exploring other optimizations","id":"exploring-other-optimizations"},{"level":2,"text":"Conclusion","id":"conclusion"},{"level":2,"text":"Acknowledgements","id":"acknowledgements"}]}}