---
title: "Keep alive"
description: "Keep sessions alive across disconnects, allowing you to reconnect without ending your session."
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.

# Keep alive

The `keepAlive` feature lets you **keep sessions alive across disconnects**, so you can reconnect and continue using the same session.

|                        | When connection closes                   |
| ---------------------- | ---------------------------------------- |
| **Without keep alive** | Session ends                             |
| **With keep alive**    | Session stays available for reconnection |

> **Note**
>
> Session keep alive is only available on plans that include it.

## Why use keep alive?

* **Reconnection workflows**: Connect, disconnect, and reconnect to the same session
* **Multiple connections**: Different scripts or tools can connect to the same session
* **Survive disconnects**: Network issues won't end your session
* **Performance**: Reconnecting to an existing session is faster than creating a new one
* **Session reuse**: Re-run scripts against the same session without recreating it
* **Billing optimization**: keep alive lets you avoid minimum browser usage charges if running many short-lived sessions (under 1 minute)

## Using keep alive

### Creating a keep alive session

Setting `keepAlive` to `true` keeps the session available for later use. Reconnect to the keep alive session using the same connect URL.

### Node.js

```typescript SDK
  import Browsers from "agentmq";

  const browsers = new Browsers({ apiKey: process.env.AGENTMQ_TOKEN! });
  const session = await browsers.sessions.create({
    keepAlive: true,
  });

  // Reconnect later using the same connect URL
  console.log("Connect URL:", session.connectUrl);
```

### Python

```python SDK
  from agentmq.browsers import Browsers
  import os

  browsers = Browsers(api_key=os.environ["AGENTMQ_TOKEN"])
  session = browsers.sessions.create(
    keep_alive=True
  )

  # Reconnect later using the same connect URL
  print("Connect URL:", session.connect_url)
```

### Releasing a keep alive session

Keep alive sessions must be explicitly released to stop; otherwise, they'll continue until the session's timeout.

> **Note**
>
> Release keep alive sessions when you're done to avoid being charged for unused browser minutes.

### Node.js

```typescript SDK
  await browsers.sessions.update(session.id, {
    status: "REQUEST_RELEASE",
  });
```

```typescript API
  const headers = {
    "Authorization: Bearer": "<your-api-key>",
    "Content-Type": "application/json",
  };

  const body = JSON.stringify({
    status: "REQUEST_RELEASE",
  });

  fetch("https://api.agent.mq/v1/browsers/sessions/<session-id>", {
    method: "POST",
    headers,
    body,
  })
    .then((response) => response.json())
    .then((response) => console.log(response))
    .catch((err) => console.error(err));
```

### Python

```python SDK
  browsers.sessions.update(
    session.id,
    status="REQUEST_RELEASE"
  )
```

```python API
  import os
  import requests

  headers = {"Authorization: Bearer": os.environ["AGENTMQ_TOKEN"]}
  json = {
      "status": "REQUEST_RELEASE",
  }

  response = requests.post(
      f"https://api.agent.mq/v1/browsers/sessions/<session-id>",
      headers=headers,
      json=json,
  )

  # Raise an exception if there wasn't a good response from the endpoint
  response.raise_for_status()
  print(response.json())
```

## Keep alive and timeouts

Keep alive sessions still respect [session timeouts](/browsers/long-sessions/timeouts). If you need a session that both survives disconnects and runs for an extended period, configure both options:

```typescript
const session = await browsers.sessions.create({
  keepAlive: true,
  timeout: 3600, // 1 hour
});
```

- **Session timeouts** — [Learn how to extend session duration beyond the default timeout.](/browsers/long-sessions/timeouts)

Source: https://help.agent.mq/browsers/long-sessions/keep-alive/index.mdx
