{"uid":"cap_VAGH_J1GyEHVltDTOjlrp","slug":"agent402-tools-compare-and-swap-cas-memory-e8f394a8","name":"agent402.tools Compare-And-Swap (CAS) Memory","description":"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.","url":"https://agent402.tools/api/memory/cas","method":"POST","headers":{},"bodySchema":{"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)"}}},"responseSchema":{"type":"json","example":{"key":"locks/import","owner":"0x…","value":"agent-7","swapped":true,"expiresAt":1760086430}},"example":null,"exampleRequest":null,"tags":["x402"],"displayCostAmount":"0.001","displayCostAsset":"USDC","priceDynamic":false,"priceHint":null,"priceStatus":"priced","priceSource":"probe","requiresHandshake":false,"reviewCount":0,"rating":{"score":"0.00","successRate":"1.00","reviews":0,"stars":null,"state":"rated"},"availabilityStatus":"unknown","priceObserved":null,"sessionDeposit":null,"pricing":{"kind":"static","summary":"$0.001/call","primary":{"kind":"static","protocol":"x402","network":"base","amountUsd":"0.001","per":"call","confidence":"exact"},"accepted":[{"kind":"static","protocol":"x402","network":"base","amountUsd":"0.001","per":"call","confidence":"exact"}]},"paymentMethods":[{"uid":"pm_8Me-ibz8Du6PZu1261X9_","protocol":"x402","methodType":"crypto","chain":"base","mode":"charge","costAmount":"0.001","costPer":"request","priority":0,"asset":"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913","unit":"request","depositMicros":null,"planRef":null}],"brandName":null,"brandSlug":null,"brandBaseUrl":null,"brandDocsUrl":null,"whatItDoes":"Atomically write or delete a key only when its current value matches an expected value, enabling distributed locks and optimistic concurrency for AI agents.","exampleAgentPrompt":"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.","exampleUseCases":null,"resultDescription":"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.","failureModes":["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"],"whenToPreferThis":"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.","instructions":null,"reviewSummary":null,"reviewSummaryHighlights":null,"reviewSummaryConcerns":null,"reviewSummaryGeneratedAt":null,"activationCount":15,"lastUsedAt":"2026-07-20T22:17:40.571Z","lastSuccessfullyRanAt":"2026-07-20T22:17:40.571Z","lastHealthCheckAt":"2026-09-14T19:14:06.934Z","isFirstParty":false}