Lesson 39.1 · 1 of 5 · 4 min read · Reviewed 24 Nov 2026
The perception and action loop
By the end, you can
- Describe the perception and action loop.
- Explain why it's slower than API calls.
A computer-use agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. works the way a person does at a screen: look, act, look again.
The loop
- Perceive: capture a screenshot (and maybe other screen data).
- Decide: the model picks one action: click at x,y, type text, press a key, scroll, or stop.
- Act: your code performs it in the sandboxSandbox: An isolated environment that limits what code or an agent can reach, such as files, network or credentials..
- Repeat with a fresh screenshot, until the task is done or a limit is hit.
Why it’s slow
Every step is a model call with an image, so a 20-click task means 20 turns. Pages load, menus animate, pop-ups appear.
When it’s worth it
- Legacy or internal apps with no API.
- Third-party websites you can’t integrate with.
- Testing a UI the way users see it.
If an API, an MCPModel Context Protocol (MCP): An open protocol that standardises how AI applications connect to tools, data and prompts through servers. server or a structured tool exists, use that. Computer useComputer use: Letting a model operate a computer through screenshots, mouse and keyboard actions. is the fallback, not the default.
Key points
- The loop: take a screenshot, decide an action, perform it, take a new screenshot, repeat.
- Each step is a full model call with an image, so tasks take many slow turns.
- Use computer use only where no API or structured tool exists.
Run a perception and action loop
Build the look, act, look again loop against a simulated app, with a step cap that stops a stuck run.
You will have: A loop that completes a three-screen task, prints every step, and stops a second run that can never finish.
Steps
- Read the simulated app: each screen has a name and buttons that lead to other screens.
- Write perceive() that returns the current screen, standing in for a screenshot.
- Write a scripted decide(screen, goal) that picks one action per turn, standing in for the model.
- Loop: perceive, decide, act, repeat, until the goal screen appears or the step cap is reached.
- Run once with a reachable goal and once with an unreachable one, and print how each ended.
Done when
- The reachable task ends on the confirmation screen in 3 steps.
- The unreachable task stops at the 8-step cap instead of looping for ever.
- Every step prints the screen seen and the action taken.
Open in the Playground No API key needed.
Starter code (Python)
app = {"home": {"Expenses": "expenses"}, "expenses": {"New claim": "form", "Back": "home"},
"form": {"Submit": "confirmation", "Back": "expenses"}, "confirmation": {}}
state = {"screen": "home"}
def perceive():
return state["screen"] # stands in for a screenshot
def decide(screen, goal):
# A scripted stand-in for the model: follow a known path, otherwise go Back
path = {"home": "Expenses", "expenses": "New claim", "form": "Submit"}
return path.get(screen, "Back")
def act(button):
state["screen"] = app[state["screen"]].get(button, state["screen"])
def run(goal, max_steps=8):
state["screen"] = "home"
# TODO: loop perceive -> check goal -> decide -> act, with a step cap
return "not implemented"
print(run("confirmation"))
Show a solution
app = {"home": {"Expenses": "expenses"}, "expenses": {"New claim": "form", "Back": "home"},
"form": {"Submit": "confirmation", "Back": "expenses"}, "confirmation": {}}
state = {"screen": "home"}
def perceive():
return state["screen"] # stands in for a screenshot
def decide(screen, goal):
# A scripted stand-in for the model: follow a known path, otherwise go Back
path = {"home": "Expenses", "expenses": "New claim", "form": "Submit"}
return path.get(screen, "Back")
def act(button):
state["screen"] = app[state["screen"]].get(button, state["screen"])
def run(goal, max_steps=8):
state["screen"] = "home"
for step in range(1, max_steps + 1):
screen = perceive()
if screen == goal:
return f"done in {step - 1} steps"
action = decide(screen, goal)
print(f" step {step}: saw {screen}, click {action}")
act(action)
return f"stopped at the {max_steps}-step cap on {perceive()}"
print("goal confirmation:", run("confirmation"))
print("goal settings:", run("settings"))