diff --git a/src/oss/images/agent-middleware-execution-flow.svg b/src/oss/images/agent-middleware-execution-flow.svg new file mode 100644 index 0000000000..2bc940e619 --- /dev/null +++ b/src/oss/images/agent-middleware-execution-flow.svg @@ -0,0 +1,2 @@ +loop_exit_nodem[0].after_modelrouting Nodegraph Nodeexit_nodetoolsm[-1].after_agentm1, m2, m3: Middlewarem1.after_agentm2m3entry_nodeloop_entry_nodem[0].before_modelm[0].before_agentSTARTENDm[-1].before_modelm[-2]m[-3]{structured_resopnse}if there is can_jump_to key,jumping to node is possible{structured_resopnse}if w/o any tools andafter_model mw......NotesModelm[-1].after_modelm[-2]m[-3]...m[-1].before_agentm[-2]m[-3]...ororororwrap_tool_callwrap_model_call \ No newline at end of file diff --git a/src/oss/langchain/middleware/custom.mdx b/src/oss/langchain/middleware/custom.mdx index 318cc5d784..f014867c8b 100644 --- a/src/oss/langchain/middleware/custom.mdx +++ b/src/oss/langchain/middleware/custom.mdx @@ -549,6 +549,20 @@ __Agent loop ends__ - `after_*` hooks: Last to first (reverse) - `wrap_*` hooks: Nested (first middleware wraps all others) +### Detailed execution flow + +The following diagram illustrates the complete agent execution lifecycle with all middleware hook points: + +Agent middleware execution flow diagram showing the lifecycle from START through before_agent, before_model, model execution, after_model, tools execution, and after_agent hooks with their invocation order and relationships + + +Diagram is based on the [langchain_v1/langchain/agents/factory.py](https://github.com/langchain-ai/langchain/blob/81c4f21b5297b31b2cd82ccefdd134aa0e59efa0/libs/langchain_v1/langchain/agents/factory.py). + + ## Agent jumps To exit early from middleware, return a dictionary with `jump_to`: