---
title: "Manage a browser session"
description: "Learn how to manage session termination and inspect completed sessions"
image: "https://help.agent.mq/og.svg"
---

> Documentation Index
> Fetch the complete documentation index at: https://help.agent.mq/llms.txt
> Use this file to discover all available pages before exploring further.

# Manage a browser session

While Browsers automatically handles session termination when you disconnect, understanding how sessions end helps you debug failed runs, manage long-running sessions, optimize resource usage, and investigate timeouts or errors.

## Session termination

Browser sessions can end in these ways:

1. **Automatic timeout**
   Sessions have a default timeout configured at the application level, which can be customized when creating a session. For longer-running tasks, enable [keep alive](/browsers/long-sessions/overview).

2. **Manual termination**
   You can end sessions explicitly by closing the browser programmatically (`browser.close()` or `driver.quit()`), using the Sessions API, or releasing keep-alive sessions when no longer needed.

3. **Unhandled errors**
   Unhandled errors in your automation code can cause your script to disconnect from the browser, ending the session prematurely. Common scenarios include network interruptions, uncaught exceptions, or exceeded resource limits.

   To prevent premature termination, make sure to implement proper error handling and cleanup in your code.

4. **CDP inactivity timeout**
   CDP (Chrome DevTools Protocol) connections are closed after 10 minutes without any CDP commands. If your script holds a session open without sending CDP commands for this period, the connection will be terminated.

   To keep the connection alive, send a periodic heartbeat, a lightweight CDP command at a regular interval:

```javascript Node.js
 // Send a heartbeat every 5 minutes to prevent CDP inactivity timeout
 setInterval(async () => {
   await page.evaluate(() => undefined);
 }, 5 * 60 * 1000);
```

```python Python
 import asyncio

 # Send a heartbeat every 5 minutes to prevent CDP inactivity timeout
 async def heartbeat(page):
     while True:
         try:
             await asyncio.sleep(5 * 60)
             await page.evaluate("undefined")
         except Exception:
             break

 asyncio.create_task(heartbeat(page))
```

## Session timeout settings

Configure timeouts at two levels:

**Application level**
Set the default timeout for all sessions in your [application settings](https://agent.mq/settings). This acts as the fallback when no session-specific timeout is set.

**Session level**
Override the application timeout for specific sessions when [creating them](/browsers/getting-started/create-browser-session#configuration-options). This gives you fine-grained control over individual session durations.

## Debugging completed sessions

The [Session Inspector](/browsers/observability/observability) is your primary tool for analyzing completed sessions:

- **Session Replays** — [Record and replay browser activity to understand what happened](/browsers/observability/session-replay)

- **Network Monitor** — [Inspect HTTP traffic, responses, and timing](/browsers/observability/observability#network)

- **Console & Logs** — [Review JavaScript output and debug messages](/browsers/observability/observability#console)

- **Performance** — [Track CPU, memory usage, and other metrics](/browsers/observability/observability#performance)

## Measuring usage

Track and analyze your browser session usage through multiple interfaces:

**console**
Your central hub at [agent.mq/overview](https://agent.mq/overview) shows total browser minutes, active sessions, usage trends, and billing information.

**Sessions list**
Browse your session history at [agent.mq/sessions](https://agent.mq/sessions) to view duration, status, and resource consumption for individual sessions.

For programmatic access to these metrics, see the Measuring Usage Guide.

Source: https://help.agent.mq/browsers/getting-started/manage-browser-session/index.mdx
