When running AI Agent tasks at scale, you can reduce the cost of idle compute resources by configuring pause and resume through the E2B SDK or by modifying the Sandbox CR with kubectl. You can suspend sandbox instances on demand and resume them with their context fully restored to improve cluster resource utilization.
Prerequisites
Create an Agent Sandbox.
Pause a sandbox
Pause a sandbox instance to release its idle compute resources and reduce operational costs.
E2B SDK
Automatic pause
When creating a sandbox, you can configure the timeout parameter and enable the auto_pause parameter to cause the instance to automatically pause after an idle timeout.
# Import the E2B SDK
from e2b_code_interpreter import Sandbox
# Create a sandbox by using the beta API and configure it to auto-pause after 600 seconds of inactivity
sandbox: Sandbox = Sandbox.beta_create(
template="code-interpreter",
auto_pause=True,
timeout=600,
)
# Execute business operations
sandbox.run_code("print('hello, world')")Manual pause
After you confirm that the task is complete, you can manually call the pause API to pause the sandbox instance and release its compute resources. Replace the following <YOUR_SANDBOX_ID> with the actual sandbox id.
# Import the E2B SDK
from e2b_code_interpreter import Sandbox
sandbox = Sandbox.connect("<YOUR_SANDBOX_ID>")
sandbox.beta_pause()Sandbox CR
Replace <YOUR_RESOURCE_NAME> with the CR name and set spec.paused in the Sandbox instance configuration to true.
k******************** <YOUR_RESOURCE_NAME> -n default --type='merge' -p '{"spec":{"paused":true}}'After pausing a sandbox instance, you can verify its status. On the cluster details page, navigate to Workloads > Custom Resources and filter the list to view the paused instance.
Resume a sandbox
E2B SDK
You can quickly resume a paused sandbox instance with the connect API. Replace <YOUR_SANDBOX_ID> with your actual sandbox id.
# Import the E2B SDK
from e2b_code_interpreter import Sandbox
# Connect to the sandbox. If the sandbox is paused, it is automatically resumed with its context restored.
sandbox = Sandbox.connect("<YOUR_SANDBOX_ID>")
# Continue with your operations
sandbox.run_code("print('hello, world')")Sandbox CR
Replace <YOUR_RESOURCE_NAME> with the actual CR name, and in the Sandbox instance configuration, set spec.paused to false.
kubectl patch sandbox <YOUR_RESOURCE_NAME> -n default --type='merge' -p '{"spec":{"paused":false}}'After resuming a sandbox instance, you can check its running status. On the cluster details page, navigate to Workloads > Pods and filter the list to view the instance.
You can check the container logs to verify workload continuity.
Monitor sandbox status
A paused Pod does not consume cluster CPU or memory resources.
On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Operations > Prometheus Monitoring.
On the Prometheus Monitoring page, select the tab and filter for the target Pod to observe its status.
Delete a paused sandbox
To permanently remove a paused instance, delete its Sandbox CR. Replace <YOUR_RESOURCE_NAME> with the name of your CR.
kubectl -n default delete sandbox <YOUR_RESOURCE_NAME>