Skip to main content

AI agent execution guard — prevents duplicate actions from retries with exactly-once execution, request-id dedup, and durable receipts.

Project description

SafeAgent

AI agents retry. Retries fire side effects twice.

Duplicate payment. Duplicate email. Duplicate trade. Duplicate ticket.

SafeAgent is an execution guard that sits between an agent decision and an irreversible action. It gives every tool call a request ID, records a durable receipt on first execution, and returns that receipt on every retry — without running the side effect again.

pip install safeagent-exec-guard

Python 3.10+ · Apache-2.0 · Live demo


The problem

agent calls tool
    ↓
network timeout
    ↓
agent retries
    ↓
side effect runs twice

Most agent frameworks handle retries at the transport layer. None of them know whether the side effect already happened. SafeAgent does.


Quickstart

from settlement.settlement_requests import SettlementRequestRegistry

registry = SettlementRequestRegistry()

def send_invoice():
    print("Sending invoice...")

# First call — executes the side effect
receipt = registry.execute(
    request_id="invoice:C123",
    action="send_invoice",
    payload={"to": "c123@example.com"},
    execute_fn=send_invoice,
)

# Retry with the same request_id — returns the original receipt, no second send
receipt = registry.execute(
    request_id="invoice:C123",
    action="send_invoice",
    payload={"to": "c123@example.com"},
    execute_fn=send_invoice,
)

Same request_id → original receipt returned → side effect runs exactly once.


Works with any agent framework

  • OpenAI tool calls
  • LangChain tools
  • CrewAI actions
  • Claude / MCP tool execution
  • Any Python function that touches a real system

How it works

Every execution goes through a four-step control plane:

Agent decision
    → Finality gate      (is this outcome confirmed?)
    → Request-ID dedup   (has this exact call run before?)
    → Execute once       (run the side effect)
    → Receipt stored     (durable, survives restarts)

State machine: OPEN → RESOLVED → IN_RECONCILIATION → FINAL → SETTLED

Execution is only permitted from FINAL. Replays at any state return the stored receipt.


Key properties

  • Exactly-once execution — same request_id never fires twice
  • Durable receipts — SQLite-backed, survives process restarts
  • Finality gating — blocks execution on ambiguous agent signals
  • Confidence thresholds — auto-finalizes when consensus exceeds threshold
  • Audit trail — every execution recorded with payload and outcome

Run the demos

# Duplicate execution prevention
python examples/safe_agent_demo.py

# Stochastic agent signal simulation
python examples/simulate_ai.py

# Restart safety (run twice)
python examples/persist_demo.py
python examples/persist_demo.py

Production use

SafeAgent is a reference implementation and pattern library. If you're deploying this in a production agent system, see LICENSING.md for commercial options.


License

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safeagent_exec_guard-0.1.14.tar.gz (7.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safeagent_exec_guard-0.1.14-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file safeagent_exec_guard-0.1.14.tar.gz.

File metadata

  • Download URL: safeagent_exec_guard-0.1.14.tar.gz
  • Upload date:
  • Size: 7.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for safeagent_exec_guard-0.1.14.tar.gz
Algorithm Hash digest
SHA256 1059a0699f3419684f2b51601d90bf6535d5485a534ecca7692e850c5d0aa554
MD5 a12551a37612f59311aae5f25772ddf3
BLAKE2b-256 023d44aa2571223d8a8351fdd29857c121f441ff262428fcd8a141a4413e71b3

See more details on using hashes here.

File details

Details for the file safeagent_exec_guard-0.1.14-py3-none-any.whl.

File metadata

File hashes

Hashes for safeagent_exec_guard-0.1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 82177711848c57dba23789b5a8662549a954df4c8d93dd6325f1be8579392abf
MD5 51752b4b005d986f32533cfc08db953c
BLAKE2b-256 95b3255c20121ec5bf0eb477dadc21cc93bb88fce0ae92a73bd647ea020a7caf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page