The Sandbox SDK provides methods to inspect, extend, reconnect, and terminate your sandbox environments.

Inspecting Sandbox Info

info = sdk.get_sandbox_info()
print(f"Sandbox ID: {info['sandbox_id']}")
print(f"Expires at: {info['end_at']}")

Extending Sandbox Timeout

# Extend the sandbox lifetime by 30 minutes (1800 seconds)
sdk.extend_sandbox_timeout(1800)

# Verify new expiration time
info = sdk.get_sandbox_info()
print(f"New expiration: {info['end_at']}")

Terminating a Sandbox

result = sdk.kill_sandbox()
if result['success']:
    print('Sandbox terminated successfully.')
else:
    print(f"Error terminating sandbox: {result['error']}")

Reconnecting to an Existing Sandbox

You can save the sandbox ID and reconnect without losing state:

# Save the sandbox ID for later
sandbox_id = sdk.sandbox_id

# Reconnect to the same sandbox
from cloudcode import SandboxSDK
import os

reconnected = SandboxSDK(
    model="gpt-4.1",
    api_key=os.getenv("CLOUDCODE_API_KEY"),
    sandbox_id=sandbox_id
)

# Inspect again
info = reconnected.get_sandbox_info()
print(info)

Best Practices

  • Use appropriate timeouts: set initial sandbox_timeout based on usage and extend only when necessary
  • Clean up when done: always call kill_sandbox() to free resources
  • Handle errors: check return values of all management methods
  • Organize files: use consistent sandbox paths (e.g., /home/user/project)
  • Security: avoid storing sensitive credentials in sandbox files

Troubleshooting

Files Not Found

If a file is missing or inaccessible:

# Force-create the missing file
sdk.run_command("touch /home/user/missing_file.py")

Connection or Timeout Issues

If reconnecting fails or sandbox expires unexpectedly:

# Increase timeout and retry
sdk = SandboxSDK(
    model="gpt-4.1",
    api_key=os.getenv("CLOUDCODE_API_KEY"),
    sandbox_timeout=1200  # 20 minutes
)

If the sandbox becomes unresponsive, terminate and create a new one:

sdk.kill_sandbox()
new_sdk = SandboxSDK(model="gpt-4.1", api_key=os.getenv("CLOUDCODE_API_KEY"))