SDK LangChain & LangGraph
Le BoundryCallbackHandler s'intègre au système d'événements asynchrones de LangChain et LangGraph. Chaque appel d'outil est intercepté avant son exécution réelle sur votre infrastructure.
Paramètres du CallbackHandler
from boundry.sensor import BoundryCallbackHandler
handler = BoundryCallbackHandler(
api_key="sk_org_...",
agent_id="support-agent-prod",
endpoint="https://api.boundry.io",
timeout_ms=500,
fail_open=False,
raise_on_blocked=True,
anonymize_dlp=True
)
Description des options
| Paramètre | Type | Défaut | Description |
|---|---|---|---|
api_key | str | env.BOUNDRY_API_KEY | Clé secrète de l'organisation. |
agent_id | str | "default-agent" | Nom de l'agent affiché sur le tableau de bord. |
endpoint | str | "https://api.boundry.io" | URL de la passerelle SecOps. |
timeout_ms | int | 500 | Délai maximal d'interception en millisecondes. |
fail_open | bool | False | Si False (Mode Fail-Secure), bloque l'action si le proxy est injoignable. |
raise_on_blocked | bool | True | Lève une SecurityPolicyException si l'action est rejetée. |
anonymize_dlp | bool | True | Masque les numéros de carte bancaire, clés API et emails (DLP). |
Exemple LangChain classique
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent, AgentType
from boundry.sensor import BoundryCallbackHandler
# Initialisation
handler = BoundryCallbackHandler(
agent_id="customer-service-bot",
raise_on_blocked=True
)
llm = ChatOpenAI(model="gpt-4o", temperature=0)
# Lier le handler
agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.OPENAI_FUNCTIONS,
callbacks=[handler]
)
try:
response = agent.run("Supprimer l'utilisateur #8812 de la base.")
except Exception as e:
print(f"Action interceptée par le proxy SecOps : {e}")
Exemple LangGraph (StateGraph)
Dans LangGraph, passez le handler via la configuration d'invocation (callbacks=[handler]) :
from langgraph.graph import StateGraph, START, END
from boundry.sensor import BoundryCallbackHandler
secops_handler = BoundryCallbackHandler(
agent_id="finance-agent-v2",
fail_open=False
)
workflow = StateGraph(AgentState)
workflow.add_node("agent", call_model)
workflow.add_node("tools", call_tools)
workflow.add_edge(START, "agent")
app = workflow.compile()
# Exécution sécurisée
result = app.invoke(
{"messages": [("user", "Exécuter le virement comptable de 10 000 €")]},
config={"callbacks": [secops_handler]}
)
Gestion des pannes réseau : Fail-Secure vs Fail-Open
- Mode Fail-Secure (
fail_open=False, recommandé) : Si le réseau subit une coupure et que le proxy Boundry ne répond pas sous 500ms, l'action est bloquée par mesure de précaution. - Mode Fail-Open (
fail_open=True) : Si le proxy ne répond pas, l'action est autorisée avec avertissement local pour garantir 100% de continuité de service.