Neural Mastery
← Practice
Idempotency-Aware Retry Safety Checkmedium

Idempotency-Aware Retry Safety Check

medium Agents in Production 15 min

NumPy allowed · plain Python earns 10 bonus points

Implement a real retry-safety check combining a tool's declared idempotency with the actual failure mode, deciding whether an agent orchestrator may automatically retry a failed tool call.

Task

Implement is_safe_to_retry(annotations, failure_reason). Retry is safe if EITHER: (a) annotations.get('idempotentHint', False) is True (repeating it has no additional effect), OR (b) failure_reason == 'timeout' AND annotations.get('readOnlyHint', False) is True (a read that may not have even reached the server is safe to redo). Otherwise return False.

Function Signature

is_safe_to_retry(annotations: dict, failure_reason: str) -> bool

Examples

Example 1: idempotent tool always safe
Input: annotations = {'idempotentHint': True} failure_reason = 'server_error'
Output: True
Example 2: read-only tool safe on timeout
Input: annotations = {'readOnlyHint': True} failure_reason = 'timeout'
Output: True
Example 3: read-only tool unsafe on non-timeout failure
Input: annotations = {'readOnlyHint': True} failure_reason = 'server_error'
Output: False

Constraints

  • failure_reason is a string
Python3Saved ✓
def is_safe_to_retry(annotations, failure_reason):
# Your implementation here
pass

Case 1: idempotent tool always safe
Input: annotations = {'idempotentHint': True} failure_reason = 'server_error'
Expected: True
Case 2: read-only tool safe on timeout
Input: annotations = {'readOnlyHint': True} failure_reason = 'timeout'
Expected: True
Case 3: read-only tool unsafe on non-timeout failure
Input: annotations = {'readOnlyHint': True} failure_reason = 'server_error'
Expected: False