# ReactAgent loop exits prematurely when LLM returns text-only message with empty tool\_calls mid-task

**URL:** <https://forum.langchain.com/t/reactagent-loop-exits-prematurely-when-llm-returns-text-only-message-with-empty-tool-calls-mid-task/4608>\
**Category:** OSS Product Help\
**Created:** [September 25, 2026, 9:03am UTC](https://forum.langchain.com/t/reactagent-loop-exits-prematurely-when-llm-returns-text-only-message-with-empty-tool-calls-mid-task/4608 "2026-09-25T09:03:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sivasankar](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Sivasankar](https://forum.langchain.com/u/Sivasankar)\
**Post date:** [September 25, 2026, 9:03am UTC](https://forum.langchain.com/t/reactagent-loop-exits-prematurely-when-llm-returns-text-only-message-with-empty-tool-calls-mid-task/4608/1 "2026-09-25T09:03:44Z")

</div>

**Problem:**

During multi-step agentic tasks, the LLM occasionally returns an AIMessage with text content and `tool_calls: []` — essentially a “status update” or “thinking out loud” message — before the task is actually complete. The ReactAgent router treats any AIMessage with empty `tool_calls` as a final answer and exits the loop immediately.

**Example of the stall:**

The agent is midway through a 5-step coding task. After completing step 3, instead of calling the next tool, the model returns:

```auto
AIMessage {
  content: "Now I'll apply the backend changes and update the service tests.",
  tool_calls: []
}

```

The ReactAgent router sees `tool_calls.length === 0` → routes to END → loop exits → user is stuck at step 3 with no way to continue.

**How often this happens:**

In production with ~thousands of agent runs, we see this in roughly 5-8% of multi-step runs. It’s more common with:

- Long-running tasks (10+ tool calls)
- Complex multi-file changes where the model “plans ahead” verbally
- Context-heavy prompts near the token window boundary
- Certain model versions (Claude Sonnet is also most frequent)

**Root cause in ReactAgent:**

The `#createModelRouter` and `#createAfterModelRouter` both use this check:

```javascript
if (AIMessage.isInstance(lastMessage) && 
    (!lastMessage.tool_calls || lastMessage.tool_calls.length === 0)) {
  return exitNode;
}

```

This is a hard binary — any text-only AI response = final answer. There’s no distinction between:

1. **Genuine final answer** — “Here’s the summary of what I did” (should exit)
2. **Mid-task status update** — “I’ll now update the tests” (should NOT exit)

**Questions for the team:**

1. **Is there a built-in mechanism to prevent the loop from exiting on empty tool\_calls?** Something like a `shouldContinue` callback or a `minIterations` option?

2. **Is this a known issue?** We couldn’t find existing discussions about this specific failure mode. Happy to contribute a fix if you can point us to the right approach.

---

<div class="post-metadata">

**Author:** ![pawel-twardziak](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/pawel-twardziak/32/960_2.png) [@pawel-twardziak](https://forum.langchain.com/u/pawel-twardziak)\
**Post date:** [September 25, 2026, 10:13am UTC](https://forum.langchain.com/t/reactagent-loop-exits-prematurely-when-llm-returns-text-only-message-with-empty-tool-calls-mid-task/4608/2 "2026-09-25T10:13:18Z")

</div>

hi @Sivasankar

**1. Is there a built-in option?** No. There’s no `shouldContinue` and no `minIterations`. `createAgent` stops as soon as the model returns an AI message without tool calls. JS implements that rule in `#createModelRouter`, and Python’s `create_agent` has the same rule in `_make_model_to_tools_edge`. To the router, “I’ll now update the tests” and a real final answer are the same kind of message. `recursionLimit` and `modelCallLimitMiddleware` only cap the loop. They don’t extend it. `minIterations` would be the wrong control anyway, because it would force extra turns after tasks that really are finished. The fix is to give the model an unambiguous “done” signal.

**2. Is this a known issue?** The model behavior is well known. Some of your 5-8% may have other causes, though, so check a few failed runs first:

- **`stop_reason: "max_tokens"`** (in `response_metadata` or `additional_kwargs`): the output was cut off before the `tool_use` block. This fits your “near the context limit” pattern. Raise `maxTokens` and/or add `summarizationMiddleware`.
- **The raw provider response has a tool call, but the assembled `AIMessage` has `tool_calls: []`**: that’s an integration or streaming bug, not the model. Examples of this class: [#9816](https://github.com/langchain-ai/langchainjs/issues/9816), [#10937](https://github.com/langchain-ai/langchainjs/issues/10937).
- **`end_turn` with text only** : the model really narrated instead of calling a tool.

Quick question before going further: how does your agent track progress through the steps?  
Specifically:

1. Do you use todoListMiddleware (or your own plan/task list in agent state), or does the model keep track of the steps only in the conversation?
2. Are the steps known up front (e.g. from a ticket or a planner you control), or does the model come up with them during the run?

This changes the recommended fix. If there’s a task list in state, the guard can check “are there open tasks?” deterministically and nudge with the exact next step. Without one, the more robust option is an explicit task\_complete tool as the only way to finish.

---

<div class="post-metadata">

**Author:** ![Sivasankar](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Sivasankar](https://forum.langchain.com/u/Sivasankar)\
**Post date:** [September 25, 2026, 11:11am UTC](https://forum.langchain.com/t/reactagent-loop-exits-prematurely-when-llm-returns-text-only-message-with-empty-tool-calls-mid-task/4608/3 "2026-09-25T11:11:14Z")

</div>

Hi @pawel-twardziak

We checked our traces for `max_tokens` and the streaming assembly bug — both ruled out (stalls happen at low token counts early in runs, and we verified a sample of raw provider payloads).

For `end_turn` with text only — we cannot confirm this yet. Our current pipeline strips `response_metadata` before persistence so `stop_reason` is not stored anywhere. We need to add logging for it first before we can say definitively.

On your two questions:

1. We use `todoListMiddleware` — but the stall happens both with and without todo state.
2. Steps are fully dynamic — the model comes up with them during the run.

---

<div class="post-metadata">

**Author:** ![pawel-twardziak](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/pawel-twardziak/32/960_2.png) [@pawel-twardziak](https://forum.langchain.com/u/pawel-twardziak)\
**Post date:** [September 25, 2026, 3:25pm UTC](https://forum.langchain.com/t/reactagent-loop-exits-prematurely-when-llm-returns-text-only-message-with-empty-tool-calls-mid-task/4608/4 "2026-09-25T15:25:35Z")

</div>

hi @Sivasankar

When you say the stall happens “without todo state”, do you mean:

- (a) runs where the model never called write\_todos,
- (b) the stall happened before its first write\_todos call

And when todos do exist at the moment of the stall, are there still open items?

Do you use LangSmith for tracing or something else?

---

<div class="post-metadata">

**Author:** ![Sivasankar](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Sivasankar](https://forum.langchain.com/u/Sivasankar)\
**Post date:** [September 25, 2026, 4:35pm UTC](https://forum.langchain.com/t/reactagent-loop-exits-prematurely-when-llm-returns-text-only-message-with-empty-tool-calls-mid-task/4608/5 "2026-09-25T16:35:40Z")

</div>

Hi @pawel-twardziak ,

As far as we noticed it occured on the below case

- Some runs where the model never calls `write_todos` at all (short/simple tasks where it skips planning and goes straight to tool calls, then stalls).

On tracing — we don’t use LangSmith. We have our own internal trace logging system.

---

<div class="post-metadata">

**Author:** ![pawel-twardziak](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/pawel-twardziak/32/960_2.png) [@pawel-twardziak](https://forum.langchain.com/u/pawel-twardziak)\
**Post date:** [September 25, 2026, 5:36pm UTC](https://forum.langchain.com/t/reactagent-loop-exits-prematurely-when-llm-returns-text-only-message-with-empty-tool-calls-mid-task/4608/6 "2026-09-25T17:36:55Z")

</div>

hi @Sivasankar

It’s always hard to judge what is the main issue without seeing the traces.  
It might be the model issue or the harness issue (e.g. tools that are confusing the model)

---

<div class="post-metadata">

**Author:** ![Sivasankar](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Sivasankar](https://forum.langchain.com/u/Sivasankar)\
**Post date:** [October 7, 2026, 6:55am UTC](https://forum.langchain.com/t/reactagent-loop-exits-prematurely-when-llm-returns-text-only-message-with-empty-tool-calls-mid-task/4608/7 "2026-10-07T06:55:31Z")

</div>

@pawel-twardziak - Please refer my message array traces from this file : [langchain-traces/langchain-traces.md at main · SivasankarMathi/langchain-traces · GitHub](https://github.com/SivasankarMathi/langchain-traces/blob/main/langchain-traces.md)

---

<div class="post-metadata">

**Author:** ![pawel-twardziak](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/pawel-twardziak/32/960_2.png) [@pawel-twardziak](https://forum.langchain.com/u/pawel-twardziak)\
**Post date:** [October 9, 2026, 11:48pm UTC](https://forum.langchain.com/t/reactagent-loop-exits-prematurely-when-llm-returns-text-only-message-with-empty-tool-calls-mid-task/4608/8 "2026-10-09T23:48:11Z")

</div>

hi @Sivasankar

Thanks for the trace, it’s very informative. This run looks different from the “never calls `write_todos`” case, and most of it points at the tooling:

1. Every `grep` call failed (17/17 returned “No matches found”), including for code that exists.

- deepagents `grep` is a literal search. The tool description says “NOT regex”. The model used regex alternation in 15 of 17 calls (`"Team Lead|team_lead|admin|role"`), which searches for that exact string.
- On the sandbox backend, `glob` becomes `find -name <glob>` (`backends/sandbox.ts`, `buildGrepCommand`). `-name` matches only the file’s basename, without brace expansion, so the model’s ` **/*`, `** /*.{ts,tsx}` and ` **/schema/** /*.ts` never match. That’s why even `grep("ResourceLimitError", glob="**/*")` returned nothing while `errors.ts:107` defines it. I reproduced this: those globs give 0 hits, `*.ts` works.
- The suggested fallback, `rg` via `execute`, isn’t installed in your sandbox (`rg: command not found`).

1. A summarization happened partway through (the model reads `/conversation_history/session_….md` at msg 50). After that it re-reads the same files again and again (`skills.route.ts` and `skill.repository.ts` 5× each). After 19 turns and 81 tool calls there isn’t a single `edit_file`/`write_file`, and the todo list is still on item 1. The final “Applying backend changes…” message comes at the end of that spiral.

2. `write_todos` was called here, with 3 items still `pending` at the stall. The todo-aware nudge I posted earlier would have fired here and named “Implement backend skill limit validation” as the next step.

What I’d do:

- Fix search first. Install ripgrep in the sandbox image. Tell the model (system prompt) to use basename globs like `*.ts`, or no glob, and one literal term per `grep` call. Alternatively, override `grep` in your backend to support `**` and braces (e.g. `find -path`, or `rg --glob`). The basename-only `find -name` behaviour is probably worth an issue on deepagentsjs.
- Keep the completion guard (todos + `task_complete`) as the safety net.
- On the stalled message, check `finish_reason` and `invalid_tool_calls`. The first editing turn is the first large output in the run. If it was cut off, the tool call ends up in `invalid_tool_calls`, and the router only looks at `tool_calls`.
- Which model is this, exactly? The `gen-…` ids look like OpenRouter. Is it still Claude 3.5 Sonnet?

---

<div class="post-metadata">

**Author:** ![chineduokafor](https://avatars.discourse-cdn.com/v4/letter/c/c37758/32.png) [@chineduokafor](https://forum.langchain.com/u/chineduokafor)\
**Post date:** [October 12, 2026, 12:50am UTC](https://forum.langchain.com/t/reactagent-loop-exits-prematurely-when-llm-returns-text-only-message-with-empty-tool-calls-mid-task/4608/9 "2026-10-12T00:50:31Z")

</div>

In LangGraph, customize the conditional edge routing rather than letting tool\_calls.length === 0 go straight to END.

Instead of treating any message without tool calls as a completion, add a router function that checks whether a specific finish criteria or tool was invoked. If the model returns text without tool calls mid-task, route to a reflection step or append a system/human turn prompting the model to proceed with the next tool execution. Another option is enforcing a dedicated completion tool, ensuring the agent only exits when that specific tool is called.

---

<div class="post-metadata">

**Author:** ![Adam13y](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/adam13y/32/3627_2.png) [@Adam13y](https://forum.langchain.com/u/Adam13y)\
**Post date:** [October 27, 2026, 4:29pm UTC](https://forum.langchain.com/t/reactagent-loop-exits-prematurely-when-llm-returns-text-only-message-with-empty-tool-calls-mid-task/4608/10 "2026-10-27T16:29:01Z")

</div>

Hi @Sivasankar, +1 to Pawel’s point that the fix is an explicit “done” signal. Since you’re on the JS `createAgent`, here’s how I’d wire it, plus one catch with your model:

1. **Add a `task_complete({ summary })` tool with `returnDirect: true`.** `createAgent` ends the run after a successful `returnDirect` tool call, so finishing becomes an action the model has to take, not something the router guesses. Say in the system prompt that this is the only way to finish.
2. **Guard text-only turns in an `afterModel` hook.** If the last AIMessage has no tool calls, append a short message (“Continue: call the next tool, or call task\_complete if you’re done”) **and** set `jumpTo: "model"`. Appending the message matters, because the router exits on a text-only AIMessage before it looks at `jumpTo`. Cap it at 2 nudges per run so it can’t spin, and log each nudge so you can see how often it fires.
3. **Forcing tool choice is the stronger version, but check your provider first.** `toolChoice: "required"` (set per call from `wrapModelCall`) makes a text-only turn impossible. But Anthropic’s API rejects forced tool choice when extended thinking is on, and on its newest models it returns a 400. On those models, step 2 is your main fix. And if you do force it, step 1’s `returnDirect` is what stops the loop. Otherwise the model is forced to call yet another tool after `task_complete`.
4. **Measure before and after.** Once `stop_reason` is logged, take 20-30 stalled runs, cut each one right before the stalled turn, and re-sample just that model call 5-10 times. That gives you a real stall rate per model to compare after the change. At 5-8%, you can’t tell a fix from noise by eyeballing new runs.

Happy to help put the replay set together if that’s useful.

Adam
