The Lawfare Academy

Lawfare Group → Knowledge Institute → Lawfare Academy

← Back to program

← AI 320 — Applied AI, Retrieval and Reliable Automation

Weeks 9–11

Tool use, state machines and idempotent workflows

Learning objectives

  • Explain and trace the mechanisms in tool use, state machines and idempotent workflows.
  • Apply the method to the worked example and changed-condition exercise.
  • Identify assumptions, failure cases and the evidence needed to justify a result.

Current study directions: use the original Academy preparation chapters, preserved teaching and complete optional worked reasoning on LawfareGroup.com. Retained attributed readings, source exercises and historical assignment directions are optional further study. This notice supersedes earlier textbook-supported directions preserved below and in historical companions. Independent subject review and observed learner testing remain pending.

An agentic workflow chooses actions in a changing environment. That flexibility increases the importance of explicit state, limited permissions and recovery behavior. A tool description is not an access-control system. The application must verify the caller, constrain arguments and authorize every consequential action.

Retries are unavoidable when networks fail, but repeating an action may duplicate its effect. An idempotency key identifies a logical operation so repeated delivery can reuse or recognize the same result. The key must be stable across retries and scoped to the authorized action. It cannot be regenerated on every attempt. For persistent multi-worker systems, storing the key and state transition needs appropriate transactional or atomic coordination; a local set is only a teaching demonstration.

Separate proposal, approval and execution states. Approval should attach to the exact action, target and content, with expiry or reapproval when material fields change. A model suggesting a different recipient does not inherit approval for the original recipient. Record enough state to reconcile an ambiguous timeout: the server may have executed an action even when the caller did not receive its response. Never treat a timeout as proof that nothing happened.

Worked reasoning The simulation performs no network action. It compares the proposed dictionary with the approved dictionary and tracks a stable operation ID. The local set is lost on restart and is not safe for concurrent workers. A production action needs durable state and permission checks; copying this toy tracker into a multi-user service would not provide reliable exactly-once behavior.

Extended teaching and laboratory

An action workflow separates proposed, validated, approved, executing and completed states. The approval should bind a stable payload fingerprint and operation identifier. A local set can demonstrate deduplication within one process, but cannot protect concurrent workers or survive restart. Durable storage and atomic state transitions are needed at the actual action boundary.

Distributed systems can fail after an external service performs an action but before the local system records success. A retry policy must reconcile that uncertain outcome using the provider’s supported idempotency or lookup mechanism. Claiming exactly-once delivery from a local flag alone conceals this failure window.

Worked problem A worker sends request R, the provider performs it, and the worker crashes before recording completed. Explain what the retry worker must avoid.

Step-by-step reasoning 1. It must not assume that absence of a local completion flag proves the action never happened.

  1. Reuse the stable operation identifier with an external idempotency mechanism where supported, or reconcile against the provider’s recorded action before retrying.
  1. Store the approved payload fingerprint durably and reject a changed payload under the same operation ID.
  1. Record uncertain outcomes explicitly and route unresolved ones to review. A second worker must not independently approve the same action through a non-atomic local check.

Apply the idea

A worker sends request R, the provider performs it, and the worker crashes before recording completed. Explain what the retry worker must avoid.

Two workers both read approved before either writes executing. Give the coordination property needed to prevent both from owning the action.

Read the worked reasoning

Use an atomic compare-and-set or transactional claim that lets only one worker move that operation from approved to executing. The loser must not proceed. This prevents a local race but does not by itself solve the external-success/local-crash window; the external reconciliation policy is still needed.

Check understanding

What should a timeout after sending an action imply?

  1. The action definitely did not happen
    Feedback

    Network failure does not establish server state.

  2. The operation should receive a new ID immediately
    Feedback

    A new ID can defeat duplicate protection and repeat the effect.

  3. The outcome may be unknown and needs reconciliation
    Feedback

    The response can be lost after the effect occurred.

Further reading

Current study directions: use the original Academy preparation chapters, preserved teaching and complete optional worked reasoning on LawfareGroup.com. Retained attributed readings, source exercises and historical assignment directions are optional further study. This notice supersedes earlier textbook-supported directions preserved below and in historical companions. Independent subject review and observed learner testing remain pending.

Use the preserved original investigation and connected Academy preparation/application below. Compare your reasoning with its optional complete model. Historical source directions remain in immutable edition4.

My learning →