Developer docs / AI Fabric tutorials
Vibe coding for thousands of users
- Sandbox
- Batch / map
- Image DSL
One sandbox is a code interpreter. A thousand sandboxes is a product. When you let many users run their own code (a vibe-coding app, a notebook backend, an autograder), every request needs its own isolated environment, and they all want to run at once. The question stops being "how do I sandbox code" and becomes "how do I sandbox code for everyone, in parallel."
In this tutorial you'll fan out one sandbox per user request with .map(), so each user gets a fully isolated box and they all execute simultaneously. You'll also set up a bladekit.Queue for back-pressure, the piece that lets you absorb bursts of traffic without overrunning the fleet.
Before you start
You need the AI Fabric CLI installed and authenticated: run blade setup once. No GPU is required: user code runs on CPU. The sandbox environment is a small debian_slim image with NumPy and requests.
Set up the app, image, and queue
Create vibe_coding_scale.py. Alongside the sandbox image, declare a named bladekit.Queue, a durable work queue you can use to buffer jobs when traffic spikes faster than the fleet can absorb it:
import bladekit
app = bladekit.app("vibe-coding-scale")
sandbox_image = (
bladekit.Image.debian_slim(python_version="3.12")
.pip_install("numpy", "requests")
)
job_queue = bladekit.Queue.from_name("coding-jobs", create_if_missing=True)Run one user's job in a sandbox
Write the per-user worker as an ordinary AI Fabric function. It takes a single job (a user id and their code), runs the code in a throwaway sandbox, and returns the result tagged with the user id so you can match outputs back to callers:
@app.function(timeout=120)
def execute_for_user(job: dict) -> dict:
"""Run one user's code in an isolated sandbox."""
user_id = job["user_id"]
code = job["code"]
with bladekit.Sandbox.create(image=sandbox_image, timeout=30) as sb:
sb.write_file("/work/solution.py", code.encode())
proc = sb.exec("python", "/work/solution.py")
proc.wait()
return {
"user_id": user_id,
"exit_code": proc.returncode,
"output": proc.stdout.read().decode()[:1000],
}Each call gets its own sandbox, so users are isolated from each other at the kernel level: one user's code can't see, touch, or interfere with another's.
Fan out across users
The scaling is one line. Build a list of jobs and hand it to execute_for_user.map(...). AI Fabric launches a separate sandbox for each job and runs them in parallel. return_exceptions=True means one user's failure never cancels the rest:
@app.local_entrypoint()
def main():
# Simulate 50 user jobs
jobs = [
{"user_id": f"user-{i}", "code": f"print('Hello from user {i}!'); print({i} ** 2)"}
for i in range(50)
]
# Fan out: each job runs in its own sandbox in parallel
results = list(execute_for_user.map(jobs, return_exceptions=True))
ok = sum(1 for r in results if not isinstance(r, Exception) and r["exit_code"] == 0)
print(f"{ok}/{len(results)} jobs succeeded")Run it
Execute the program with the AI Fabric CLI:
blade run vibe_coding_scale.py50/50 jobs succeededAll 50 jobs run at once, each in its own isolated sandbox, and you get a tidy success count back. To go from 50 users to thousands, make the job list longer and AI Fabric provisions more containers automatically. And when traffic is bursty rather than batched, front the workload with the bladekit.Queue: producers enqueue jobs, workers dequeue and process, and the queue smooths the spikes.
Take it for a spin
Everything above in one file, ready to run.
import bladekit
app = bladekit.app("vibe-coding-scale")
sandbox_image = (
bladekit.Image.debian_slim(python_version="3.12")
.pip_install("numpy", "requests")
)
job_queue = bladekit.Queue.from_name("coding-jobs", create_if_missing=True)
@app.function(timeout=120)
def execute_for_user(job: dict) -> dict:
"""Run one user's code in an isolated sandbox."""
user_id = job["user_id"]
code = job["code"]
with bladekit.Sandbox.create(image=sandbox_image, timeout=30) as sb:
sb.write_file("/work/solution.py", code.encode())
proc = sb.exec("python", "/work/solution.py")
proc.wait()
return {
"user_id": user_id,
"exit_code": proc.returncode,
"output": proc.stdout.read().decode()[:1000],
}
@app.local_entrypoint()
def main():
# Simulate 50 user jobs
jobs = [
{"user_id": f"user-{i}", "code": f"print('Hello from user {i}!'); print({i} ** 2)"}
for i in range(50)
]
# Fan out: each job runs in its own sandbox in parallel
results = list(execute_for_user.map(jobs, return_exceptions=True))
ok = sum(1 for r in results if not isinstance(r, Exception) and r["exit_code"] == 0)
print(f"{ok}/{len(results)} jobs succeeded")Concepts used: Cold start, Run