Back to Blog
Tutorial

Rachel Kim6 min
⌘+K

Building a desktop agent often means capturing a screenshot, asking the vision model where to click, and hoping the layout hasn't shifted. That works for tiny tasks but quickly burns credits and adds latency for anything longer than a few steps. The Coasty computer use API offers two paths: a stateless predict call for quick actions and stateful sessions that keep a trajectory in memory so the next prediction can see what happened before. Choosing the right one matters for cost, latency, and reliability.

How it works

The stateless path uses POST /v1/predict. You send a base64 screenshot, an instruction, and the cua_version (v3 is the default). The model returns an action list and a status like "pending" or "done". You then capture the next frame, predict again, and repeat until the status is "done". Each predict costs $0.05. For a 10-step task, that is $0.50 in vision and prediction fees alone. This is fine for one-off clicks but becomes expensive for long workflows. The stateful path starts with POST /v1/sessions. You specify a cua_version and optionally a system_prompt. The response includes a session_id. You then POST /v1/sessions/{id}/predict with a screenshot and an instruction. The server includes the previous trajectory snapshot in the context, so the model can remember past actions and screen state. The response again contains actions and a status. Predictions here cost $0.04 each, which is a 20% saving versus the stateless endpoint. You can keep calling predict until the status is "done", then clean up the session with the usual HTTP methods.

bash
#!/bin/bash
# Example: stateless predict (quick click, no history)

SESSION_ID=$(curl -s -X POST https://coasty.ai/v1/sessions \
  -H "X-API-Key: $COASTY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "cua_version": "v3"
  }' | jq -r '.session_id')

capture_and_predict() {
  SCREEN=$(base64 -i screen.png -w 0)
  curl -s -X POST https://coasty.ai/v1/predict \
    -H "X-API-Key: $COASTY_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "screenshot": "'$SCREEN'",
      "instruction": "Click the save button.",
      "cua_version": "v3"
    }' | jq '.'
}

capture_and_predict
python
#!/usr/bin/env python3
import os
import base64
import requests
import json

def capture_screen() -> str:
    # Replace with your actual screenshot capture
    with open("screen.png", "rb") as f:
        return base64.b64encode(f.read()).decode("utf-8")

def create_session() -> str:
    url = "https://coasty.ai/v1/sessions"
    headers = {
        "X-API-Key": os.environ["COASTY_API_KEY"],
        "Content-Type": "application/json"
    }
    payload = {"cua_version": "v3"}
    resp = requests.post(url, headers=headers, json=payload)
    resp.raise_for_status()
    data = resp.json()
    return data["session_id"]

def predict(session_id: str) -> dict:
    url = f"https://coasty.ai/v1/sessions/{session_id}/predict"
    headers = {
        "X-API-Key": os.environ["COASTY_API_KEY"],
        "Content-Type": "application/json"
    }
    payload = {
        "screenshot": capture_screen(),
        "instruction": "Click the save button.",
        "cua_version": "v3"
    }
    resp = requests.post(url, headers=headers, json=payload)
    resp.raise_for_status()
    return resp.json()

if __name__ == "__main__":
    sid = create_session()
    result = predict(sid)
    print(json.dumps(result, indent=2))

When to pick stateless predict

  • One-shot actions like clicking a button, typing a form, or verifying a state change.
  • You want the simplest possible integration without managing session lifecycles.
  • You expect the task to finish within 1, 3 predict calls, keeping the $0.05 per call cost low.
  • You do not need to remember previous actions or screen history across predictions.

Use stateless predict when the task is short and history does not matter.

When to pick stateful sessions

  • Multi-step workflows like onboarding flows, account creation, or long-running tasks.
  • You need the model to see what it did before to avoid repeating mistakes.
  • You want to save credits: each predict in a session is $0.04 instead of $0.05.
  • You need a stable trajectory in memory so the agent can correct itself based on prior actions.

Use stateful sessions for multi-step workflows to reuse trajectory and reduce prediction cost.

Where this beats brittle automation

Traditional automation relies on fragile selectors like XPath, CSS classes, or ID attributes. A layout change breaks the selector, and the script fails. The Coasty computer use API lets the model see the actual screen and understand the layout at each step. With stateful sessions, it remembers what it has already done, so it can adapt to small UI shifts without requiring selector updates. You get an agent that behaves like a real user while keeping costs predictable and low.

Start with stateless predict for quick clicks, then move to stateful sessions when your tasks grow longer. With trajectory memory and a 20% prediction discount, your agents become more reliable and cost effective. Get a key at https://coasty.ai/developers and build smarter computer use agents today.

© 2026 Coasty

Backed byYCombinator