Skip to content

[bug] E2BTools creates sandbox eagerly in __init__ instead of lazily on first tool call #7215

@didodim

Description

@didodim

Description

E2BTools.__init__ calls Sandbox.create() immediately at construction time (line 49 in agno/tools/e2b.py). This causes several problems:

  1. Sandbox created before it's needed — if the agent never uses E2B tools in a session, the sandbox is wasted
  2. Multiple sandboxes on restart — every process restart creates new sandboxes while old ones linger until timeout (300s default)
  3. Cannot use with AgentOSAgentOS requires Agent instances at construction, which triggers E2BTools.__init__, which creates sandboxes before the app even starts serving requests
  4. No cleanup on shutdownE2BTools never calls sandbox.kill(), so sandboxes accumulate until timeout

Expected Behaviour

Sandbox should be created lazily on the first tool call (e.g., run_python_code, list_files, read_file_content), not in __init__. Something like:

def _get_sandbox(self):
    if self.sandbox is None:
        self.sandbox = Sandbox.create(api_key=self.api_key, timeout=self.timeout, **self.sandbox_options)
    return self.sandbox

Also, E2BTools should provide a close() or shutdown() method that kills the sandbox.

Environment

  • agno 2.5.11
  • e2b-code-interpreter 2.6.0
  • Python 3.14

Steps to Reproduce

  1. Create an Agent with E2BTools in the tools list
  2. Register it with AgentOS
  3. Observe that sandboxes are created immediately on startup, before any user request
  4. Restart the process — observe duplicate sandboxes in E2B dashboard

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions