diff --git a/docs/sandbox.md b/docs/sandbox.md index a6ae169a3..071f4b0cd 100644 --- a/docs/sandbox.md +++ b/docs/sandbox.md @@ -1,3 +1,6 @@ +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + # Sandbox / Code Execution Run model-generated code inside an isolated sandbox and get its output back. The API is provider-agnostic; e2b is the first supported backend, talked to directly over HTTPS with no extra SDK dependency. @@ -124,11 +127,50 @@ result = await litellm.acode_interpreter_tool( Behind the scenes the call goes directly to e2b's REST API: `POST api.e2b.app/sandboxes` to create, a streamed NDJSON `POST` to the per-sandbox host on port 49999 to execute, and `DELETE api.e2b.app/sandboxes/{id}` to tear down. -## LiteLLM Proxy: Responses API code interpreter interceptor +## Responses API code interpreter interceptor Route OpenAI's `code_interpreter` tool to your sandbox instead of OpenAI's container. The client request stays plain Responses API. -### Setup +### SDK + +Register the sandbox tool, install the interceptor as a callback, call `litellm.aresponses` with the `code_interpreter` tool unchanged. + +```python showLineNumbers title="sandbox_interceptor.py" +import os, litellm +from litellm.sandbox.sandbox_tools import register_sandbox_tools +from litellm.integrations.code_interpreter_interception.handler import ( + CodeInterpreterInterceptionLogger, +) + +os.environ["E2B_API_KEY"] = "e2b_..." +os.environ["OPENAI_API_KEY"] = "sk-..." + +register_sandbox_tools([ + { + "sandbox_tool_name": "my-e2b", + "litellm_params": { + "sandbox_provider": "e2b", + "api_key": "os.environ/E2B_API_KEY", + }, + } +]) + +litellm.callbacks = [ + CodeInterpreterInterceptionLogger( + enabled_providers=["openai"], + sandbox_tool_name="my-e2b", + ) +] + +response = await litellm.aresponses( + model="openai/gpt-5", + tools=[{"type": "code_interpreter", "container": {"type": "auto"}}], + input="Product of first 6 primes. Just the number.", +) +print(response.output_text) +``` + +### Proxy setup #### 1. Set keys @@ -169,6 +211,9 @@ litellm --config /path/to/config.yaml #### 4. Call `/v1/responses` + + + ```bash curl -s "http://localhost:4000/v1/responses" \ -H "Authorization: Bearer sk-1234" \ @@ -180,6 +225,25 @@ curl -s "http://localhost:4000/v1/responses" \ }' ``` + + + +```python +from openai import OpenAI + +client = OpenAI(api_key="sk-1234", base_url="http://localhost:4000/v1") + +response = client.responses.create( + model="gpt-5", + tools=[{"type": "code_interpreter", "container": {"type": "auto"}}], + input="Product of first 6 primes. Just the number.", +) +print(response.output_text) +``` + + + + Response contains a `code_interpreter_call` item with a `cntr_*` `container_id` wrapping the e2b sandbox id. ### Notes