# 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/index.md)* ### [**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.thinkingmachines.ai/cookbook/api-reference/sandbox/sandboxresult/index.md)* ### [**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.thinkingmachines.ai/cookbook/api-reference/sandbox/sandboxresult/index.md)* ### [**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.thinkingmachines.ai/cookbook/api-reference/sandbox/sandboxresult/index.md)* ### [**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*