
Code Interpreter Interceptor: route code execution to a self-hosted sandbox instead of OpenAI’s
快速结论:此问题为功能提案,而非报错。用户希望 LiteLLM 增加 Code Interceptor 功能,将 OpenAI Code Interpreter 的代码执行路由到自托管的沙箱(如 OpenSandbox、e2b、Daytona)中,而不是 OpenAI 的托管容器。目前该功能尚未实现,优先排查是否有自定义 Logger 实现或代理配置支持此路由。
问题场景
用户在使用 LiteLLM 代理时,客户端发送了 tools: [{ "type": "code_interpreter", "container": { "type": "auto" } }] 请求。默认情况下,代码会在 OpenAI 管理的容器中执行。管理员希望将代码执行重定向到自己的基础设施(如 VPC 内的自托管沙箱),以满足数据合规、成本控制、自定义环境或跨模型兼容性需求。
报错原文
This ticket proposes a Code Interpreter Interceptor that detects code interpreter tool calls and runs the generated code in an admin-chosen sandbox backend (OpenSandbox, e2b, Daytona, or other) instead of OpenAI's sandbox, while keeping the request and response shape that clients expect.
原因分析
这是功能缺失问题,而非错误。LiteLLM 已为 web_search 工具实现了拦截器(Web Search Interceptor),但尚未提供等效的 Code Interpreter Interceptor。当前代码执行只能通过 OpenAI 的托管容器完成,管理员无法控制执行后端。
环境排查
- LiteLLM 版本:确认使用的 LiteLLM 版本是否已包含
litellm/integrations/websearch_interception/模块(该模块是功能提案的参考模板) - 代理配置:检查
callbacks配置中是否已注册自定义拦截器(CustomLogger) - Python 环境:如果使用 e2b 后端,需要安装
e2b-code-interpreter包(功能提案中作为可选依赖) - 沙箱后端:确认 OpenSandbox、e2b 或 Daytona 等沙箱服务已部署并可访问
解决步骤
- 阅读功能提案:详细阅读 Issue 中的实现计划(Implementation plan v1),了解功能开发分为多个阶段:第一版只实现执行原语(
litellm.aexecute_code()/litellm.execute_code()),不包含代理端点、拦截器或文件上传。 - 等待后续 PR:关注 LiteLLM 仓库中与
Code Interpreter Interceptor相关的 PR,第一版 PR 预计使用e2b-code-interpreterSDK 实现无状态代码执行。 - 优先尝试已有拦截器模式:如果当前需要将代码执行路由到自托管沙箱,可以参考 Web Search Interceptor 的实现(
litellm/integrations/websearch_interception/),自定义CustomLogger实现类似逻辑。这是 Issue 中讨论的多阶段方案的第一步。 - 联系维护者:在 Issue 中回复,提供使用场景和需求,帮助推动功能实现。
注意:目前没有可立即执行的修复步骤,因为功能尚未实现。建议跟踪 Issue 进度并参与讨论。
验证方法
功能实现后,通过以下方式验证:
- 分层验证(EKS runbook 中的方案):
- Layer 0:确认 OpenSandbox 可访问性
- Layer 1:通过 Containers API(
POST /v1/containers等端点)测试容器生命周期,不涉及模型调用 - Layer 2:使用 GPT-5 的
code_interpreter工具并指定显式容器 - Layer 3:完整 auto-container 拦截器路径
- Layer 4:确认日志中显示三条关联记录(原始调用、代码执行、后续调用)且共享同一请求 ID
- 日志检查:使用
http://localhost:4000/ui/?page=logs检查日志,确认容器创建和代码执行日志与父请求关联,且未意外捕获代码正文或标准输出(除非显式开启) - 负向控制:将
api_base指向无法访问的地址,验证不会静默回退到 OpenAI



