agent-ops-patterns / gallery / LangChain map / how it decides
How LangChain decides
In the ordinary tool loop of an agent built with LangChain's create_agent, the model decides when it is finished: a reply that asks for no tool ends the run, and nothing checks that reply. By default nothing trims the conversation; call limits and summarization are middleware the developer adds.
How it decides it is done
-
Is the agent finished with the task?
Decided by an AI model The model decides: in the ordinary tool loop, a reply that asks for no tool ends the run and is the answer, unchecked. Structured output adds its own exits.
factory.py:2016-2019· decision G1 in the full map -
Does a tool's result end the run without the model reading it?
Decided by plain code When every client-side tool in that batch is marked return_direct by the developer, the run ends on the tool output and the model does not see it; the tool's status is not looked at.
factory.py:2086-2100· decision G2 in the full map -
When the provider's native structured answer fails to parse, is the model asked again?
Decided by plain code No: a native structured answer that is not valid JSON or does not match the schema raises straight away, and the run fails unless a middleware around the model call catches it.
factory.py:1262-1269· decision G6 in the full map
Limits and memory
-
How many steps may one agent run take before it is stopped?
Decided by plain code create_agent sets a limit of 9,999 agent steps; the run is then stopped with a GraphRecursionError and no answer, even if it finished exactly on the last step. The caller can change the number per run with config={"recursion_limit": n}.
factory.py:1899· decision F1 in the full map -
What happens when the conversation grows past what the model can read in one call?
Decided by plain code Nothing trims it by default: every model call sends the whole message list. What happens when it no longer fits is the provider's; where the provider raises, the run fails, unless summarization or context-editing middleware was added.
factory.py:1502-1506· decision F3 in the full map -
When should older conversation be summarized away?
Decided by plain code Only if the developer adds summarization middleware and gives it a trigger: a token count, a message count or a share of the model's context window. Code checks before each model call; with no trigger it never summarizes.
summarization.py:704-744· decision F4 in the full map -
How many model calls may one run, or one conversation thread, make?
Decided by plain code Unlimited by default, apart from the step limit. With model-call-limit middleware, code compares the counts before each model call and by default ends the run with a "limits exceeded" message; a thread limit counts across runs only with a checkpointer.
model_call_limit.py:186-210· decision F8 in the full map -
Does a new run pick up where an earlier conversation left off?
Decided by plain code No by default: every run starts from only the messages passed in. With a checkpointer, the caller's checkpoint coordinates (normally thread_id) decide which saved conversation the run continues.
factory.py:833· decision F10 in the full map
Other judgments
-
Given just "gemini-2.5-pro" or "gpt-oss-20b", which provider does the agent decide to call?
Decided by plain code With no recognised prefix, code walks an ordered table of name prefixes; a name that matches nothing raises at construction. The match reads the name only, so gpt-oss-20b goes to OpenAI and o4-mini matches nothing.
base.py:547-620· decision A2 in the full map -
What standing instructions does the model get each step, and are they saved with the history?
Decided by plain code LangChain adds no system prompt of its own. On the default path, a system_prompt from the caller is kept outside state and put in front of the conversation on each model call, so it is not saved into the history; middleware can replace it per call.
factory.py:1050-1056· decision B1 in the full map
Three problems in the code
-
1. In an agent with tools, a reply with only invalid_tool_calls ends the run
In an agent with a tools step, if a reply's calls are all recorded as invalid_tool_calls (on ChatOpenAI's chat-completions path, arguments that are not valid JSON) and its tool_calls list is empty, the run ends there, without retrying them and without an exception. On that route, the model is told about such a call only on a later invocation that carries that history, and only if the call has an ID.
Already tracked upstream (#33504), checked 2026-11-15.
Where:
factory.py:2016-2019,factory.py:692-707. How we checked: the code was re-read at the source version named above; the sentences were checked by an independent AI reviewer from another company (OpenAI Codex, read-only) on 2026-11-08, in four rounds; each sentence it questioned was narrowed, removed or kept with the reason recorded. -
2. An invalid structured answer beside another tool call can end the run without a structured response
With a tool-based response format and the default error handling, one reply can hold a structured answer that fails to parse and also an ordinary tool call. The ordinary tool can then run and the run can end with no structured response and no exception, before the model reads the retry message.
Already tracked upstream (#40423), checked 2026-11-15.
Where:
factory.py:1329-1346,factory.py:2021-2032,factory.py:2102-2104. How we checked: the code was re-read at the source version named above; the sentences were checked by an independent AI reviewer from another company (OpenAI Codex, read-only) on 2026-11-08, in four rounds; each sentence it questioned was narrowed, removed or kept with the reason recorded. -
3. A long tool loop can turn summarized history into a fixed placeholder
By default, summarization middleware tries to trim the older messages to a 4,000-token window that starts at a human message. If nothing is left, as can happen in a long run of tool calls after one request, the summary model is not called and the older messages are replaced by the text "Previous conversation was too long to summarize."
Already tracked upstream (#41146), checked 2026-11-15.
Where:
summarization.py:1028-1046,summarization.py:935-937. How we checked: the code was re-read at the source version named above; the sentences were checked by an independent AI reviewer from another company (OpenAI Codex, read-only) on 2026-11-08, in four rounds; each sentence it questioned was narrowed, removed or kept with the reason recorded.