letta-ai / letta

Letta (formerly MemGPT) is a framework for creating LLM services with memory.
https://letta.com
Apache License 2.0
12.88k stars 1.41k forks source link

feat: Sandboxing for tool execution #2040

Open mattzh72 opened 1 week ago

mattzh72 commented 1 week ago

Description

Enable sandboxing for tool execution. Previously, tool execution was being done in the same runtime environment/thread as the agent control loop - this is problematic for obvious reasons. We introduce the ability to run tools in a sandboxed environment:

We also add the ability for users to manage sandbox configurations:

class E2BSandboxConfig(BaseModel): timeout: int = Field(5 * 60, description="Time limit for the sandbox (in seconds).") template_id: Optional[str] = Field(None, description="The E2B template id (docker image).")


- Add environment variables per sandbox configuration

There's also some nifty optimizations around E2B, such as not immediately killing the sandbox so sandboxes can get reused (save on spin-up time), and only refreshing the sandbox when we detect the user has changed either the config or environment variables for that box. 

In a separate PR (merged into this one), we also add the ability to modify agent state via tools by serializing the agent state and passing back and forth between the sandbox and running Python thread.

## Testing
- A suite of unit tests that cover the client functionality (both local and REST)
- A suite of live integration tests with E2B covering happy paths + edge cases where the config changed in the middle of execution, differing environments compared to local, etc.
- Manual testing on the dev portal

## Contributions
Thanks to @carenthomas for contributing, this PR is based off her initial PR!