Learned code
Research previewTurn what Sai did into Simulang code you own, run it without the agent, and repair it when it breaks.
Sai solves a task by looking at the screen and deciding each step. Once it has solved a task, you can turn what it did into a Simulang function and run that function again with new inputs, without the agent. The code is faster and cheaper to run, and you can read it, test it and keep it under version control. When the code breaks, Sai repairs it from what went wrong.
Try this first, before the details. With Sai added to your coding agent (Claude Code, Codex or Cursor), ask it:
Use Sai to open TechCrunch, find all AI news published today, write their headlines into Notepad and save the file as techcrunch_headlines.txt, then turn what it did into code I can rerun.The loop has four calls. You can run each one yourself, or let the API run the full algorithm:
-
Record. Send the task to
POST /messagewithoptions.recordTrajectory: true, ideally as a template with inputs, so the inputs are named:Record a taskcurl -s "$SAI_API_URL/v1/agents/message" \ -H "Authorization: Bearer $SAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "message": "Open {{newsSite}}, find all {{topic}} news published today, write their headlines into Notepad, one per line, and save the file as {{fileName}}", "options": { "inputs": { "newsSite": "TechCrunch", "topic": "AI", "fileName": "techcrunch_headlines.txt" }, "recordTrajectory": true }, "wait": false }' -
Learn.
POST /v1/agents/learnwith the turn's id returnsasync function main(params = { newsSite: "...", topic: "...", fileName: "..." }), the template and the parameters. A clarification you sent during the task, or a follow-up turn, can be included by listing its turn too. -
Run. Open a control lease and run the code with new inputs:
await main({ "newsSite": "TechCrunch", "topic": "Fintech", "fileName": "techcrunch_fintech.txt" })as the block's last line. The block's result is whatmainreturns. The code throwsneeds_sign_in: <site>when the computer is not signed in, andprecondition_failed: ...when it starts on the wrong screen. -
Judge and repair. Check a run yourself, or with
POST /v1/agents/judge. When it failed, calllearnagain with the code asinitialCodeand the run as an evaluation. If the code cannot handle a case at all, send the template toPOST /messagewith those inputs, let Sai solve it, and pass that new turn tolearn: the repair adds what the code was missing.
In the SDKs:
learned = sai.learn.create([turn])
with sai.control.open(machine=pc) as lease:
r = lease.run(learned, {"newsSite": "TechCrunch", "topic": "Fintech", "fileName": "techcrunch_fintech.txt"})
if not r.ok:
learned = sai.learn.create(
initial_code=learned.code,
evaluations=[Evaluation(inputs={"newsSite": "TechCrunch", "topic": "Fintech", "fileName": "techcrunch_fintech.txt"}, result=r, passed=False)],
)Let Sai run the loop
One call can do all four steps on a computer: learn with maxIterations runs the agent on your task (or starts from your recorded turns), converts its run, then runs the code several times to test Pass^k reliability, judges each run, and repairs the code after a failure, until a version passes every run or the versions run out. It answers with a learn job you read until it ends.
job = sai.learn.create(
task_template="Open {{newsSite}}, find all {{topic}} news published today, write their headlines into Notepad, one per line, and save the file as {{fileName}}",
inputs={"newsSite": "TechCrunch", "topic": "AI", "fileName": "techcrunch_headlines.txt"},
criteria="techcrunch_headlines.txt is saved and holds only the headlines of the AI articles TechCrunch published today, one per line, with no text left from earlier runs",
max_iterations=3,
rollouts=3,
)
done = sai.learn.wait(job)
print(done.test_result) # passed 3 of 3 runs, on these inputs
print(done.code)The example sends no reset, so the runs go back to back: each starts where the last one left off, with Notepad still open and the file already saved. That is how the code starts when you rerun it, so a version that only works from a clean screen fails a run and gets repaired. When every real run starts from a known state, send a reset that sets it up: Simulang code, such as reset={"code": "for (const w of await machine.getWindow(\"Notepad\")) await w.close()"}, or a task for the agent, such as reset={"task": "Close every Notepad window"}. A task needs no code, but it is slower, billed like an agent run, and itself not always reliable. Each run's record says what its reset did (reset: kind, ok, durationMs), and a reset that fails stops the job (reset_failed).
What helps it work:
criteriasay what "done" means. The judge checks each run's return value and the screen when it ended against them.resetputs the computer back in its starting state before each run, as Simulang code or a task for the agent. Without it, each run starts where the last one ended, as a rerun of the code will.guidancepasses what you know about the task to the conversion and every repair.continueWithAgentlets the agent finish a failed run, so the repair sees how the task is done from where the code got stuck.judgeInitialAgentRunstops the job before writing code when the agent's own run fails the judge.
testResult says how the returned code did on the inputs it ran with, not on other inputs. The runs do the task for real, so use test inputs or a test account for a task that sends or submits something.
What the code can do
Learned code runs in a control lease, so it can do what direct control can: the screen, apps, the browser, the shell, files, and callLLM to answer a question about the screen (billed to your account). It cannot ask a person anything mid-run. showMessageToUser writes a line to the run's logs.
Limits of the preview
learnreads the steps, the agent's reasoning, their results and the final answer, not the screenshots.- One experience per call: turns from one session. Several demonstrations of the same task come later.
- One exec runs for at most 300 seconds. For a longer task the code is split into step functions you call in separate execs.
- A call that only writes code never runs it. A learn job runs it on your computer, each run limited to 15 minutes by default.
- How long a call takes depends on the task: a longer run gives longer code to write and check. A learn job also depends on
maxIterations,rollouts, the reset and whether the agent continues failed runs.