Stateful Sessions vs Stateless Predict in the Computer Use API
Many computer use agents start with a simple pattern: capture a screen, send it to a model, get actions, repeat. That works for tiny tasks but breaks as environments persist across steps. You need memory of what happened so the model can reason about state changes. The Coasty Computer Use API solves this with stateful sessions. You create a session, keep a running trajectory, and call /v1/sessions/{id}/predict for $0.04 per step. This post shows how to build a session-based agent and why it beats stateless calls.
How it works
The stateless path is POST /v1/predict. It takes a base64 screenshot, an instruction, and cua_version. You pay $0.05 and get back actions and status. You must manually keep a history of previous steps. The stateful path is POST /v1/sessions, then POST /v1/sessions/{id}/predict. The session endpoint creates a trajectory and returns an id. Each predict call appends a step to that trajectory so later calls see everything that happened. Predict costs $0.04 per call. The API tracks the session state so the model can reason about changed UI elements without you managing history yourself.
curl -X POST https://coasty.ai/v1/sessions \ -H "X-API-Key: $COASTY_API_KEY" \ -H "Content-Type: application/json" \ -d '{"cua_version": "v3"}'
# Response
{
"id": "sess_abc123",
"status": "ready",
"cua_version": "v3"
}Stateful predict call
- ●POST /v1/sessions/{id}/predict expects a screenshot (base64), instruction, and cua_version.
- ●The call costs $0.04 per step.
- ●The response includes actions, a status, and the updated trajectory.
- ●Loop capture → predict → act until status is "done" or you stop manually.
import base64
import requests
import os
BASE = "https://coasty.ai/v1"
API_KEY = os.getenv("COASTY_API_KEY")
def create_session():
resp = requests.post(
f"{BASE}/sessions",
headers={"X-API-Key": API_KEY, "Content-Type": "application/json"},
json={"cua_version": "v3"},
)
resp.raise_for_status()
return resp.json()["id"]
def predict(session_id, screenshot_b64, instruction):
resp = requests.post(
f"{BASE}/sessions/{session_id}/predict",
headers={"X-API-Key": API_KEY, "Content-Type": "application/json"},
json={
"screenshot": screenshot_b64,
"instruction": instruction,
"cua_version": "v3",
},
)
resp.raise_for_status()
return resp.json()
if __name__ == "__main__":
sess_id = create_session()
# load a screenshot as base64 here
screenshot = open("screen.png", "rb").read()
screenshot_b64 = base64.b64encode(screenshot).decode()
result = predict(sess_id, screenshot_b64, "Click the submit button.")
print(result)Use /v1/sessions + /v1/sessions/{id}/predict for $0.04 per step so your agent sees the full trajectory and remembers previous actions.
Where this beats brittle automation
Pure API tools rely on stable IDs or selectors that can break when UI changes. A computer use agent looks at the actual screen content. In stateful mode, you do not need to manually stitch histories together. The API keeps the trajectory and passes it to the model each step. This means the agent can adapt when buttons move, labels change, or a modal appears without you updating selectors. Stateless predict is fine for quick one-off checks, but for multi-step workflows and complex environments sessions give you persistence and lower cost per step.
Start building persistent agents with stateful sessions. Use /v1/sessions and /v1/sessions/{id}/predict to run multi-step computer use workflows reliably. Get your key at https://coasty.ai/developers and try the examples above.