Sandboxes
SDK and CLI
Build, check and run Agent Playground flows from TypeScript, Python or the qr command line, and keep flows as JSON in your repository.
The TypeScript and Python SDKs and the qr command line wrap the Flows API. Install them as on the SDK, Python and CLI pages, and set QR_API_KEY.
Note
Flows and runs arrive in the next release of the SDKs and the
qrcommand line. Until then, use the HTTP calls.
Run a recipe, watch it, read the result
import { Sandboxes } from "@quietrobot/sandboxes";
const qr = new Sandboxes(); // reads QR_API_KEY
const flow = await qr.flows.create({ recipe: "fix-until-pass" });
const { forecast } = await qr.flows.simulate(flow.id);
console.log("1 in 10 runs cost more than", forecast.costUsd.p90);
const run = await qr.flows.run(flow.id, { guards: { maxLoops: 3 } });
const done = await qr.runs.wait(run.id, { onStep: (s) => console.log(s.label, s.status) });
console.log(done.status, done.loops, done.costUsd);from quietrobot_sandboxes import Sandboxes
qr = Sandboxes() # reads QR_API_KEY
flow = qr.flows.create(recipe="fix-until-pass")
forecast = qr.flows.simulate(flow["id"])["forecast"]
print("1 in 10 runs cost more than", forecast["costUsd"]["p90"])
run = qr.flows.run(flow["id"], guards={"maxLoops": 3})
run = qr.runs.wait(run["id"], on_step=lambda s, r: print(s["label"], s["status"]))
print(run["status"], run["loops"], run["costUsd"])A step that your Policies hold shows as waiting until a person decides in the dashboard. A run that needs more sandboxes at once than your plan allows throws a plan_limit error before anything starts.
Build a flow in code
const flow = await qr.flows.create({
name: "Fix my tests",
graph: {
start: "test",
secrets: ["ANTHROPIC_API_KEY"],
nodes: [
{ id: "test", kind: "run", label: "Run tests", cmd: "python3 -m pytest -q" },
{ id: "fix", kind: "agent", label: "Fix it", goal: "Make the tests pass. They said:\n{{steps.test.output}}", done: "python3 -m pytest -q" },
{ id: "done", kind: "end", label: "Fixed" },
],
edges: [
{ from: "test", to: "fix", when: "fail" },
{ from: "fix", to: "test" },
{ from: "test", to: "done", when: "pass" },
],
},
guards: { maxLoops: 4, timeSec: 600, budgetUsd: 1 },
});It opens in the builder like a flow you drew. The fields of each step are in Write a flow as JSON.
Check a flow first
validate runs the same checks as Run, without saving or starting anything: the flow itself, your plan's sandboxes at once, your keys, and the steps your Policies will likely hold. A flow that wouldn't run comes back with valid: false and its problems; it doesn't throw.
const check = await qr.flows.validate({ graph, guards }); // or { flow: "flw_123" } or { recipe: "pr-reviewer" }
if (!check.valid) {
for (const p of check.problems) console.log(`${p.code} at ${p.path ?? p.step ?? "the flow"}: ${p.message}`);
process.exit(1);
}
console.log(`${check.sandboxes.atOnce} of ${check.sandboxes.limit} sandboxes at once`, check.policy);check = qr.flows.validate({"graph": graph, "guards": guards}) # or flow="flw_123" or recipe="pr-reviewer"
for p in check["problems"]:
print(p["code"], p.get("path"), p["message"])The codes are listed on the Flows API page.
Keep flows in your repository
Export a flow as JSON, change it in your editor or in a pull request, check it, and save it back. The JSON is the same document the builder's JSON view shows.
import { readFileSync, writeFileSync } from "node:fs";
writeFileSync("flows/fix.json", JSON.stringify(await qr.flows.export("flw_123"), null, 2));
// later, in CI
const doc = JSON.parse(readFileSync("flows/fix.json", "utf8"));
const check = await qr.flows.validate(doc);
if (check.valid) await qr.flows.update("flw_123", doc);import json
with open("flows/fix.json", "w") as f:
json.dump(qr.flows.export("flw_123"), f, indent=2)
doc = json.load(open("flows/fix.json"))
if qr.flows.validate(doc)["valid"]:
qr.flows.update("flw_123", name=doc["name"], graph=doc["graph"], guards=doc["guards"])
copy = qr.flows.import_flow(doc, name="Fix my tests (copy)") # a new flow, here or in another organizationqr.flows.schema() returns the flow's JSON Schema, for tools that check or complete flow JSON.
The qr command line
qr recipes list
qr flows create --recipe fix-until-pass # prints the new flow's id
qr flows list
qr flows export flw_123 -o flows/fix.json # the flow as JSON
qr flows validate flows/fix.json # exits 1 and lists the problems when it wouldn't run
qr flows update flw_123 flows/fix.json # save your edits back
qr flows create flows/fix.json # or save them as a new flow
qr graph simulate --flow flw_123 --budget 0.10
qr flows run flw_123 --max-loops 3 --wait # prints each step as it happens
qr run list
qr run show run_123
qr run cancel run_123qr flows validate exits 1 when the flow wouldn't run, so it fits in a CI step:
- run: pip install quietrobot-sandboxes && qr flows validate flows/fix.json
env:
QR_API_KEY: ${{ secrets.QR_API_KEY }}Everything else
Both SDKs also have qr.recipes.list() and get, qr.flows.list(), get, delete, runs, versions, saveVersion (save_version in Python) and restore, qr.runs.create, list, get and cancel, and qr.graphs.simulate for a flow you haven't saved.