Skip to main content

Deterministic execution guard for AI agents: request-id dedup + finality gating + durable state.

Project description

PyPI version Python versions License

Exactly-once execution guard for AI agent side effects.

Demo

SafeAgent Demo

SafeAgent prevents duplicate, replayed, or premature irreversible actions triggered by LLM agents or distributed workflows.

Typical protected actions include:

  • payments
  • emails / notifications
  • tickets
  • trades
  • tournament payouts
  • financial settlement

Installation

pip install safeagent-exec-guard

Python 3.10+ required.


Why SafeAgent Exists

AI systems retry operations constantly.

Examples:

  • agent loops retry tool calls
  • HTTP clients retry failed requests
  • queue workers replay jobs
  • orchestrators restart workflows

Without protection this can cause:

retry -> duplicate payment
retry -> duplicate email
retry -> duplicate ticket
retry -> duplicate payout

SafeAgent inserts an execution guard between the decision and the irreversible side effect.

agent decision

request_id generated

SafeAgent execution guard

side effect executes once

future retries return cached receipt


Minimal Example

from safeagent_exec_guard import SettlementRequestRegistry

registry = SettlementRequestRegistry()

def send_email(payload): print("SENDING EMAIL:", payload["to"])

receipt = registry.execute( request_id="email:C123:invoice", action="send_email", payload={"to": "c123@example.com"}, execute_fn=send_email, )

print(receipt)

If the same request_id runs again, the side effect is NOT executed again.
SafeAgent returns the stored receipt.


Decorator API

from safeagent_exec_guard import SettlementRequestRegistry, safeagent_guard

registry = SettlementRequestRegistry()

@safeagent_guard( registry=registry, action="send_email", request_id_fn=lambda payload: f"email:{payload['to']}:{payload.get('template','default')}", ) def send_email(payload): print("REAL SIDE EFFECT:", payload["to"])

send_email({"to": "user@example.com", "template": "invoice"}) send_email({"to": "user@example.com", "template": "invoice"})

The second call returns the cached receipt instead of executing the side effect again.


MCP Example

from safeagent_exec_guard import SettlementRequestRegistry from safeagent_exec_guard.mcp import safe_mcp_tool

registry = SettlementRequestRegistry()

@safe_mcp_tool( registry=registry, action="send_payment", request_id_fn=lambda payload: f"payment:{payload['recipient']}:{payload['amount']}", ) def send_payment(amount: float, recipient: str): print(f"REAL SIDE EFFECT: sending ${amount} to {recipient}")

Run the demo:

python examples/mcp_retry_demo.py

What it shows:

  1. payment executes on first call
  2. retry with the same logical action returns a cached receipt
  3. the payment side effect runs exactly once

Framework Examples

OpenAI-style tool execution

python examples/openai_tool_safeagent.py

LangChain example

python examples/langchain_safeagent.py

CrewAI example

python examples/crewai_safeagent.py

Decorator example

python examples/decorator_safeagent.py

LangChain adapter example

python examples/langchain_adapter_safeagent.py

MCP retry demo

python examples/mcp_retry_demo.py


Failure Semantics

SafeAgent records a durable execution receipt for each request_id.

Retry behavior

same request_id -> return stored receipt

The side effect is never executed again.

Timeout after execution

execution completed
response lost
caller retries

SafeAgent detects the existing receipt and returns it.

Partial failures

SafeAgent does NOT attempt automatic rollback.

Applications should handle partial commits with:

  • audit logs
  • reconciliation processes
  • compensating actions

SafeAgent guarantees no duplicate execution, not business policy validation.


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.11.tar.gz (72.7 kB 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.11-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: safeagent_exec_guard-0.1.11.tar.gz
  • Upload date:
  • Size: 72.7 kB
  • 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.11.tar.gz
Algorithm Hash digest
SHA256 e804d8e4c714efca18b6186f0c50a1b896dd96727e55b7424b2040d55afa38da
MD5 a2b537bd89bcc706f47e4a81792992c5
BLAKE2b-256 2c00f3300dc76a9ac99cdc73a4b5c273ee09713c2c68a48a71b3d3b400bb94b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for safeagent_exec_guard-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 c41b90fe013bae2111b280109eff788e4f1545256f9d9a23e2e3ee0e1bb5d252
MD5 3409a12dd5b3d2bc1f94cdcaf306eb5c
BLAKE2b-256 188516d01852b9bdb144610b6efcbd4f604c73305df8c6820024954e6eff0e62

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