import tracely_ai import traceback class DedupedToolCallMonitor: def __init__(self): self.failure_signatures = set() def record_failure(self, agent_id, tool_name, exc: Exception): tb_str = ''.join(traceback.format_exception(type(exc), exc, exc.__traceback__)) sig = (agent_id, tool_name, type(exc).__name__, tb_str.split('\n')[0]) if sig not in self.failure_signatures: self.failure_signatures.add(sig) tracely_ai.log_failure( agent_id=agent_id, tool_name=tool_name, error=tb_str[:500] ) else: tracely_ai.log_info(f"Suppressed duplicate tool-failure: {sig}") def run_tool_with_monitoring(agent_id, tool, *args, deduper): try: return tool(*args) except Exception as exc: deduper.record_failure(agent_id, tool.__name__, exc) raise # Bubble up for higher-level recovery