LangChain and LangGraph
Use CocoGuard on agents built with upstream LangChain and LangGraph.
Use CocoGuard on agents built with upstream LangChain and LangGraph.
CocoGuard attaches as middleware on create_agent. It's the same guard as on coco-framework, built on upstream's middleware class.
Prerequisites: The SDK, installed with [langchain], which brings langchain and langgraph 1.0 or later.
Attach the guard
Build an engine and add the guard:
from langchain.agents import create_agent
from langgraph.checkpoint.memory import InMemorySaver
from coco_sdk import Engine
from coco_sdk.langchain import CocoGuard
engine = Engine.from_path("contracts/", mode="enforce", ledger=".coco/ledger.db")
agent = create_agent(model, tools, middleware=[CocoGuard(engine)], checkpointer=InMemorySaver())With a project folder, CocoGuard(load().engine()) builds the engine from coco.yaml and decides through the gate once the project is connected.
Choose the import
| Import | Attaches to |
|---|---|
from coco_sdk.langchain import CocoGuard | Upstream LangChain and LangGraph only. |
from coco_sdk import CocoGuard | coco-framework when it's installed, otherwise upstream. |
load().guard() | The same as from coco_sdk import CocoGuard. |
Good to know: With both installed, import from coco_sdk.langchain for an upstream agent.
What the guard does
The guard wraps wrap_tool_call and awrap_tool_call, so sync and async agents both pass through it.
| Effect | Result |
|---|---|
run | The tool runs. |
deny | The tool doesn't run. The model receives a ToolMessage with status error and the reason. |
escalate | interrupt() pauses the graph until a person decides. |
The session is the graph's thread_id, and judge checks read the conversation from the graph state as data.
Resume after an escalation
An escalation needs a checkpointer. Resume with upstream's Command:
from langgraph.types import Command
agent.invoke(Command(resume={"escalation_id": esc_id}), config)The guard reads the decision from Coco's record, never from the resume payload.
Move between upstream and coco-framework
coco-framework is LangChain and LangGraph forked at pinned versions (LangChain 1.4.2, LangGraph 1.2.12) and renamed. The check is the same on both.
| Upstream | coco-framework |
|---|---|
langchain.agents.create_agent | coco.agents.create_agent |
langgraph.types.Command, interrupt | coco_graph.types.Command, interrupt |
langgraph.checkpoint.memory.InMemorySaver | coco_graph.checkpoint.memory.InMemorySaver |
langchain_core.tools.tool | coco_core.tools.tool |