
Add Agent Safety to Any LangChain Tool in Two Lines
You have a LangChain agent with tool access. It can run shell commands, call APIs, modify files. It works great in development. Then you give it production credentials and it does something you didn't expect. The fix is two lines. The Problem With Tool Access Today When you define a LangChain tool, there's nothing between the model's decision and the execution: from langchain.tools import tool @tool def run_bash ( command : str ) -> str : """ Execute a bash command and return the output. """ import subprocess return subprocess . check_output ( command , shell = True ). decode () The model decides to call run_bash . It runs. No questions asked. If the model decides rm -rf /tmp/important_data is the right move, that's what happens. No log. No gate. No way to know it happened until something is broken. The Fix: @safe_tool Canopy 0.2.1 ships a decorator that wraps any function with a policy check before execution: from langchain.tools import tool from canopy import safe_tool @tool @safe_to
Continue reading on Dev.to Python
Opens in a new tab




