Skip to main content

Code interpreter interception

Code interpreter interception runs the code_interpreter tool in a sandbox that the gateway controls. When a Responses API or chat completions request includes the code_interpreter tool, the gateway gives the model a function tool instead, runs the code the model emits in a sandbox from sandbox_tools, feeds the output back to the model, and continues until the model answers. Responses API clients get the usual code_interpreter_call items in the output.

A real-world scenario​

An analyst sends spreadsheets to a model and asks for charts. The company does not want model-written code to run on a provider's servers or reach the internet. The admin turns on code interpreter interception with a sandbox from their own E2B account and internet access off, so code runs in a sandbox the gateway controls and is deleted after each request.

Steps​

  1. Add the sandbox_tools entry and the code_interpreter_interception callback from Configuration and restart the gateway. Code interpreter interception has no Admin UI control and no settings API, it is configured in YAML only.

  2. Send a Responses API request with the code_interpreter tool.

    curl http://localhost:4000/v1/responses \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{"model": "gpt-6.1-sol", "input": "What is 2 to the power of 64?", "tools": [{"type": "code_interpreter", "container": {"type": "auto"}}]}'
  3. Open Logs in the Admin UI and check the request and the follow-up model calls.

Expected result​

The output holds a code_interpreter_call item with the code and its result, followed by the model's answer.

Configuration​

model_list:
- model_name: gpt-6.1-sol
spanway_params:
model: openai/gpt-6.1-sol

sandbox_tools:
- sandbox_tool_name: my-e2b
spanway_params:
sandbox_provider: e2b
api_key: os.environ/E2B_API_KEY

spanway_settings:
callbacks: ["code_interpreter_interception"]
code_interpreter_interception_params:
enabled_providers: ["openai"]
sandbox_tool_name: my-e2b
allow_internet_access: false
SettingMeaning
enabledTurns interception on or off. Defaults to true.
enabled_providersProvider names whose requests are intercepted. All providers when unset.
sandbox_tool_nameSandbox tool from sandbox_tools that runs the code. Supported providers are e2b and opensandbox.
allow_internet_accessLets code in the sandbox reach the internet. Defaults to false.

Sandbox network access​

Interception sandboxes are created with internet access turned off, so code written by the model cannot download packages, call external services or send data out of the sandbox. On E2B the gateway creates the sandbox with allow_internet_access set to false, and on OpenSandbox it applies a network policy that denies all egress.

Set allow_internet_access: true only when your workloads need it. Only the YAML boolean true turns it on, and any other value keeps the sandbox offline. With access on, model generated code can reach any host on the internet from the sandbox. The gateway does not yet pass a per-domain egress allowlist to the sandbox provider, so any narrower policy has to be enforced outside gateway config, for example on your OpenSandbox server or in your network.

Sandbox lifetime​

Each request gets its own sandbox, which is deleted when the request finishes. A request that carries a session_id in its metadata reuses one sandbox across requests from the same key for that session, and two keys that send the same session_id always get separate sandboxes. A request with a session_id that the gateway cannot tie to a key gets a fresh sandbox, so a session is never shared between callers.

Session sandboxes are kept in the memory of the gateway instance that created them. A session sandbox idle for more than 15 minutes is deleted the next time that instance handles a code interpreter request, not on a timer, and the limit of 10 session sandboxes per key applies on each instance separately, deleting the least recently used one when the key opens another. With several instances behind a load balancer, requests of one session can land on different instances and each instance then keeps its own sandbox for it.

Errors​

What you seeWhat it meansFix
The request goes to the provider's own code interpreterThe provider is not in enabled_providers, or enabled is false.Add the provider, or leave enabled_providers unset for all.
The request fails with "CodeInterpreterInterception: no sandbox available. Provide a sandbox_config or configure a sandbox tool resolvable via sandbox_tool_name."sandbox_tool_name names no entry in sandbox_tools.Fix the name or add the entry.
The request fails with the sandbox provider's errorThe sandbox could not be created, usually a wrong E2B key or OpenSandbox address. The whole request fails, no tool result is returned.Fix sandbox_tools.
A tool result that starts with [execution error]The sandbox exists, but the code the model wrote failed. The model sees the error and can try again.Nothing to fix on the gateway.
Code cannot install packagesInternet access is off by default.Set allow_internet_access: true only if the workload needs it.

Logs​

LevelLineWhat it meansAction
ERRORCodeInterpreterInterception: failed to delete sandbox containerA sandbox could not be deleted after a request, so it keeps running at the sandbox provider until the provider's own timeout.Check the sandbox provider key and remove leftover sandboxes in the provider's console.