Idempotency Keys in the Computer Use API: Never Double Charge
When you drive real desktops, browsers, and terminals, retries are easy. Network blips, load balancers, and client-side retry logic can send the same task twice. In the Coasty computer use API, that means two $0.05 agent steps. Build a reliable automation pipeline and you need a replay-safe primitive. Idempotency keys are that primitive.
How idempotency works
Idempotency protects only the 18 documented reserve-and-replay operations when an Idempotency-Key header is present on the original request. If a duplicate request arrives with the same key, the server returns the stored result from the first call and skips the second. Without this header, retries can trigger duplicate charges and wasted steps. The feature is scoped to specific operations. It does not retroactively protect requests you send without the header.
import os
import requests
import base64
import json
def run_task_with_idempotency(
instruction: str,
cua_version: str = "v3",
idempotency_key: str = None,
):
"""Send a task to Coasty and apply idempotency for replay safety."""
api_key = os.environ["COASTY_API_KEY"]
url = "https://coasty.ai/v1/runs"
headers = {
"X-API-Key": api_key,
"Content-Type": "application/json",
}
if idempotency_key:
headers["Idempotency-Key"] = idempotency_key
payload = {
"machine_id": "your-machine-id",
"task": instruction,
"cua_version": cua_version,
"max_steps": 50,
"deadline_seconds": 600,
"on_awaiting_human": "pause",
}
resp = requests.post(url, json=payload, headers=headers)
resp.raise_for_status()
result = resp.json()
return result
# Example: prevent double-charge on a retry
if __name__ == "__main__":
run_resp = run_task_with_idempotency(
instruction="Open Chrome and navigate to https://coasty.ai",
idempotency_key="unique-request-id-123",
)
print(json.dumps(run_resp, indent=2))Supported operations
- ●Idempotency protects only the 18 documented reserve-and-replay operations when the Idempotency-Key header is present on the original request.
- ●Without the header, retries may cause duplicate $0.05 agent steps.
- ●Scopes and keys gate access. Insufficient scope (403) or credits (402) will still fail.
- ●Idempotency does not retroactively protect past requests. The key must be sent with the original call.
- ●Webhooks are signed with HMAC (header Coasty-Signature) but idempotency is handled server-side before the webhook fires.
Always include Idempotency-Key on the original request to guarantee exactly one charge per action.
Where this beats brittle automation
Traditional automation relies on brittle selectors and fixed APIs. When the DOM changes or an endpoint changes, your script breaks. Coasty’s computer use agent watches the screen, understands context, and acts like a human. Idempotency keys ensure that even if you retry a failed run, you never pay for the same work twice. Combine this with server-sent events (GET /v1/runs/{id}/events) to stream progress and implement true replay safety.
Build resilient desktop automation pipelines that never accidentally double-charge. Get your API key at https://coasty.ai/developers to start using idempotency keys today.