LangGraph adapter
For a walkthrough built on a runnable sample, see using LangGraph.
from langchain_core.messages import SystemMessagefrom langgraph.graph import END, MessagesState, StateGraphfrom langgraph.prebuilt import ToolNode
from sectr import AgentAppfrom sectr.adapters.langgraph import as_langgraph_tool, langgraph
tools = ToolNode([as_langgraph_tool(t) for t in TOOLS]) # TOOLS: sectr.tools.tool functions# ... build a StateGraph(MessagesState) with an agent node and `tools` ...graph = builder.compile() # no checkpointer
app = AgentApp(name="support-agent")app.add_route("/chat", langgraph(graph))- Requires
langgraph>=1.2.11,langgraph-prebuilt>=1.1.0andlangchain-core>=1.6.6. To install, see the support matrix. - Signatures:
langgraph(graph, *, context=None) -> Handler— wraps a compiled graph. RaisesValueErrorat construction (before the first turn) for unsupported graph shapes.contextis a factory called once per invocation with the turn’sSessionContext(itsctx.session_ididentifies the session), sync orasync. Its value is the graph’s runtime context (astream(context=...)): tools read it asruntime.context(ToolRuntime), and nodes and middleware asRuntime.context. A static value iscontext=lambda ctx: value. A resumed approved tool gets the same context.
as_langgraph_tool(tool, *, needs_approval=False) -> BaseTool— converts asectr.tools.toolfunction for aToolNode. The model-facing schema comes from the function signature (untyped parameters arestr); the tool needs a docstring. Given a LangChain tool instead (anyBaseTool: a@tool, an MCP tool, a community tool), it returns it gated whenneeds_approval=True, otherwise unchanged.needs_approval=Truealso gates a sectr tool.
Behavior
Section titled “Behavior”- Each turn streams the graph afresh (
graph.astream), with its message state rebuilt from the journal: the journal is the checkpoint, so don’t compile the graph with one of your own. - Reasoning and names carry over. A model message’s reasoning, as the
graph stores it (signatures included), and its
name(a named agent’s) are sent back on later turns and approval resumes. - Gated tools pause the turn:
needs_approval=True,HumanInTheLoopMiddleware’sinterrupt_on, or the deployment’sapproval_tools, which also gates theToolNode’s other tools, such as plain LangChain tools and MCP tools fromlangchain-mcp-adapters. Other tool calls in the same model message run first, so each runs once. - An approved call runs exactly once, at the graph’s tool node, as on any
turn: your
ToolNode’s settings, tool middleware, injected arguments,Commandresults and the edges out of the tool node all apply. The model generates only the continuation. A denied call gets the standard rejection text without running. - Several gated calls in one model message become sequential approvals, one per turn. See approvals.
Supported graphs
Section titled “Supported graphs”ReAct-shaped: a single top-level ToolNode holding your
sectr tools: langchain.agents.create_agent, a StateGraph(MessagesState)
with an agent node, or the deprecated create_react_agent. These fail with a
clear error instead:
- a graph compiled with a checkpointer (the journal is the only state store);
- sectr tools in several
ToolNodes (multi-agent and supervisor graphs), or in aToolNodenested in a subgraph (the error namessubagent_gated_tool: see subagents); - a sectr tool that takes graph-injected arguments (
InjectedState,InjectedStore,ToolRuntime); - a gated sectr tool called from your own tool node instead of a
ToolNode, a tool named inapproval_toolsin a second top-levelToolNode, or a gated call in a graph whose tool node is reached from more than one node (these fail at the first gated call, before anyone is asked to approve); - structured output, which isn’t supported yet: a
response_formatoncreate_agentorcreate_react_agent. (A node of your own that callswith_structured_outputcan’t be detected; its result isn’t journaled as the answer.)
Limits to know about
Section titled “Limits to know about”- Gate tools, don’t call
interrupt(). LangGraph’s nativelanggraph.types.interrupt()can’t be resumed without a checkpointer, so it fails loudly (a runtime error;sectr inspectalso warns when it finds the import). Gate tools withneeds_approval=True, or with LangChain’sHumanInTheLoopMiddlewareoncreate_agent: Sectr honors itsinterrupt_onwith its own pause, so the middleware never callsinterrupt(). Each entry must allow both approve and reject (Truedoes);editandresponddecisions aren’t offered, and an entry’swhenpredicate applies. Middleware on a subagent isn’t honored: a tool it gates ends the turn withsubagent_gated_tool, without running. Nativeinterrupt(), multi-agent graphs, and checkpointed resume are planned. - Conversation state only. Non-message state channels don’t survive from
one turn to the next. Keep state in the conversation — for example a
todo_writetool whose journaled call is the write.
Subagents
Section titled “Subagents”A subagent is another agent your agent runs from a tool. On Sectr it’s an ordinary tool, and an opaque one: the journal, and every client streaming the session, see the tool call and its result, but not the subagent’s own messages and tool calls, so later turns don’t see those either.
To require approval for what a subagent does, gate its tool: approving it runs
the whole subagent, and denying it runs none of it. A gated tool inside a
subagent isn’t supported yet, because Sectr can’t pause a turn partway through
one: such a tool never runs. Instead the agent is rejected when the runner
starts, or the turn ends with an ERROR whose code is subagent_gated_tool.
An escalate tool that runs a specialist agent, gated as a whole:
specialist = create_agent(model, tools=[as_langgraph_tool(track_parcel)], name="specialist")
@tool(needs_approval=True) # sectr.tools.toolasync def escalate(question: str) -> str: """Escalate a question to the shipping specialist.""" result = await specialist.ainvoke({"messages": [{"role": "user", "content": question}]}) return result["messages"][-1].text
graph = create_agent(model, tools=[as_langgraph_tool(escalate)])A LangGraph subgraph used as a node is treated the same way: sectr tools in it
are rejected when the runner starts, and a gated call to any other tool in it
(by the policy, or by the subgraph’s own HumanInTheLoopMiddleware) ends the
turn with subagent_gated_tool.
Where your own code starts the subagent or handles what it returns, the adapter can’t see its run:
-
Keep your graph’s config when you pass one to a subagent. LangChain passes your graph’s config on to
specialist.ainvoke(...)by itself, and the adapter uses it to tell the subagent’s run from your graph’s. A config you pass merges with it, exceptconfigurable, which replaces it whole. To add your own values, extend the inherited ones:from langgraph.config import get_configconfigurable = {**get_config()["configurable"], "thread_id": thread_id}result = await specialist.ainvoke(messages, {"configurable": configurable})Without them, a gated tool inside the subagent still doesn’t run, but the turn’s
ERRORislanggraph_unmatched_approvalinstead ofsubagent_gated_tool. The same happens if a task started before the turn, such as a background worker, runs the subagent.