Q&A-2Design a production-grade tool contract.
Answer
Opening. Before writing anything I'd pin down three things: who calls this, what the blast radius is, and whether the call is safe to retry. I'll assume an LLM agent doing function calling against a JSON Schema, backed by an HTTP service, and that the operation moves money. The contract then has two halves: the schema the model sees, and the semantics the server enforces. The schema is necessary and never sufficient: a model can emit schema-valid arguments that are semantically wrong, so every constraint the schema can't express has to be enforced server-side and returned as a structured error.
The schema.
{
"name": "refund_order",
"description": "Refund a captured payment. Irreversible. Call only after the customer confirms.",
"input_schema": {
"type": "object",
"additionalProperties": false,
"required": ["order_id", "amount_minor", "currency", "reason", "idempotency_key"],
"properties": {
"order_id": {"type": "string", "pattern": "^ord_[0-9a-f]{24}$"},
"amount_minor": {"type": "integer", "minimum": 1, "maximum": 100000000},
"currency": {"type": "string", "enum": ["USD", "EUR", "GBP"]},
"reason": {"type": "string", "enum": ["duplicate", "fraud", "customer_request", "other"]},
"idempotency_key": {"type": "string", "minLength": 16, "maxLength": 64},
"note": {"type": "string", "maxLength": 500}
}
}
}Four decisions in there. Money is an integer in minor units plus an ISO 4217 enum, never a float: 19.99 as a double is a rounding bug waiting for a reconciliation job. additionalProperties: false makes a hallucinated field a hard validation error instead of a silently dropped one. reason is an enum so approval routing and analytics are total functions. And the description carries the effect class, because "Irreversible" is the token the model actually conditions on before calling.
What the schema can't say.
| Concern | Where it lives | Concrete rule |
|---|---|---|
| Caller identity | Auth context, never an argument | Tenant comes from the token; order_id outside it returns 404, not 403, so existence doesn't leak |
| Idempotency | Required argument | Same key + same body replays the stored response; same key + different body is 422 |
| Concurrency | If-Match ETag on the order | Stale version returns 409 with the current version in the body |
| Timeout | Server budget | 10 s hard cap; on cancel the refund is not rolled back: it may already be committed |
| Errors | RFC 9457 application/problem+json | type, title, status, detail, plus a retryable boolean the agent can branch on |
Trade-offs. Strict versus permissive schema: additionalProperties: false catches hallucinated fields but breaks forward compatibility the moment you add one, so you version the tool rather than loosen the schema. Idempotency key versus natural key (order_id + amount): the natural key needs no client state but blocks legitimate partial refunds, so the explicit key wins. Sync versus async: the refund API call returns immediately and settlement is asynchronous, so a synchronous tool is fine; anything that can exceed the 10 s budget should return 202 plus a poll tool, because a model that times out will retry, and without idempotency you now have two refunds.
Failure modes. Duplicate refund from a retry after timeout: detected by idempotency-key replay, and the reason the key is required rather than optional. Stale write when two agents refund the same order concurrently: detected by ETag mismatch, surfaced as 409. Prompt-injected arguments, where a fetched page tells the agent to refund a different account: detected only by the server-side ownership check; the schema cannot stop it. Silent truncation of note: reject over-length input rather than trimming, because trimming hides a model bug you want to see.
Versioning. Additive-only within a major version. A new required field means a new tool name (refund_order_v2), with v1 kept for a deprecation window and calls to it logged. Pick an idempotency retention window and document it, Stripe retains keys for 24 hours, because that window is the real bound on how long a retry is safe.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.