Create ephemeral, secure execution environments within your functions
Sandboxes are short-lived, isolated environments that you can spin up instantly for code execution. Sandboxes can be deployed within Buildfunctions (via CPU or GPU Functions) or deployed in app code anywhere else (e.g., local scripts, Next.js apps, external workers).
GPUSandbox provides instant access to secure, hardware-isolated VMs with GPUs. They include automatic storage for self-hosted models (perfect for agents) and support concurrent requests on the same GPU for significant cost savings.
You have the option to manually call delete() to clean up a Sandbox when you’re ready.If you don’t call delete(), the sandbox will be automatically cleaned up after the period you set for the timeout argument.
Default Timeout: If you don’t set a timeout argument, the default is 1 minute.
Auto-Cleanup: The sandbox is destroyed automatically after the timeout expires.
One of the most powerful features of Buildfunctions is Nested Orchestration. You can deploy a top-level Function (e.g., a Node.js API) that spins up child Sandboxes (e.g., Python GPU workers) to handle requests.
This example demonstrates an advanced agentic workflow: Code Generation with Reward Scoring. The agent uses Claude to generate a Python function, then immediately spins up a secure CPUSandbox to test the code against a set of unit tests (the reward function). This allows the agent to verify the correctness of its output before proceeding.
This example requires an ANTHROPIC_API_KEY to be available in your environment.
Copy
import osimport reimport timefrom pathlib import Pathimport anthropicimport pytestfrom dotenv import load_dotenvfrom buildfunctions import Buildfunctions, CPUSandboxload_dotenv()API_TOKEN = os.environ.get("BUILDFUNCTIONS_API_TOKEN", "")HANDLER_TEMPLATE = (Path(__file__).parent / "reward_handler.py").read_text()def strip_markdown_fences(text: str) -> str: return re.sub(r"^```[\w]*\n|```$", "", text.strip(), flags=re.MULTILINE).strip()@pytest.mark.asyncioasync def test_code_generation_with_reward(): if not API_TOKEN: pytest.skip("Set BUILDFUNCTIONS_API_TOKEN in .env file") print("Testing Code Generation with Reward Scoring...\n") sandbox = None try: # Step 1: Authenticate print("1. Authenticating...") client = await Buildfunctions({"apiToken": API_TOKEN}) print(f" Authenticated as: {client.user.username}") # Step 2: Generate code with Claude print("\n2. Generating sorting function with Claude...") claude = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_KEY")) response = claude.messages.create( model="claude-opus-4-6", max_tokens=512, messages=[{ "role": "user", "content": ( "Write a Python function called `sort_list` that takes a list and " "returns it sorted. Handle edge cases like empty lists, single elements, " "and mixed types. Return ONLY the code, no markdown, no explanations." ), }], ) generated_code = strip_markdown_fences(response.content[0].text) print(f" Generated code:\n{generated_code}\n") # Step 3: Create CPU Sandbox with reward function print("3. Creating CPU Sandbox with reward function...") handler_code = HANDLER_TEMPLATE.format(generated_code=generated_code) sandbox = await CPUSandbox.create({ "name": f"reward-eval-{int(time.time())}", "language": "python", "code": handler_code, "memory": "512MB", "timeout": 30, }) print(f" CPU Sandbox created: {sandbox.name}") # Step 4: Run the reward function print("\n4. Running reward function...") result = await sandbox.run() print(f" Result: {result.response}") # Step 5: Clean up print("\n5. Deleting CPU Sandbox...") await sandbox.delete() print(" CPU Sandbox deleted") print("\nCode generation with reward scoring test completed!") except Exception: if sandbox and sandbox.delete: try: await sandbox.delete() except Exception as e: print(f"Cleanup failed: {e}") raiseif __name__ == "__main__": import asyncio asyncio.run(test_code_generation_with_reward())