All articles

Agents and MCPThe agent-native ledger, Part 2

Idempotency keys for AI agents and MCP tools

Published 10 min read

The short answer

An idempotency key is a value a client sends with a write so the server can recognise a retry and return the first result instead of acting twice. An AI agent should derive the key from the business fact, such as supplier and invoice number, not from the tool-call id. The server should refuse the same key with different arguments.

Why does an AI agent need an idempotency key?

Because an agent cannot see whether its first call landed. The response gets lost on the way back, the client times out, the session restarts, or the model runs a step again in its next turn because it has lost track of the first attempt. Without protection, the same invoice ends up in the books twice.

The Model Context Protocol (MCP) offers only a hint for this: idempotentHint. When it is set, the schema says, calling the tool again with the same arguments has no additional effect. The schema also says that every annotation is a hint with no guarantee, and that a client should not rely on annotations from an untrusted server. MCP has no field for a key, neither in 2025-11-25 nor in the current 2026-07-28 revision. So the key is an ordinary tool argument, and the server has to require it and check it.

Tool in TILLKeyAnnotations TILL advertises
post_entry (post an entry)requireddestructiveHint: false, idempotentHint: true
save_draft (save a draft)requireddestructiveHint: true, idempotentHint: true
send_invoice (send an invoice)optionaldestructiveHint: true, no idempotentHint
create_contact (create a contact)optionaldestructiveHint: false, no idempotentHint
set_fiscal_config (legal form, currency, fiscal year)nonedestructiveHint: true, idempotentHint: true
list_accounts (read accounts)nonereadOnlyHint: true

The rule behind the table: TILL advertises idempotentHint only where it holds. A tool with a required key never doubles on a retry. A tool with no key sets an absolute state (“set this field”), and setting it twice changes nothing. With an optional key it depends on the caller, so TILL promises nothing there.

Where should an AI agent’s idempotency key come from?

An agent should derive its key from the business fact: the invoice, not the attempt. The IETF draft for the Idempotency-Key header recommends a UUID, and so does Stripe. That fits a program that holds the UUID in a variable and retries in a loop. A language model holds no variable across turns. When it retries a step in a new turn, it mints a new UUID, and the server sees a new operation.

Source of the keySurvives a retry in a new turn?Why
The tool-call idnoIt names one attempt, not one operation. Every attempt gets a new one.
A random UUID per callnoEvery attempt mints a new value.
A UUID minted once per intent and held by the clientonly while the client holds itA restart or a lost context forgets it.
Derived from the business factyesThe same invoice always yields the same key.

An example. Muster Versicherung AG bills Muster Werkstatt GmbH for premium 2026-1187, CHF 486.50. The agent posts it under the key bill:muster-versicherung:2026-1187. Both the supplier and the number go in, because two suppliers can issue the same invoice number. Personal data stays out: Stripe explicitly advises against using email addresses and other personal identifiers as keys.

{
  "workspaceId": "ws_demo",
  "date": "2026-10-31",
  "description": "Muster Versicherung AG, premium 2026-1187",
  "source": "agent",
  "idempotencyKey": "bill:muster-versicherung:2026-1187",
  "lines": [
    { "account": "<id of 6300>", "debit": 48650 },
    { "account": "<id of 1020>", "credit": 48650 }
  ]
}
Amounts are whole Rappen: 48650 is CHF 486.50. 6300 is insurance, fees and permits, 1020 the bank account. Insurance supplies are exempt from Swiss VAT (MWSTG Art. 21 para. 2 no. 18), so there is no tax line.

In what order should a server check a retried call?

A server that survives retries compares the arguments before it replays, and replays before any check reads the current state of the data. Checks that read nothing may run earlier. Get the order wrong and the key protects less than it seems to. In TILL the order is:

The path of a keyed call in TILL: required fields, permission and the workspace, the argument check, the shape check, the replay, the checks against the books, then the act and its result in one transaction. Only the last step writes.
  1. Required fields, permission and the workspace: the workspace has to exist and must not be archived, and TILL turns account numbers into ids. A missing field or a missing right ends the call here. A key with a prefix that only TILL itself mints is refused here too.
  2. The argument check: if the key arrived before with different arguments, TILL answers idempotency_key_conflict and names the fields that differ. The tool does not run at all.
  3. The shape check: date, source, currency and rate. These checks read nothing from the books.
  4. The replay: if the key is already spent, the stored result comes back.
  5. The checks against the books: accounts, debits equal to credits, VAT, locked periods, drafts already posted.
  6. The act: the entry, the result and the arguments are written in the same transaction. A crash leaves all of it or none of it.

A draft shows why the replay comes before step 5. An agent posts a saved draft and loses the response. If it retries with the same key, it gets the same entry id back. If the “already posted” check ran before the replay, it would get already_posted instead and have to guess whether its attempt worked. That answer goes only to a caller who tries to post the same draft a second time under a new key.

What does a refusal do to the key?

A refusal that wrote nothing does not spend the key. The agent fixes the call and sends it again under the same key. Stripe describes the same rule: a request that fails parameter validation is not saved and may be retried. The answers below come from TILL, for a direct call with the arguments above (a person posting on their own seat; the agent’s proposals follow in the next section). First a typo on the credit side, 48560 instead of 48650:

{ "debit": 48650, "credit": 48560, "diff": 90, "ok": false, "error": "unbalanced" }

The journal is still empty. The corrected call under the same key posts, and every later retry returns the same entry id. But when the same key arrives with different lines, that is not an attempt any more. It is a different operation:

{
  "ok": false,
  "error": "idempotency_key_conflict",
  "idempotencyKey": "bill:muster-versicherung:2026-1187",
  "action": "post_entry",
  "fields": ["lines"],
  "reason": "Same key, different lines. If this is the same act, resend the original arguments; only a genuinely different act takes a new key."
}

