from veto import AsyncVetoClientasync def check_authorization(): async with AsyncVetoClient(api_key="veto_...") as client: result = await client.authorize("agent-uuid", "send_email", { "to": "user@example.com", "subject": "Refund confirmation", }) if not result.allowed: print(f"Blocked: {result.reason}")
Wrap any LangChain tool function with Veto authorization. If the action is denied, the wrapper returns the denial reason as a string instead of executing the tool.
from veto import VetoClient, VetoErrorveto = VetoClient(api_key="veto_...")def veto_tool_wrapper(tool_func, tool_name: str, agent_id: str): """Wrap any LangChain tool with Veto authorization.""" def wrapper(*args, **kwargs): result = veto.authorize(agent_id, tool_name, kwargs) if not result.allowed: return f"Authorization denied: {result.reason}" return tool_func(*args, **kwargs) wrapper.__name__ = tool_func.__name__ wrapper.__doc__ = tool_func.__doc__ return wrapper# Usagedef web_search(query: str) -> str: return search_the_web(query)web_search = veto_tool_wrapper(web_search, tool_name="web_search", agent_id="agent-uuid")
from veto import VetoError, UnauthorizedError, RateLimitErrorimport timetry: result = client.authorize("agent-uuid", "send_email", { "to": "user@example.com", }) if not result.allowed: print(f"Denied: {result.reason}")except RateLimitError as e: # back off and retry print(f"Rate limited — retry after {e.retry_after_ms}ms") time.sleep(e.retry_after_ms / 1000)except UnauthorizedError: # bad or expired API key print("Invalid API key — check VETO_API_KEY")except VetoError as e: # any other Veto error (timeout, network, server error) print(f"{e.code}: {e}")