Idempotency Key Generator

Easy
Agents

Implement idempotency key management:

  1. generate_key(tool_name, params): Create deterministic hash from tool name and params dict
  2. is_executed(key): Check if key exists in executed set
  3. mark_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 == key2
Output: True
Explanation: 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: True
Explanation: 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
        pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews