E2B icon indicating copy to clipboard operation
E2B copied to clipboard

Feature: per-session CPU/memory override in Sandbox.create()

Open manjunathbhaskar opened this issue 4 months ago • 1 comments

Summary

Sandboxes created from the same template share identical CPU and memory allocation set at template build time. There is no way to override resource limits at sandbox creation time via Sandbox.create() — even though the orchestrator gRPC proto already carries the vcpu and ram_mb fields end-to-end.

This gap was first raised in #1257 (closed April 2026 without implementation, redirected to read-only metrics). This issue proposes a concrete, minimal fix across the SDK and API layers.

Current behaviour

# The only way to control resources today:
# Build a separate template for each resource tier
# — no runtime override exists
sandbox = await Sandbox.create("my-template")  # always uses template's fixed Vcpu/RamMb

Desired behaviour

sandbox = await Sandbox.create(
    "my-template",
    cpu_count=2,       # override template default
    memory_mb=1024,    # override template default
)

Why this matters

  • Multi-tenant workloads: different tasks have very different resource requirements (a data analysis job needs more RAM than a simple shell command)
  • Cost control: code execution agents can now right-size each sandbox instead of provisioning a large template for all tasks
  • Security: a per-session limit bounds the blast radius of a runaway process; currently an OOM kill makes the entire sandbox unresponsive (issue #339) with no pre-emptive cap

Root cause

The infrastructure proto (orchestrator.proto, fields 26–27) already defines int64 vcpu and int64 ram_mb in SandboxConfig. The orchestrator already reads these fields. The gap is that the HTTP API layer hard-codes them from the template's build record rather than accepting them from the caller:

// packages/api/internal/orchestrator/create_instance.go
// Vcpu and RamMb always come from sbxData.Build, never from the HTTP request body

Proposed fix (minimal, two-repo)

e2b-dev/e2b (SDK + OpenAPI spec)

  1. spec/openapi.yml — the spec already defines reusable CPUCount and MemoryMB components; add them to NewSandbox schema as optional fields
  2. packages/python-sdk/e2b/sandbox_async/main.py — add cpu_count: int | None = None and memory_mb: int | None = None to AsyncSandbox.create()
  3. packages/python-sdk/e2b/sandbox_sync/main.py — same for sync variant
  4. Pass through to the generated API client in sandbox_api.py

e2b-dev/infra (API service layer)

  1. packages/api/internal/orchestrator/create_instance.go — if req.CpuCount != nil, use it instead of sbxData.Build.Vcpu; same for MemoryMb
  2. Add validation: cpu_count must not exceed the template's original allocation unless the team has the resource_override permission flag

No Firecracker or envd changes are needed — the VM's hard memory cap is already set at launch from the Vcpu/RamMb values in the orchestrator.

Willingness to contribute

Happy to submit a PR to e2b-dev/e2b covering the OpenAPI spec and Python SDK changes (steps 1–4). The e2b-dev/infra change (step 5) may need maintainer guidance on the validation/permission model.

manjunathbhaskar avatar Jul 21 '26 21:07 manjunathbhaskar

thanks, we are aware that users want this and it's currently on the roadmap

mishushakov avatar Jul 22 '26 21:07 mishushakov