Stateful Sessions vs Stateless Predict in the Computer Use API
Most computer use APIs require you to send every screenshot and instruction from scratch. That works for simple tasks but breaks on complex workflows. Stateless predict loops are expensive and fragile. The Coasty Computer Use API introduces stateful sessions to solve this. Sessions store the entire trajectory so the model can reference past actions. This reduces cost, improves reliability, and lets you chain tasks.
Stateless predict
- ●Endpoint: POST /v1/predict
- ●Cost: $0.05 per call
- ●Input: base64 screenshot, instruction, cua_version
- ●Output: actions array + status
- ●No memory of previous steps
- ●You must repeat the entire context each iteration
Stateful sessions
- ●Endpoint: POST /v1/sessions ($0.10)
- ●Endpoint: POST /v1/sessions/{id}/predict ($0.04)
- ●Session holds trajectory memory of all actions
- ●You only send the new screenshot, not everything
- ●Saves money and improves context for the model
import base64
import os
import requests
import json
api_key = os.environ.get("COASTY_API_KEY")
if not api_key:
raise RuntimeError("COASTY_API_KEY must be set")
# 1. Create a stateful session
session_resp = requests.post(
"https://coasty.ai/v1/sessions",
headers={"X-API-Key": api_key},
json={"cua_version": "v3"}
)
session_resp.raise_for_status()
session = session_resp.json()
session_id = session["id"]
# 2. Loop with predict on that session
def predict_step(screenshot_base64, instruction):
predict_resp = requests.post(
f"https://coasty.ai/v1/sessions/{session_id}/predict",
headers={"X-API-Key": api_key},
json={
"screenshot": screenshot_base64,
"instruction": instruction,
"cua_version": "v3"
}
)
predict_resp.raise_for_status()
return predict_resp.json()
# 3. Use a tiny demo screenshot
def encode_image(path):
with open(path, "rb") as f:
return base64.b64encode(f.read()).decode("utf-8")
# For testing you can replace with your own base64 screenshot
# screenshot_base64 = encode_image("screenshot.png")
# actions, status = predict_step(screenshot_base64, "Click the Save button.")
# print(json.dumps(actions, indent=2))
# if status == "done":
# print("Task completed.")Session: POST /v1/sessions ($0.10) then POST /v1/sessions/{id}/predict ($0.04).
When to choose each model
- ●Use stateless predict for quick one-offs and demos
- ●Use stateful sessions for multi-step workflows, browser sessions, and long tasks
- ●Sessions give the model trajectory memory, reducing context bloat
- ●Stateless predict costs $0.05 per call regardless of steps; sessions cost $0.04 per predict after the initial $0.10 setup
Where this beats brittle automation
Traditional automation relies on stable selectors, XPath, or API endpoints that may change. Screens can shift layout, buttons can appear or disappear, and DOM structures evolve. Computer use agents see the screen like a human does. They can infer element positions, handle layout changes, and recover from unexpected states. Sessions let the agent remember prior actions, making it easier to follow multi-step flows without fragile retry logic. This is why developers prefer computer use agents for complex, open-ended tasks.
Start using stateful sessions for robust, long-running automation. Get your API key at https://coasty.ai/developers and build smarter computer use agents with Coasty.