create_agent, Middleware and Legacy Agents
In LangChain 1.x, `create_agent` (from `langchain.agents`) is the standard agent: a model calling tools in a loop, compiled to a LangGraph graph. You customise it with middleware hooks rather than new agent classes. Know the hook order, which built-in middleware solves which production problem, how structured output and human approval work inside the loop, and how legacy `AgentExecutor` code maps onto it.
Key points
- 1
create_agent(model, tools, *, system_prompt, middleware, response_format, state_schema, context_schema, checkpointer, store, ...)returns a compiled graph. The loop alternates amodelnode and atoolsnode, and it ends when the model replies with no tool calls. Areturn_directtool, a graceful limit or ajump_to: "end"also end it. - 2
Node-style hooks (
before_agent,before_model,after_model,after_agent) return state updates. Wrap-style hooks (wrap_model_call,wrap_tool_call) receive ahandlerand can retry, short-circuit, or rewrite the call viarequest.override(model=..., tools=..., messages=..., system_message=...). - 3
Ordering for
middleware=[m1, m2, m3]:before_*hooks run m1 to m3,wrap_*hooks nest with m1 outermost, andafter_*hooks run m3 to m1.before_agentandafter_agentrun once per invocation; model hooks run on every model call. - 4
Use a decorator (
@before_model,@wrap_model_call,@dynamic_prompt, ...) for a single quick hook. Use anAgentMiddlewaresubclass for several hooks, sync and async variants, configuration, astate_schemaof its own, or extratoolsthat ship with the middleware. - 5
Jumps: a node hook returns
{"jump_to": "end" | "model" | "tools"}and must declare the target withcan_jump_to(@hook_config(can_jump_to=[...])or the decorator argument). - 6
Request overrides in
wrap_model_callaffect only that call. State updates from node hooks are persisted by the checkpointer. The system prompt is applied at call time and is not stored inmessages. - 7
Built-ins:
SummarizationMiddleware(needs atrigger;keepdefaults to 20 messages),HumanInTheLoopMiddleware,ModelCallLimitMiddleware(run_limit/thread_limit, exitendby default),ToolCallLimitMiddleware(exitcontinueby default),ModelFallbackMiddleware,ModelRetryMiddleware/ToolRetryMiddleware,PIIMiddleware,LLMToolSelectorMiddleware,ContextEditingMiddleware,TodoListMiddleware(addswrite_todos). - 8
response_format:ToolStrategy(the schema becomes an artificial tool; supports unions;handle_errors=Trueretries on validation errors) orProviderStrategy(provider-native, optionalstrict). A bare schema picks the provider strategy when the model profile supports it. The result is inresult["structured_response"]. - 9
Human in the loop:
interrupt_on={"tool": True | False | {"allowed_decisions": [...]}}, with decisions approve, edit, reject or respond. It needs a checkpointer and athread_id, and you resume withCommand(resume={"decisions": [...]}), one decision per action in order. - 10
Runtime context: declare
context_schema, passinvoke(..., context=...), and readruntime.contextin node hooks,request.runtime.contextin wrap hooks, or a hiddenToolRuntimeparameter in tools. - 11
Migration:
langgraph.prebuilt.create_react_agentbecamecreate_agentandpromptbecamesystem_prompt. Pre and post model hooks becamebefore_model/after_model, dynamic models and tool error handling moved towrap_model_call/wrap_tool_call, custom state must be aTypedDictextendingAgentState, andAgentExecutor/initialize_agentlive inlangchain-classic. - 12
Positioning: LangGraph is the runtime, LangChain's
create_agentis a minimal harness, and Deep Agents is an opinionated harness built on it (filesystem, subagents, skills). Use a plain chain or graph when the steps are fixed.
Common traps
An exception raised inside a tool body propagates and fails the run by default. Only invalid arguments and unknown tool names become error
ToolMessages automatically, so addwrap_tool_callor retry/error middleware.Returning
jump_towithout declaringcan_jump_tois silently ignored: the model step still runs.A sync-only
wrap_model_callraisesNotImplementedErrorunderainvoke/astream(and async-only fails underinvoke). Sync node hooks still work in async runs.Two instances of the same middleware class fail the build as duplicates. Configure one
ModelCallLimitMiddlewarewith bothrun_limitandthread_limit.PIIMiddlewarechecks only user input by default. Setapply_to_tool_resultsandapply_to_outputto catch data that enters through tools or leaves in replies.HumanInTheLoopMiddlewarewithout a checkpointer can still pause, but the resume fails. Also,respondtells the model the tool succeeded, so userejectto deny an action.Adding tools in
wrap_model_callthat were never registered withcreate_agentraises an error when the model calls them, unless awrap_tool_callexecutes them.SummarizationMiddlewarewith notriggernever summarizes.
Read the source
Further reading
Suggest a resourceNo extra reading for create_agent, Middleware and Legacy Agents yet. Found a guide, video or cheat sheet that made it click? Share it and help the next learner.
Test yourself on create_agent, Middleware and Legacy Agents
Ten questions, with the answer and explanation after each one.