> ## Documentation Index
> Fetch the complete documentation index at: https://docs.runloop.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Devbox Lifetime Management

> Control devbox automatic shutdown and idle behaviors

<Tip>Start with the [Runloop Quickstart](/docs/tutorials/quickstart) to use the examples below.</Tip>

### Manual Stopping

You can stop a running Devbox any time using the `devbox.shutdown`
call in the client SDK.

<CodeGroup>
  ```python Python theme={null}
  devbox = await runloop.devbox.create()
  ...
  await devbox.shutdown()
  ```

  ```typescript TypeScript theme={null}
  const devbox = await runloop.devbox.create();
  ...
  await devbox.shutdown();
  ```
</CodeGroup>

### Devbox Max Lifetime

When you create a Devbox, it is automatically given a maximum
lifetime.  This prevents unwanted charges by leaving Devboxes running
when they are not needed. The default lifetime is 1 hour, but this can
be configured when you create the Devbox:

<CodeGroup>
  ```python Python theme={null}
  devbox = await runloop.devbox.create(
      launch_parameters={
          "keep_alive_time_seconds": 1800,
      },
  )
  ```

  ```typescript TypeScript theme={null}
  const devbox = await runloop.devbox.create({
    launch_parameters: {
      keep_alive_time_seconds: 1800,
    },
  });
  ```
</CodeGroup>

<Warning>
  Configure either `keep_alive_time_seconds` or an idle policy, not both. Use
  `keep_alive_time_seconds` when you want a fixed maximum lifetime for the Devbox.
  Use `lifecycle.after_idle` with `on_idle` when you want Runloop to act only after
  the Devbox becomes idle. Mixed configurations are not recommended for normal use:
  depending on the API or client path, the request may be rejected or the idle policy
  may take precedence over the fixed keep-alive timeout.
</Warning>

### Idle Behavior

You can also configure your Devboxes to automatically shutdown or
suspend when they are idle using the `lifecycle.after_idle` launch parameter.
By default, Runloop will do nothing if your Devbox is idle. `on_idle` only applies to `RUNNING` Devboxes and the action won't be taken if the Devbox is not in `RUNNING` state.

<CodeGroup>
  ```python Python theme={null}
  devbox = await runloop.devbox.create(
      launch_parameters={
          "lifecycle": {
              "after_idle": {"idle_time_seconds": 1800, "on_idle": "suspend"},
          }
      },
  )
  ```

  ```typescript TypeScript theme={null}
  const devbox = await runloop.devbox.create({
    launch_parameters: {
      lifecycle: {
        after_idle: { idle_time_seconds: 1800, on_idle: "suspend" },
      },
    },
  });
  ```
</CodeGroup>

### Network Access Control

You can restrict what network resources a Devbox can access by applying a [Network Policy](/docs/network-policies). This is useful for security, compliance, and controlling costs.

<CodeGroup>
  ```python Python theme={null}
  # Create a policy that only allows specific hosts
  policy = await runloop.network_policies.create(
      name="restricted-policy",
      allow_all=False,
      allowed_hostnames=["github.com", "api.openai.com"]
  )

  # Create a devbox with the network policy
  devbox = await runloop.devbox.create(
      launch_parameters={
          "network_policy_id": policy.id
      }
  )
  ```

  ```typescript TypeScript theme={null}
  // Create a policy that only allows specific hosts
  const policy = await runloop.networkPolicy.create({
      name: "restricted-policy",
      allow_all: false,
      allowed_hostnames: ["github.com", "api.openai.com"]
  });

  // Create a devbox with the network policy
  const devbox = await runloop.devbox.create({
      launch_parameters: {
          network_policy_id: policy.id
      }
  });
  ```
</CodeGroup>

See the [Network Policies documentation](/docs/network-policies) for more details on creating and managing policies.
