# agent402.tools Compare-And-Swap (CAS) Memory

> agent402.tools Compare-And-Swap (CAS) Memory is a paid API for AI agents from agent402.tools, paid per call via x402, $0.001/call, status unknown (last checked 2026-09-14, last successful call 2026-07-20).

Atomically write or delete a key only when its current value matches an expected value, enabling distributed locks and optimistic concurrency for AI agents.

## Facts

- Endpoint: POST https://agent402.tools/api/memory/cas
- Price: $0.001/call
- Payment: x402
- Status: unknown
- Last checked: 2026-09-14
- Last successful call: 2026-07-20
- Success rate: 100% of calls made through Zero
- Activations on Zero: 15
- Tags: x402
- Canonical page: https://www.zero.xyz/c/agent402-tools-compare-and-swap-cas-memory-e8f394a8
- Structured record (JSON): https://api.zero.xyz/v1/capabilities/cap_VAGH_J1GyEHVltDTOjlrp

Status and success rate cover calls made through Zero and Zero's own probes. Third-party monitors may report differently.

## How to call it through Zero

Zero handles the 402 payment challenge and records the run. With the Zero CLI installed (`npm i -g @zeroxyz/cli`):

```sh
zero fetch --capability agent402-tools-compare-and-swap-cas-memory-e8f394a8 -d '<json body>'
```

Example prompt: Acquire a distributed lock on the key 'job-queue-processor' — expected value should be null (key absent), set it to 'agent-7-locked' with a 30-second TTL lease so no other agent can grab it while I'm working.

## When to prefer this

Use this endpoint when you need atomic, race-condition-safe coordination between multiple agents or processes accessing shared state — specifically for distributed lock acquisition and release, or optimistic concurrency patterns where you only want to write if no one else has changed the value since you last read it. Prefer this over a plain write when correctness depends on the key not having changed between read and write.

## Known failure modes

- CAS mismatch: current value does not match expected — write rejected, another agent may hold the lock
- Key already exists when expected=null — lock acquisition fails
- Key expired or was deleted unexpectedly — release may fail if expected value no longer present
- Invalid or malformed key — 400 error
- TTL not set on lock acquisition — lock never expires, potential deadlock
- Payment failure via x402 protocol — 402 response before operation executes
- Owner namespace mismatch — 403 if readwrite grant not present

## How this service works

Atomically write (or release) a key only if its current value equals `expected` - the coordination primitive for distributed locks and optimistic concurrency across agents. Acquire a lock: expected=null + a value + ttlSeconds. Release it: expected=<your token> with no value (deletes on match). Update safely: expected=<old>, value=<new>. Returns whether it swapped and the current value.

## Output

The agent receives confirmation of whether the conditional write succeeded or failed — if the key's current value matched the expected value, the new value is written (or the key is deleted if no value was provided) and the operation confirms success; if the values did not match, the write is rejected, signaling a concurrency conflict or that the lock is already held.

## Request schema (JSON Schema)

```json
{
 "type": "object",
 "properties": {
  "key": {
   "type": "string",
   "description": "Key to conditionally write"
  },
  "owner": {
   "type": "string",
   "description": "Optional 0x namespace (requires a readwrite grant)"
  },
  "value": {
   "description": "New value to set on match; omit to DELETE on match (lock release)"
  },
  "expected": {
   "description": "Required current value to match (null or omitted = key absent/expired)"
  },
  "ttlSeconds": {
   "type": "number",
   "description": "Optional TTL for the written value (lease for locks)"
  }
 }
}
```

## Response schema (JSON Schema)

```json
{
 "type": "json",
 "example": {
  "key": "locks/import",
  "owner": "0x…",
  "value": "agent-7",
  "swapped": true,
  "expiresAt": 1760086430
 }
}
```

## More

- Live health (JSON, refreshed every minute): https://www.zero.xyz/c/agent402-tools-compare-and-swap-cas-memory-e8f394a8/health.json
- [Zero catalog index](https://www.zero.xyz/llms.txt)
- [Other services from agent402.tools](https://www.zero.xyz/host/agent402.tools/llms.txt)
