You can set a timeout when creating a Sandbox, and you can adjust the timeout after the Sandbox has been created. A reasonable timeout helps prevent idle Sandboxes from occupying resources for too long.
Configure at creation time
Python example:
import os
sandbox = Sandbox.create(
template="code-interpreter-v1",
timeout=300,
api_key=os.environ["E2B_API_KEY"],
api_url=os.environ["E2B_API_URL"],
domain=os.environ["E2B_DOMAIN"],
)
TypeScript example:
const sandbox = await Sandbox.create("code-interpreter-v1", {
timeoutMs: 300_000,
apiKey: process.env.E2B_API_KEY,
apiUrl: process.env.E2B_API_URL,
domain: process.env.E2B_DOMAIN,
});
Adjust After Creation
After creation, you can continue to adjust the timeout of the current Sandbox:
sandbox.set_timeout(600)
You can also adjust the timeout of a specific Sandbox through the static method:
Sandbox.set_timeout(sandbox_id, 600)
Notes
-
The Python SDK and TypeScript SDK use different timeout units. The Python examples typically use seconds, while the TypeScript examples use milliseconds.
-
If the timeout is too short, tasks may end prematurely. If it is too long, resource usage and cost risk increase.
-
Even with a timeout configured, you should still call
sandbox.kill()explicitly when the task is complete.