Docs/Learned code
Concepts

Learned code

Research preview

Turn 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:

Prompt
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:

  1. Record. Send the task to POST /message with options.recordTrajectory: true, ideally as a template with inputs, so the inputs are named:

    Record a task
    curl -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
      }'
  2. Learn. POST /v1/agents/learn with the turn's id returns async 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.

  3. 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 what main returns. The code throws needs_sign_in: <site> when the computer is not signed in, and precondition_failed: ... when it starts on the wrong screen.

  4. Judge and repair. Check a run yourself, or with POST /v1/agents/judge. When it failed, call learn again with the code as initialCode and the run as an evaluation. If the code cannot handle a case at all, send the template to POST /message with those inputs, let Sai solve it, and pass that new turn to learn: the repair adds what the code was missing.

In the SDKs:

Learn, run and repair
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.

Learn a reliable function from a task
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:

  • criteria say what "done" means. The judge checks each run's return value and the screen when it ended against them.
  • reset puts 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.
  • guidance passes what you know about the task to the conversion and every repair.
  • continueWithAgent lets the agent finish a failed run, so the repair sees how the task is done from where the code got stuck.
  • judgeInitialAgentRun stops 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

  • learn reads 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.
© 2026 Simular Inc. All rights reserved.