Source: LangChain agents, middleware and structured output docs
Official LangChain v1 docs for create_agent, the middleware hook model, the built-in middleware catalogue, and ToolStrategy/ProviderStrategy structured output.
Contents
- What: official open-source documentation (Python)
- Publisher: LangChain
- Date: accessed 2026-11-14 (pages reference
langchainup to 1.5.x) - Original: v1 release notes, Agents, Middleware overview, Built-in middleware, Custom middleware, Structured output
Summary
The docs define an agent as a model calling tools in a loop until done, and create_agent as the harness around that loop. Everything else (summarisation, retries, approval, PII handling, tool selection, context trimming) is middleware: classes or decorated functions that hook before, after, or around model and tool calls.
Key claims
create_agentparameters:model(a"provider:model"string or instance),tools,system_prompt,middleware,response_format,state_schema(subclass ofAgentState, must be a TypedDict),context_schema,checkpointer,name.AgentStateholds an append-onlymessageslist; persistence across turns needs a checkpointer and athread_id;contextcarries immutable per-run data read viaruntime.context.- Six hooks: node-style
before_agent,before_model,after_model,after_agent(return dict updates merged via reducers) and wrap-stylewrap_model_call,wrap_tool_call(call thehandlerzero, one or many times, enabling short-circuit and retry). - Ordering for
[m1, m2, m3]:before_*run m1 to m3;after_*run m3 to m1;wrap_*nest with m1 outermost. - Hooks can return
jump_to("end","tools","model") if declared withcan_jump_to; for conflicting non-reducer writes the outermost middleware wins; commands from discarded retry attempts are dropped. - Built-ins include
SummarizationMiddleware(trigger,keepdefault 20 messages),HumanInTheLoopMiddleware(interrupt_on, needs a checkpointer),ModelCallLimitMiddleware/ToolCallLimitMiddleware(thread_limit,run_limit,exit_behavior),ModelRetryMiddleware/ToolRetryMiddleware(default 2 retries after the initial call, backoff factor 2.0, jitter),ModelFallbackMiddleware,PIIMiddleware(redactdefault,block,mask,hashstrategies),TodoListMiddleware,LLMToolSelectorMiddleware,ContextEditingMiddleware(clears old tool outputs, default trigger 100k tokens, keep 3),ShellToolMiddleware,FilesystemFileSearchMiddleware,LLMToolEmulator, plus newerToolErrorMiddleware(1.3.14),ModelRefusalMiddleware(1.4.9) and betaModelRouterMiddleware/AutoModeMiddleware(1.5.0). Provider-specific middleware exists for Anthropic prompt caching and tools, Bedrock caching and OpenAI moderation. - Structured output:
ToolStrategy(works with any tool-calling model;handle_errorsretries by feeding validation errors back as aToolMessage) vsProviderStrategy(native provider enforcement;strictsince 1.2). Passing a bare schema auto-selects from the model profile (since 1.1). Result lands inresult["structured_response"]. A raw JSON-Schema dict is not auto-detected and must be wrapped in a strategy. If the model emits several structured outputs in one turn, the defaulthandle_errors=Truereturns an errorToolMessageasking it to retry with one (MultipleStructuredOutputsError). - Legacy chains, retrievers, the indexing API and hub moved to
langchain_classic.