The short answer
Two things stop double charges. First, the balance check and the deduction must be one atomic step, so two simultaneous calls cannot both see enough money. Second, each logical operation carries an idempotency key, so a retry of the same call is recognised and refused instead of charged again. On Nexus402, a retry under a key that already paid gets 422 Idempotency_Conflict and costs nothing.
Why agents get charged twice
Three ordinary things cause it:
- Timeouts that were not failures. The call succeeded, but the answer took longer than the client waited, so the client tries again.
- Lost answers. The call ran and was charged, and the connection dropped on the way back. From the agent's side it looks exactly like a failure.
- Concurrency. An agent fans out several calls at once, or a framework retries in parallel, and two copies arrive together.
None of these is a bug in the agent. Retrying is the right reaction to an unknown outcome. The billing system has to make retrying safe.
Layer one: one atomic deduction
The simplest meter reads the balance, checks there is enough, then writes the new balance back. Between the read and the write, another call can do the same read. Both see enough money, both deduct, and the wallet goes below zero or the caller pays twice.
Nexus402 does the check and the deduction as one atomic operation in front of every paid call: there is no moment where two calls can see the same balance. Because this is the part that must never be wrong, the build runs a test that fires 10,000 concurrent requests at one wallet and checks that exactly the right number are charged, and it runs on every change.
Layer two: an idempotency key
Atomicity stops two calls racing. It does not stop a deliberate retry of a call that already went through. For that, the caller labels the operation:
curl -s https://api.nexus402.dev/v1/mcp \
-H "X-Nexus-Api-Key: nx_dev_..." \
-H "Idempotency-Key: 7f3a9c12-4e5b-4d8a-9f01-2b3c4d5e6f70" \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/call",...}'
One key pays for a call at most once. If the first attempt under it was accepted and not refunded, a retry is refused with 422 Idempotency_Conflict: it does not reach the tool and does not touch the balance. If the first attempt was refunded, because the tool failed before delivering anything, the key is free again and the retry runs as a new call, charged once.
What counts as the same request
Two requests are the same when they use the same API key, reach the same tool version and carry the same body. The JSON-RPC id and the order of keys in the JSON do not count, so a client that mints a new id for its retry is still sending the same request, and is still protected.
A refusal that took nothing never uses the key at all: a 402, a 429, or a 503 before the debit leaves it free for the retry.
What to do in your agent
- Generate a fresh idempotency key, a UUID, for each logical operation.
- Reuse it only to retry that same operation, never for a new one.
- On
422 Idempotency_Conflict, stop retrying: the first attempt was taken. Look it up in your call log or movements. - Give each agent its own key with a rolling 24-hour cap, so even a retry storm has a ceiling.