Engineering

Stateful Sessions vs Stateless Predict in the Computer Use API

Lisa Chen||5 min
+K

You want an agent that watches a screen, understands it, and acts like a human. Coasty gives you two ways to do that. Stateless predict sends a screenshot and an instruction, it returns actions for that single turn. Stateful sessions create a persistent trajectory so the agent can remember previous steps. This post explains the real endpoints, fields, and pricing so you can choose the right model for your automation.

How stateless predict works

Stateless predict is the simplest starting point. You POST a base64 screenshot, an instruction, and the CUA version. The server returns actions and a status. You repeat the loop until status is "done". This is ideal for one-shot tasks or when you do not need memory between turns. The endpoint is /v1/predict. It costs $0.05 per request. The response includes actions and a status field. You capture a new screenshot, send it back, and keep going.

bash
Example stateless predict loop with curl.

You need a base64 screenshot (S) and a COASTY_API_KEY in the environment.


# Capture screenshot to base64
S=$(base64 -i screenshot.png)

# Loop until status is done
while true; do
  
  # Stateless predict request
  STATUS=$(curl -s -X POST https://coasty.ai/v1/predict \
    -H "X-API-Key: $COASTY_API_KEY" \
    -H "Content-Type: application/json" \
    -d "{\"screenshot\": \"$S\", \"instruction\": \"Click the login button\", \"cua_version\": \"v3\"}" \
    -o response.json -w "%{http_code}")
  
  if [ "$STATUS" -lt 200 ] || [ "$STATUS" -ge 300 ]; then
    echo "Request failed with status $STATUS"
    cat response.json
    exit 1
  fi
  
  # Parse status from JSON
  STATUS_TEXT=$(jq -r '.status' response.json)
  
  echo "Status: $STATUS_TEXT"
  
  if [ "$STATUS_TEXT" = "done" ]; then
    echo "Actions: $(jq -c '{actions, status}' response.json)"
    break
  fi
  
  # Capture next screenshot
  S=$(base64 -i screenshot.png)

done

How stateful sessions work

Stateful sessions give the agent memory. First you create a session with POST /v1/sessions. The response includes a session_id. Then you POST /v1/sessions/{id}/predict with a screenshot and instruction. The server adds these steps to the trajectory. If the task needs multiple turns, the agent sees its own history. This is useful for multi-step workflows like account creation, data entry, or multi-page flows. Stateful predict costs $0.04 per request.

python
Python example for stateful sessions.

You need requests and base64. Set COASTY_API_KEY in the environment.

```python
import os
import base64
import requests
import json

def stateful_session_demo():
    api_key = os.getenv("COASTY_API_KEY")
    base_url = "https://coasty.ai/v1"
    
    # 1. Create a stateful session
    resp = requests.post(
        f"{base_url}/sessions",
        headers={"X-API-Key": api_key},
        json={},
    )
    resp.raise_for_status()
    session_id = resp.json()["session_id"]
    print(f"Created session {session_id}")
    
    # 2. First predict with initial screenshot
    screenshot = base64.b64encode(open("screenshot.png", "rb").read()).decode("utf-8")
    payload = {
        "screenshot": screenshot,
        "instruction": "Click the login button",
        "cua_version": "v3",
    }
    
    resp = requests.post(
        f"{base_url}/sessions/{session_id}/predict",
        headers={"X-API-Key": api_key, "Content-Type": "application/json"},
        json=payload,
    )
    resp.raise_for_status()
    result = resp.json()
    print("First result:", json.dumps(result, indent=2))
    
    # 3. Second predict using trajectory memory
    screenshot = base64.b64encode(open("screenshot.png", "rb").read()).decode("utf-8")
    payload = {
        "screenshot": screenshot,
        "instruction": "Type a username and hit enter",
        "cua_version": "v3",
    }
    
    resp = requests.post(
        f"{base_url}/sessions/{session_id}/predict",
        headers={"X-API-Key": api_key, "Content-Type": "application/json"},
        json=payload,
    )
    resp.raise_for_status()
    result = resp.json()
    print("Second result:", json.dumps(result, indent=2))
    
    # 4. Cancel session when done
    requests.post(f"{base_url}/sessions/{session_id}", headers={"X-API-Key": api_key})
    print("Session cancelled")

if __name__ == "__main__":
    stateful_session_demo()
```

Pricing comparison per request

  • Stateless predict: $0.05 per POST /v1/predict call
  • Stateful predict: $0.04 per POST /v1/sessions/{id}/predict call
  • Session creation (POST /v1/sessions) is free
  • You pay only for predict calls, not for creating sessions

Choose stateless predict for quick one-shot tasks. Choose stateful sessions for multi-step workflows that need trajectory memory.

Where this beats brittle automation

Traditional automation relies on brittle selectors and hardcoded APIs. If a layout changes or an element ID shifts, you must update your code. Stateful sessions let the agent see the screen, understand the context, and reference its own history. It can handle UI changes, missing IDs, or dynamic content without your updates. This is the power of computer use agents over selector-based tools.

You now know the difference between stateless predict and stateful sessions in the Coasty computer use API. Start with stateless predict for simple tasks, then move to stateful sessions for multi-step workflows. Get your key and start building at https://coasty.ai/developers.

Want to see this in action?

View Case Studies
Try Coasty Free