The answer names the field so the model can decide: resend the same operation with its original arguments, or derive a new key for an operation that really is new. The IETF draft gives this case status 422, and Stripe returns an error. Here agents walk into a trap. Language models love to rephrase. In TILL the description is part of the entry, and a retry that only rewords it is refused with fields: ["description"]. Only a free-text field named reason is left out of the comparison by default, and the setting idempotency.hash_free_text switches that. So on a retry, an agent resends its arguments word for word.

New since 02.11.2026: the argument check for every write tool arrived after version 1.0.0-beta.2 of 28.10.2026, so that release does not have it. Before it, payments, bank bookings and a few other paths already refused such a call, but post_entry returned the first entry for a reused key with different arguments.

How do retries work when a person approves the write?

A proposal needs the same idempotency as a posting, or the person sees two proposals for one invoice. At the default dial, “ask”, an agent writes nothing to the ledger in TILL: its post_entry files a proposal. When it repeats the same call, it gets the same proposal back:

{ "ok": true, "drafted": true, "actionId": "…", "status": "pending", "replayed": false }
{ "ok": true, "drafted": true, "actionId": "…", "status": "pending", "replayed": true }
The first call and the retry, shortened. Both name the same actionId; the queue holds one proposal.

The same key with different lines is refused here too, and the answer names the actionId of the existing proposal. When a person approves it, TILL replays the stored call under the stored key. Approving twice therefore posts once, and an agent’s retry after the approval returns the entry’s result. In the example: approve, approve again, the agent retries, one entry. Who approves what, and when, is the subject of the next part of this series.

What about side effects outside the database?

An email cannot be rolled back, so the intent has to be recorded before the send. TILL’s send_invoice records that a send is starting, in its own transaction, before it calls the transport. On a retry there are three cases:

  • The send started and no result was recorded: the outcome is unknown. TILL refuses with send_outcome_unknown, and a person resolves it.
  • The email provably went out and only the records are missing: TILL writes the records and sends nothing.
  • The transport reports clearly that nothing left: the intent is cleared, and a later attempt may send.

Outside the transaction, the key alone is not enough. The server needs a recorded intermediate state, and when it cannot know the outcome it stops instead of guessing. In the public beta, TILL’s MIT core ships no email transport of its own.

What idempotency keys do not solve

  • Two different keys for the same invoice are two operations. If an agent derives the key once from the invoice number and once from the file name, the key does not help. In TILL, a separate check added on 03.11.2026 catches the same receipt being booked a second time (not in any released version yet).
  • The key does not protect against a bad derivation. Leave the supplier out of the key, and two invoices with the same number collide: the second is refused as a conflict.
  • TILL does not expire keys. The IETF draft asks a server that purges keys to document its expiry policy; Stripe may prune keys after 24 hours on API v1 and counts retries within 30 days on API v2. In return, TILL’s keys do not go into a backup or an export: a restored workspace remembers no spent keys.
  • TILL does not answer concurrent calls with an “in progress” status like the IETF draft’s 409. SQLite writes one at a time, and the argument check runs again under the write lock. A call that waits too long for the lock gets store_busy with retryable: true.

A checklist for MCP write tools

Anyone who builds write tools for agents, for any system, can check these points:

  1. Every tool that creates or moves something requires a key. Exceptions only set an absolute state, and each has a written reason.
  2. The tool description tells the model what to derive the key from: the business fact, not the clock, chance or the call id.
  3. The key is scoped per workspace and per tool, enforced by a primary key in the database.
  4. The result and the act are written in the same transaction.
  5. The argument check and the replay come before every check that reads current state.
  6. The same key with different arguments is refused, and the answer names the fields that differ.
  7. A refusal that wrote nothing does not spend the key.
  8. Proposals are idempotent too: the same key returns the same pending proposal.
  9. For effects outside the database, the intent is recorded first, and an unknown outcome goes to a person.
  10. idempotentHint appears only where it holds.
  11. A test calls every write tool twice and requires that the second call changes nothing in the database.

TILL is a public beta and open source under MIT. How an agent posts to a ledger in the first place is in part 1 of this series. What an agent may do in TILL is on the agents page, and what TILL protects against is on the security page.

Questions people ask

What is an idempotency key?

A value a client sends with a write so the server can recognise a retry. When the same key arrives again, the server returns the first result instead of acting a second time. The IETF draft requires that a key is never reused for a request with a different payload.

Should an AI agent use a UUID as its idempotency key?

Only if the client holds the UUID across every retry. A language model that retries a step in a new turn mints a new UUID, and the server sees a new operation. A key derived from the business fact, such as supplier and invoice number, without personal data, is safer.

What does idempotentHint mean in MCP?

A tool with idempotentHint: true declares that calling it again with the same arguments has no additional effect. It is a hint, not a guarantee, and the MCP schema warns against relying on it for untrusted servers. MCP itself carries no key, neither in 2025-11-25 nor in the current 2026-07-28 revision.

What happens if an agent sends the same key with different arguments?

A careful server refuses instead of passing the old result off as the new one. TILL answers idempotency_key_conflict with the fields that differ, and nothing is written. The IETF draft gives this case status 422.

Does a refused call use up the key?

Not in TILL, as long as the refusal wrote nothing. The agent fixes the error and sends again under the same key. Stripe does the same for requests that fail parameter validation.

How long should a server keep idempotency keys?

The server decides, and the IETF draft says it should publish the policy. Stripe may prune keys after 24 hours on API v1 and counts a retry within 30 days on API v2. TILL does not expire them, but it leaves them out of backups.