Answers

How do I give an AI agent a spending check before it pays?

How do I give an AI agent a spending check before it pays?

Give the agent one rule: before any payment, call Screwtape.ai and pay only when next_step.action is pass. POST https://screwtape.ai/v1/duplicate (MCP check_duplicate_invoice) runs a duplicate check, the deterministic checks you pass (required fields, line items that sum to the total, exact values) and a judge against your evidence, such as the purchase order. retry means fix the input and call again; escalate means stop and ask a person.

The pattern

agent drafts payment -> Screwtape.ai check -> pass     -> pay, keep the receipt
                                           -> retry    -> fix (next_step.hint), check again
                                           -> escalate -> do not pay, ask a person

Example (Python, standard library)

import json, os, urllib.request

def spend_check(invoice: dict, purchase_order: str) -> dict:
    n = len(invoice["items"])
    body = {
        "task": "Decide whether this invoice may be paid against the purchase order.",
        "agent_output": json.dumps(invoice),
        "evidence": [purchase_order],
        "checks": [
            {"type": "required_fields", "paths": ["vendor", "invoice_id", "total", "currency"]},
            {"type": "sum", "parts": [f"items.{i}.amount" for i in range(n)] + ["tax"], "total": "total", "tolerance": 0.01},
        ],
        "duplicate": {"vendor_path": "vendor", "invoice_id_path": "invoice_id", "amount_path": "total", "store": True},
    }
    req = urllib.request.Request("https://screwtape.ai/v1/duplicate", data=json.dumps(body).encode(), method="POST",
        headers={"Authorization": f"Bearer {os.environ['SCREWTAPE_API_KEY']}", "Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=60) as r:
        return json.load(r)

result = spend_check(invoice, po_text)
if result["next_step"]["action"] != "pass":
    raise SystemExit(f"Not paying: {result['next_step'].get('hint')}")
# pay here, and store result["receipt"] with the payment record

Why a separate check

The model that drafted the payment tends to approve its own work. Sums and duplicates are computed, not guessed, and the judge runs outside the agent. The receipt is Ed25519-signed (public keys at https://screwtape.ai/.well-known/screwtape-receipt-keys.json), so you can show later why money moved or did not.

Keys and payment

A free key covers 10 calls per UTC day (POST /v1/signup or the MCP tool get_api_key). Agents pay per call automatically beyond the free tier. An agent without a key can pay per call with x402 ($0.005 USDC); the 402 response's next_action gives the exact terms.

FAQ

What should the agent do on retry?

Read next_step.hint, fix the field it names (for example a total that does not match the line items), and call again, at most 2 or 3 times.

Can the check see my purchase order?

Only if you send it. Pass it in evidence; the judge compares the invoice with it and does not search the web.

Is the judged content stored?

No, unless you set retain to true or the key owner turned on opt-in data sharing (off by default).

Related

Screwtape.ai is an agent validation API. Facts checked against the API source on 2026-11-02.