Implement idempotency key management:
generate_key(tool_name, params): Create deterministic hash from tool name and params dictis_executed(key): Check if key exists in executed setmark_executed(key): Add key to executed set
Key Requirements:
- Same tool_name + params must generate identical key
- Use SHA256 hash
- Include sorted params to ensure consistency
- Return hex digest of first 16 chars (for brevity)
Use Case: Prevents duplicate API calls when agent retries or loops back.
Examples
Example 1:
Input:
mgr = IdempotencyManager(); key1 = mgr.generate_key('add', {'x': 1, 'y': 2}); key2 = mgr.generate_key('add', {'y': 2, 'x': 1}); key1 == key2Output:
TrueExplanation: Same params in different order should produce same key
Example 2:
Input:
mgr = IdempotencyManager(); key = mgr.generate_key('test', {}); mgr.mark_executed(key); mgr.is_executed(key)Output:
TrueExplanation: After marking, key shows as executed
Starter Code
import hashlib
import time
class IdempotencyManager:
"""
Manages idempotency keys for agent tool calls.
Prevents duplicate execution of the same operation.
"""
def __init__(self):
self.executed_keys = set()
def generate_key(self, tool_name, params):
"""
Generate deterministic idempotency key from tool + params.
Should return same key for same inputs.
"""
# Your implementation here
pass
def is_executed(self, key):
"""Check if key was already executed"""
# Your implementation here
pass
def mark_executed(self, key):
"""Mark key as executed"""
# Your implementation here
passPython3
ReadyLines: 1Characters: 0
Ready