One browser many exits with Puppeteer contexts
The question behind most Puppeteer proxy setups
Puppeteer jobs that use proxies nearly always hit the same design question within the first week. You’ve got a handful of exits, maybe from one provider in several countries, maybe sticky sessions on a rotating gateway. Do you launch a separate Chrome for each exit, or one Chrome that talks through several exits at once?
For a long time the honest answer in Puppeteer was “one browser per exit”, because the proxy was a launch flag and couldn’t be changed afterwards. That’s no longer the only option. Browser contexts can now carry their own proxy server, which means one Chrome process can hold several isolated sessions, each leaving through a different address.
That’s a real improvement, and it comes with trade-offs that aren’t obvious from the API docs. Authentication works differently from the proxy address itself. Memory doesn’t drop as much as people hope. And a single browser holding six exits is a single point of failure for six jobs.
This piece is the Node-side companion to our Playwright proxy settings piece, and it deliberately doesn’t repeat that one’s list of silent failures. Playwright and Puppeteer share an engine and some habits, but they draw the proxy boundary in different places. Here the focus is how Puppeteer does it: proxies at launch, proxies per context, how credentials get attached, and how to decide between one browser with many exits and one browser per exit.
A note on names before we start. Recent Puppeteer versions call the method browser.createBrowserContext(). Older versions called it createIncognitoBrowserContext(). The idea is the same. If you’re on an old release and the options described here don’t exist, upgrading is the fix.
We run proxy infrastructure and the browser jobs that sit on top of it. Nothing below is benchmark data. Where a number would need a test on your own hardware, we’ll say so instead of inventing one.
How Chrome itself takes a proxy
Puppeteer doesn’t have its own networking. It drives Chrome (or Chromium) over the DevTools protocol, and Chrome does the fetching. So the first thing to understand is how Chrome accepts proxy settings, because everything Puppeteer offers is a wrapper around that.
The oldest route is a command-line switch passed at launch:
const browser = await puppeteer.launch({
args: ['--proxy-server=http://gw.example-provider.net:8000'],
});
That flag applies to the whole browser. Every page, every context, every service worker goes through that proxy unless a bypass rule says otherwise. There’s a companion flag, --proxy-bypass-list, that takes a semicolon-separated list of hosts to reach directly.
The switch takes a scheme, a host and a port. It doesn’t take a username or password. People try http://user:pass@host:port in the flag, and Chrome quietly drops the credentials part, so the first request gets a 407 challenge from the proxy that nothing answers. The browser then shows an error page or hangs on a blank tab, depending on the version.
Chrome also supports SOCKS through the same flag, with socks5://host:port. For SOCKS5, Chrome sends the hostname to the proxy and lets it do the lookup. What Chrome doesn’t support is SOCKS5 with a username and password. If your provider only offers authenticated SOCKS, the browser can’t use it directly, and you’ll need either the provider’s HTTP endpoint, IP allowlisting, or a small local forwarder that holds the credentials and exposes an unauthenticated port to the browser. That forwarder pattern is covered in its own piece on this site, so here it’s enough to know it exists.
Everything in the rest of this article sits on those two facts. The proxy address is configuration. The credentials are a response to a challenge.
The launch-level proxy and what it’s good for
The launch flag is the simplest setup, and for plenty of jobs it’s still the right one. One browser, one exit, every page through it. If your job runs a single sticky session, or your provider gives you a rotating gateway where one address hands out different exits on its side, there’s nothing to gain from contexts.
It’s also the most predictable. Because the proxy is set before the browser starts, there’s no window where a page loads before proxy settings apply. Background traffic that Chrome generates on its own (component updates, safe browsing checks, and similar) goes through the same route unless you’ve disabled it, which is usually what you want on a scraping box.
The weakness is that it’s fixed. You can’t change the exit of a running browser. If a job needs a different country halfway through, or an exit starts failing, the only option is to close the browser and launch a new one with a different flag. That’s slow compared with opening a context, and it throws away every warm connection and cached asset.
The launch-level model also forces one browser per exit when you have several exits. That’s where the memory cost comes in, and it’s the reason people started asking whether contexts could carry their own proxy.
A small practical point: when you use the launch flag, set the bypass list deliberately. A bypass for localhost is sensible if the job talks to a local service. A bypass that’s too broad, copied from a desktop setup, sends target traffic direct without any error. The browser won’t tell you which requests bypassed. Only an outside check will.
Contexts that carry their own proxy
A browser context is an isolated session inside one Chrome process. Each context has its own cookies, local storage, cache and permissions. Pages in one context can’t see another context’s state. It’s the same machinery Chrome uses for a guest or incognito window.
In current Puppeteer, you can give a context its own proxy when you create it:
const ctxA = await browser.createBrowserContext({
proxyServer: 'http://exit-a.example-provider.net:8000',
proxyBypassList: ['localhost'],
});
const ctxB = await browser.createBrowserContext({
proxyServer: 'http://exit-b.example-provider.net:8000',
});
Pages opened in ctxA leave through exit A. Pages in ctxB leave through exit B. The browser itself can have no launch-level proxy at all, or it can have one that acts as the default for pages opened outside any custom context.
The proxy is fixed for the life of the context, which is fine because contexts are cheap to make and close. Changing exits means closing a context and making a new one, which takes far less time than restarting a browser. You lose that context’s cookies and cache when you do it, which is often correct anyway. A new exit with an old session’s cookies is exactly the kind of mismatch our piece on rotating without losing your session warns about.
Watch the default context. Every browser has one, and browser.newPage() opens a page in it rather than in any context you created. The default context uses the launch-level proxy if there is one, and no proxy at all if there isn’t. So a helper somewhere that calls browser.newPage() for a quick check, or a library that does it on your behalf, sends that page’s traffic direct from a browser you thought was fully proxied. If you run with per-context proxies and no launch flag, search the code for browser.newPage() and replace each one with a page from a named context.
One thing contexts don’t isolate is the browser process itself. Every context shares the same Chrome build, the same launch flags, the same GPU process, the same user agent defaults. If something about the browser configuration is wrong, it’s wrong in every context at once. And if the browser crashes, every context goes with it.
Authenticating a proxy per page
Here’s the part that surprises people. A context can carry a proxy address. It can’t carry proxy credentials. Authentication in Puppeteer happens at the page level, with page.authenticate():
const page = await ctxA.newPage();
await page.authenticate({ username: userA, password: passA });
await page.goto('https://example.com/');
What authenticate does is tell Puppeteer to answer authentication challenges for that page with those credentials. When the proxy replies with a 407, Puppeteer supplies the username and password and the request goes through. The Puppeteer docs note that it turns on request interception behind the scenes to do this, which can affect performance. That’s a real consideration on pages with hundreds of subresources, and how much it matters depends on the page and the machine. It’s worth measuring on your own job rather than assuming either way.
Because authentication is per page, every new page needs it. That includes pages you didn’t open yourself. A link with target="_blank", a window.open() from a script, or a login flow that opens a popup all create a new page in the same context, and that page starts without credentials. The first request from it gets a 407 and fails.
The fix is to listen for new targets in the context and authenticate them as they appear:
ctxA.on('targetcreated', async (t) => {
const p = await t.page();
if (p) await p.authenticate({ username: userA, password: passA });
});
There’s a small race there, since the popup’s first request can go out before the handler runs. If the job depends on popups working reliably, test that path specifically, and consider whether the provider can allowlist your server’s IP for that context’s exit so no challenge happens at all. IP allowlisting sidesteps the whole per-page problem, at the cost of tying the account to a fixed server address.
Where credentials and addresses go wrong together
With the address on the context and the credentials on the page, there are two separate places to get something wrong, and the failure modes combine in awkward ways.
The obvious one is a page that has the right address and no credentials. That’s the popup case above. It fails loudly, with a 407 or a proxy error page, so at least you’ll see it.
The quieter one is credentials that belong to a different exit. Say you build a helper that creates a context and a page, and the helper takes the proxy URL and the credentials as separate arguments. A refactor swaps two variables, and now context A’s pages authenticate with exit B’s credentials. On some providers that fails. On providers where one account’s credentials work on every gateway, and the username itself decides the country or session, it succeeds, and your context A traffic is now coming out of whatever exit B’s username asks for. The route works. It’s the wrong route.
The fix is structural. Keep each exit’s address and credentials in one object, and build the context and its pages from that object only:
const exits = [
{ name: 'de-1', server: 'http://gw.example-provider.net:8000', user: 'u-de-1', pass: '...' },
{ name: 'fr-1', server: 'http://gw.example-provider.net:8000', user: 'u-fr-1', pass: '...' },
];
Notice that both exits above share the same server address. That’s common with gateway-style providers, where targeting lives in the username. It also means the context’s proxyServer is identical for both, and the only thing separating them is which credentials each page sends. If you ever see two contexts with the same proxy server, the credentials are doing all the routing, and a mix-up there is invisible until you check the exit address from inside each context.
Memory, and what a context actually saves
The pitch for contexts is that they’re cheaper than browsers. That’s true, and it’s worth being precise about which costs go away.
A Chrome instance is several processes: a browser process that coordinates everything, a GPU process (or a software rendering equivalent on a headless server), a network service, and one or more renderer processes for page content. When you launch a second browser, you pay for all of that again. When you create a second context in the same browser, you share the browser process, the GPU process and the network service. You still get renderer processes for the pages you open, because Chrome isolates sites into their own renderers regardless of context.
So contexts save the fixed overhead per browser. They don’t save the per-page cost, and on heavy pages the per-page cost is most of the memory. A job running six contexts with two heavy pages each will still use roughly what twelve heavy pages need, plus one browser’s overhead instead of six. How big that difference is depends on the pages, the Chrome version and whether you’re headless. We haven’t published a measurement, and a number from someone else’s machine wouldn’t transfer to yours anyway. Test it with your real pages, using the process list, before and after.
Startup time is the other saving. Launching Chrome takes noticeably longer than opening a context in one that’s already running, so jobs that switch exits often gain the most from contexts.
The saving people forget to claim is cache. Each context has its own cache, so contexts don’t share downloaded assets. Two contexts hitting the same site download the same scripts and images twice, through two different exits, and on a metered plan you pay for both. That’s the correct behaviour for isolation. It just means contexts don’t reduce bandwidth, only fixed memory and startup.
Cleanup is the real cost of sharing a browser
The trade-off that decides most designs is failure handling. One browser with six contexts is efficient until something goes wrong, and then it’s six jobs that go wrong together.
Browsers leak. Long-running Chrome processes tend to grow over time, especially on pages with heavy scripts or long-lived connections. Closing a context should release its pages and state, and it mostly does, but a browser that’s opened and closed hundreds of contexts over a day can still end up larger than a fresh one. The practical answer is to recycle: after a number of contexts, or a period of time, close the whole browser and launch a new one. What that number should be is something to find from your own process monitoring rather than from a rule of thumb.
Contexts also have to be closed explicitly. A context you forget to close keeps its pages alive and its connections open. The safe pattern is a try and finally around every unit of work, with await context.close() in the finally, so an exception during scraping doesn’t leave an orphaned context behind.
Crashes are the harder case. If the browser process dies, every context and page goes with it, and your Node code gets a disconnected event on the browser object. A job that holds six exits in one browser needs to handle that event, work out which units of work were in flight, and requeue them on a fresh browser. A job with one browser per exit only loses one exit’s work.
Then there are zombie processes. If Node itself exits unexpectedly without closing the browser, Chrome processes can survive and pile up on the host. Register a handler on process exit that closes the browser, and have a supervisor that kills stray Chrome processes on restart. This matters more with one big shared browser, because one orphan holds more memory.
When one browser per exit is still the better call
Contexts aren’t automatically the right answer. There are clear cases where one browser per exit is simpler and safer.
The first is when exits need different browser configuration, not just a different proxy. Launch flags, window size, locale and timezone settings (which our piece on timezone and locale mismatch covers) apply at the browser level, and some of them can’t be set per context. If exit A is in Germany and exit B in Brazil, and the job needs the browser’s locale to match each, separate browsers are the clean way.
The second is when isolation of failure matters more than memory. A job that runs long, heavy sessions and can’t afford to lose six of them to one crash is better off paying for six browsers.
The third is when you’re running on a machine with plenty of memory and few exits. If you have three exits and a server with room to spare, the complexity of per-page authentication, target listeners and shared-browser crash handling may not be worth it. Three launches with three flags is easy to reason about.
Contexts win when you have many exits and short units of work: open a context, do a small task through one exit, close it, move on. That’s the pattern where startup time and fixed overhead dominate, and where losing a browser means losing a few seconds of work rather than hours.
A mixed design is also common. Run a small number of browsers, each holding a handful of contexts, and recycle each browser on a schedule. That caps the blast radius of a crash without paying full overhead for every exit.
Matching a context’s life to a sticky session
Many providers sell sticky sessions on a rotating gateway: the username carries a session token, and the gateway keeps handing out the same exit for that token until a time limit runs out or the exit drops. Contexts pair naturally with this. One context, one session token, one exit.
The catch is that the two lifetimes are set in different places. Your code decides how long a context lives. The provider decides how long the sticky session holds. If the context outlives the session, the gateway quietly moves that token to a new exit, and the context carries on with cookies that were set from the old address. Nothing in Puppeteer notices. The page just starts behaving like a session that changed networks halfway through, which is exactly what it is.
There are two ways to keep them aligned. The simple one is to make every context shorter than the sticky window the provider documents, with a margin, and to close it before the window runs out. The other is to check the exit from inside the context at intervals during a long task, and to end the unit of work cleanly if it changes. The second costs an extra request each time, which on a metered plan is small but not free.
It’s also worth knowing what the provider does when the exit behind a sticky session goes offline before the window ends. Some reassign the token straight away. Some return errors until the window expires. That behaviour is a good question to put to a provider during a trial, because it decides whether your context sees a clean failure or a silent change of address.
Our piece on how long a session should live covers the target-side reasons to keep sessions short. The Puppeteer-specific point is simpler: a context doesn’t know about the provider’s clock, so your code has to.
Telling proxy errors from target errors
When a page fails to load in a proxied context, the first question is which hop failed. Puppeteer surfaces Chrome’s network errors directly, and the error text tells you a lot if you log it instead of catching everything under one generic retry.
Errors from the proxy hop tend to have recognisable names. net::ERR_PROXY_CONNECTION_FAILED means Chrome couldn’t reach the proxy at all. net::ERR_TUNNEL_CONNECTION_FAILED usually means the proxy was reached but refused or failed to open the tunnel to the target. net::ERR_INVALID_AUTH_CREDENTIALS points at authentication, which in a context setup often means a page that didn’t get authenticate called, or got the wrong credentials.
Those errors are thrown from page.goto() because there’s no response to return. When the proxy works and the target answers, goto returns a response object, and its status belongs to the target. A 403, a 429 or a challenge page came through the exit successfully. The proxy did its job.
Keep the two apart in your handling. Proxy-hop errors are an infrastructure signal: count them per exit, back off that exit, and alert if one keeps failing. Target responses are a behaviour signal: slow down for that site and read what it’s telling you. Retrying a 429 straight away on a fresh context with a fresh exit is the wrong reaction, because the site is responding to pace, and a new address doesn’t change pace.
Logging the exit name next to every error makes the pattern obvious within a day. One exit failing on every site is a bad exit. Every exit failing on one site is that site asking you to slow down.
Checking the exit from inside each context
With several exits in one browser, you need a way to prove each context leaves where it should. Checking once from the command line proves nothing about the contexts, because each has its own proxy and each page has its own credentials.
The check is to open a page in each context, send it to an IP echo endpoint you trust, read the body, and compare it with what that exit should be:
async function exitOf(ctx, creds) {
const p = await ctx.newPage();
try {
await p.authenticate(creds);
await p.goto('https://ip-echo.example.org/', { waitUntil: 'domcontentloaded' });
return (await p.evaluate(() => document.body.innerText)).trim();
} finally {
await p.close();
}
}
Run that for every context at the start of each job and log the result with the exit’s name. If two contexts report the same address, or a context reports your server’s own address, stop and look. Our piece on why a proxy works in curl but fails in browser automation walks through what to do when the browser disagrees with a command-line test.
Also remember that an echo page checks only HTTP traffic. Chrome has features that can open connections outside the HTTP proxy path, WebRTC being the usual example, and whether those follow your proxy depends on Chrome’s settings and version. For a scraping job that doesn’t need them, the cleanest approach is not to use pages that depend on them, and to check your launch configuration against current Chrome documentation rather than trusting an old snippet.
Bandwidth and interception across contexts
Browser jobs through proxies are expensive on metered plans, because a rendered page pulls down far more than its HTML. Request interception is the common way to trim that, by blocking images, fonts or media the job doesn’t need.
Interception in Puppeteer is also per page, with page.setRequestInterception(true) and a request handler. Two things to know when you combine it with proxy authentication. Authentication already turns on interception internally, and Puppeteer handles both together, but if you write your own handler, every intercepted request must be continued, aborted or answered exactly once, or the page stalls. And if more than one handler wants a say on the same request, Puppeteer’s cooperative intercept mode exists for that case. Read the current docs for it before you rely on it.
Blocking assets saves bandwidth. It can also change what the page does. Some pages wait for fonts or images before rendering content, and some scripts behave differently when a resource fails. Test a blocked-asset page against a full load before you trust its output.
Across contexts, remember that there’s no shared cache. If five contexts all load the same heavy page, you pay for five downloads. For jobs that visit the same pages through several exits, for example to compare how a public page looks from different countries, it’s worth asking whether every exit really needs the full page, or whether a lighter check would do.
None of this changes how a target treats your traffic. Blocking images doesn’t make a job less visible, and a site that rate limits browser sessions is reacting to how many and how fast. The durable approach is pacing that respects the target’s published limits, and asking for access where a site offers it.
What to do before your next Puppeteer run
If you’re running Puppeteer through proxies now, a short review covers most of the risk.
Check your Puppeteer version and confirm createBrowserContext accepts proxyServer in it. If it doesn’t, upgrade before redesigning anything.
Decide the model on purpose. Many exits with short tasks points to contexts. Few exits, long heavy sessions or per-exit browser settings point to one browser per exit. A few browsers with a few contexts each is a reasonable middle.
Put each exit’s server address and credentials in one object, and build contexts and pages only from it. Authenticate every new page, including popups, or use IP allowlisting for exits where popups matter.
Wrap every unit of work in try and finally with a context close. Handle the browser’s disconnected event and requeue in-flight work. Recycle browsers on a schedule you’ve based on your own memory graphs.
Run the exit check in every context at startup, log it, and treat a duplicate or a direct address as a stop.
Finally, write down which exits the job uses and why, next to the code. When someone else picks the job up in six months, a list of exit names with their countries and session rules explains the design faster than any amount of reading the context helpers.
That’s the whole discipline. Contexts make multi-exit browser jobs lighter. They don’t make them simpler, and the savings only show up if cleanup is as careful as setup.
More setup guides for browser automation and proxies are on Proxy Scraping.
Get new guides and videos first — join the Telegram channel.