# tinker\_cookbook.sandbox.ModalSandbox

## _class_ [**tinker\_cookbook.sandbox.ModalSandbox**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L73)()

Persistent Modal sandbox for code execution. Conforms to SandboxInterface.

**Usage:**
sandbox = await ModalSandbox.create()

await sandbox.write\_file("/workspace/code.py", "print('hello')")
result = await sandbox.run\_command("python /workspace/code.py")
print(result.stdout)

await sandbox.cleanup()

### [**create**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L102)( _app\_name_, _timeout_, _image_, _max\_stream\_output\_bytes_)

Create a new Modal sandbox.

**Parameters:**

- [**app\_name**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L104) ( _str_)
- [**timeout**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L105) ( _int_)
- [**image**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L106) ( _modal.Image \| None_)
- [**max\_stream\_output\_bytes**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L107) ( _int_)

**Returns:** _[ModalSandbox](https://tinker-docs.thinkingmachines.ai/cookbook/api-reference/sandbox/modalsandbox/)_

### [**run\_command**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L133)( _command_, _workdir_, _timeout_, _max\_output\_bytes_)

Run a shell command in the sandbox.

**Parameters:**

- [**command**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L135) ( _str_)
- [**workdir**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L136) ( _str \| None_)
- [**timeout**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L137) ( _int_)
- [**max\_output\_bytes**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L138) ( _int \| None_)

**Returns:** _[SandboxResult](https://tinker-docs.thinking-machines.ai/cookbook/api-reference/sandbox/sandboxresult/)_

### [**read\_file**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L157)( _path_, _max\_bytes_, _timeout_)

Read a file from the sandbox.

**Parameters:**

- [**path**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L158) ( _str_)
- [**max\_bytes**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L158) ( _int \| None_)
- [**timeout**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L158) ( _int_)

**Returns:** _[SandboxResult](https://tinker-docs.thinking-machines.ai/cookbook/api-reference/sandbox/sandboxresult/)_

### [**write\_file**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L167)( _path_, _content_, _executable_, _timeout_)

Write content to a file in the sandbox.

**Parameters:**

- [**path**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L169) ( _str_)
- [**content**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L170) ( _str \| bytes_)
- [**executable**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L171) ( _bool_)
- [**timeout**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L172) ( _int_)

**Returns:** _[SandboxResult](https://tinker-docs.thinking-machines.ai/cookbook/api-reference/sandbox/sandboxresult/)_

### [**cleanup**](https://github.com/thinking-machines-lab/tinker-cookbook/blob/main/tinker_cookbook/sandbox/modal_sandbox.py#L211)()

Terminate the Modal sandbox and wait for it to fully shut down.

**Returns:** _None_
