Legato

Terminal and Python

Both use the model and desktop that legato setup saved (setup), or the ones you name.

Terminal

legato run "Find the next Caltrain from SF to Palo Alto"

It prints each turn with a timer, the answer if the task asked a question, and where the time went. It exits 0 when the task is done, 2 when it could not run (a setup, model or desktop error) and 1 otherwise.

flag what it does
--app NAME macOS: work in that app's window in the background (--app Notes, --app Safari)
--screen use this whole screen; it moves your real mouse (macOS, Windows, Linux)
--sandbox docker a fresh Linux desktop container (needs Docker); also e2b, or your own file.py:function (desktops)
--model M --base-url URL --api-key-env VAR another model for this run
--max-steps N model turns before it gives up (default 40)
--timeout S end the run after S seconds
--json one JSON object at the end (status, done, answer, error, actions, wall_s, usage), for scripts and agents
--yes on this computer, do not ask before risky actions (a typed rm or Remove-Item, Cmd+Q, Alt+F4...)

With no flag, on a Mac it works in the background in the app the task names ("... in Notes", "... on Spotify"), else in Safari; elsewhere on the whole screen. legato setup --desktop docker (or screen) changes that default.

Python

import legato

result = legato.run("On Wikipedia, look up the Eiffel Tower and tell me how tall it is")
if result.status == "done":   # the check for success: `failed` is only "the model called fail"
    print(result.answer)
else:
    print(result.status, result.error)
print(result.report())        # how it ended, the model calls, the cost, and where the time went

legato.run(task, desktop=None, *, model=None, base_url=None, api_key=None, app=None, max_steps=40, on_step=None, confirm="auto", timeout_s=None, cancel=None, max_cost_usd=None, record_dir=None) opens the desktop, runs the task and closes the desktop. desktop is a name ("background", "screen", "docker", "e2b") or a desktop you opened yourself. It returns a result for every way a run can end, model and desktop errors included; only a missing model or a bad argument (a desktop that names none) raises.

result.status (typed legato.RunStatus) says how the run ended. Check result.status == "done", not not result.failed: failed is False for max_steps, timeout, max_cost and error too. For every status but done, result.error says why.

status what happened what to do
done the model called done read answer
failed the model called fail (it judged the task impossible), or the desktop refused the same action 3 times, or 6 actions in all error has the model's reason (also in answer) or the refusals that ended it; reword the task, or give it a desktop that can (--screen)
max_steps max_steps ran out first raise max_steps, or split the task
timeout timeout_s ran out raise timeout_s; actions shows how far it got
max_cost the reported cost reached max_cost_usd raise it; usage["cost_usd"] is what was spent
cancelled your cancel event was set nothing
stopped the person at the computer stopped it nothing; their choice
error a model call failed (HTTP 401: ..., a timeout) or the desktop did (the desktop failed: ...) fix the key, model or desktop that error names, then retry; a refused key's error names where the key came from ($OPENROUTER_API_KEY, the one legato setup saved), never the key

Choose each part yourself

The whole loop is agent.run(task, desktop):

import os, legato

agent = legato.Harness(model="qwen/qwen3.8-27b", base_url="https://openrouter.ai/api/v1",
                       api_key=os.environ["OPENROUTER_API_KEY"])
desk = legato.local()                         # this whole screen (macOS, Windows, Linux)
# desk = legato.local(background="Safari")    # macOS only: one app's window, in the background

def on_step(turn, actions, timing):           # after each model turn
    print(turn, [a.describe() for a in actions if a], f"${timing['usage']['cost_usd']:.4f} so far")

result = agent.run("Open example.com and tell me the page title", desk, max_steps=40, on_step=on_step,
                   max_cost_usd=0.50, record_dir="run-1")
print(result.answer)

agent.run(task, desk, max_steps=40, on_step=None, *, timeout_s=None, cancel=None, raise_desktop_errors=True, max_cost_usd=None, record_dir=None) takes the same timeout_s, cancel, max_cost_usd and record_dir. A desktop that fails raises out of agent.run; pass raise_desktop_errors=False to get it back as a result, as legato.run does.

Use Legato as a tool in your agent

import asyncio, legato

def use_computer(task: str) -> dict:
    r = legato.run(task, timeout_s=120, max_cost_usd=0.50)
    if r.status == "done":
        return {"ok": True, "answer": r.answer}
    return {"ok": False, "status": r.status, "why": r.error, "did": r.actions}

async def use_computer_async(task: str) -> dict:
    return await asyncio.to_thread(use_computer, task)

legato.run blocks until the run ends, so inside a running event loop call it through asyncio.to_thread, never directly. On your own screen ("screen", "background") the desktop keeps its state between calls: windows the last task opened are still there for the next one. Run one task at a time per desktop.

When a desktop fails

Opening a desktop yourself raises instead: legato.local(background="NoSuchApp"), legato.providers.docker(...) and the other built-in desktops raise legato.DesktopStartError when they can't start, and one that fails later raises legato.DesktopError out of agent.run (unless raise_desktop_errors=False). A desktop you attach yourself (legato.attach(screenshot=..., execute=...)) raises whatever your functions raise. DesktopStartError is a DesktopError, and both are RuntimeErrors, so except RuntimeError still catches them.

Write a starting point

legato init writes agent.py, a working agent with your saved model and desktop, to run as it is or copy into your own code: python agent.py "Open example.com and tell me the page's heading". It prints each turn, the answer and the report.

Benchmarks

legato bench runs the office benchmark and head-to-head comparisons (benchmarks). RL rollouts at scale are part of Legato Cloud, in beta (rl.md).

Edit this page on GitHub · Markdown