> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hex65.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Operations agent

> The operations example end to end, with intercepted effects, tool timeouts, and a check you write yourself.

The example agent works failed-payment tickets. It reads the ticket and the account, charges the amount due, and replies; an expired card goes to a person. When a charge times out, it tries once more. It calls no model.

## Run it

```sh theme={null}
mkdir operations-guide && cd operations-guide
proving init --example operations
proving connect
proving record --inputs tasks.jsonl --yes
proving redact
proving twin build
proving run --seed 1
proving results
```

```text Output theme={null}
Pass rate: 90% [66.8%, 97.58%] at 95%, n = 25, cases = 5, effective n = 15.8
  pass^1: 90%, too few cases for an interval, cases = 5
  pass^2: 90%, too few cases for an interval, cases = 5
  pass^4: 90%, too few cases for an interval, cases = 5
  Stratum flaky_billing (weight 0.1, dangerous): 0% [0%, 24.25%] at 95%, n = 12, cases = 2, effective n = 12
  Stratum typical (weight 0.9): 100% [77.19%, 100%] at 95%, n = 13, cases = 3, effective n = 13
  Stratum other (weight 0): no laps, not covered by this run
  Cluster double_charge / send / retried_charge_is_idempotent / ticketing.send_reply / condition:tool_timeout / tool_timeout: 12 failures, attribution tool (clu_<id>)
```

## Timeouts on purpose

Two cases carry a condition: the billing system's `charge` times out on the first call.

```yaml theme={null}
conditions:
  - type: tool_timeout
    tool: billing.charge
    times: 1
```

The twin answers that call with a timeout. The charge that timed out counts as attempted, not done, so the agent's retry is the one effect in the ledger, and the default effect checks pass.

## A check you write

What the default checks cannot see is that the retry was unsafe: in the real world a timed-out charge may have gone through, and a second charge with nothing to tell the two apart charges the customer twice. The example's own deterministic check catches it:

```yaml theme={null}
- key: retried_charge_is_idempotent
  type: deterministic
  category: send_safety
  station: send
  detects: double_charge
  required: true
  description: A charge sent more than once carries an idempotency key on every attempt, so a retry can never charge twice.
  assert:
    - op: any
      assert:
        - select: "tool_calls[?tool=='billing.charge']"
          op: count
          value:
            lte: 1
        - select: "tool_calls[?tool=='billing.charge']"
          op: json_schema
          value:
            type: array
            items:
              type: object
              required: [args]
              properties:
                args:
                  type: object
                  required: [idempotency_key]
```

Selectors read the lap's record (its tool calls, effects, messages and answer). `proving checks test <check> --lap <lap>` shows what a selector picks on a real lap. The format is on [Checks](https://docs.hex65.com/reference/formats/checks.md).

## Fix it

Send the same idempotency key on every attempt of one charge, for example the ticket's id:

```js theme={null}
const charge = () =>
  billing.call("charge", { account_id: account.id, amount: account.amount_due, idempotency_key: `collect-${id}` });
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.