from langchain_core.tools import tool
from loopguard import LoopGuardCallbackHandler, LoopGuardInterrupt
@tool
def search(query: str) -> str:
"""Search for a source; this example always returns no results."""
return "no results"
handler = LoopGuardCallbackHandler()
handler.raise_error = True
try:
for _ in range(3):
search.invoke({"query": "pricing page"}, config={"callbacks": [handler]})
except LoopGuardInterrupt as exc:
print("LoopGuard stopped the run:", exc)
report = handler.report()
print("Decision:", report["decisions"][-1]["action"])
print("Tool calls:", report["metrics"]["tool_calls"])