Integrations
OpenAI Agents SDK
A function_tool bound to one sandbox, opened and killed around Runner.run.
Install
Terminal
pip install sandboxplease openai-agentsCode
Copy it as openai_agents.py. The model is the only part you would change.
openai_agents.py
"""OpenAI Agents SDK: give an agent a run_command tool backed by a SandboxPlease sandbox.
Run: SANDBOXPLEASE_API_KEY=... OPENAI_API_KEY=... uv run python openai_agents.py
"""
import asyncio
import json
from agents import Agent, Model, Runner, function_tool
from sandboxplease import AsyncSandbox, AsyncSandboxPlease
def sandbox_agent(sandbox: AsyncSandbox, model: str | Model = "gpt-5-mini") -> Agent:
@function_tool
async def run_command(command: str) -> str:
"""Run a shell command in a Linux sandbox with Python 3 and Node.
Args:
command: Shell command, e.g. python3 -c 'print(1+1)'.
"""
result = await sandbox.exec(command)
return json.dumps({"exit_code": result.exit_code, "stdout": result.stdout, "stderr": result.stderr})
return Agent(
name="Coder",
instructions="Use run_command to compute answers. Reply with the result only.",
tools=[run_command],
model=model,
)
async def solve(prompt: str, model: str | Model = "gpt-5-mini") -> str:
async with AsyncSandboxPlease() as sp, await sp.sandboxes.create(timeout=600) as sandbox:
result = await Runner.run(sandbox_agent(sandbox, model), prompt)
return str(result.final_output)
if __name__ == "__main__":
print(asyncio.run(solve("Compute 6*7 with Python in the sandbox.")))Run it
Terminal
export SANDBOXPLEASE_API_KEY=sp-v1-…
export OPENAI_API_KEY=…
python openai_agents.pyThe agent runs Python in the sandbox and answers 42. Each sandbox shows up in Logs with its provider and cost.