[GH-ISSUE #82] Agent can enter an infinite loop when handling large tool results with long lines #26

Closed
opened 2026-02-16 06:16:54 -05:00 by yindo · 0 comments
Owner

Originally created by @KDKHD on GitHub (Dec 16, 2025).
Original GitHub issue: https://github.com/langchain-ai/deepagentsjs/issues/82

Originally assigned to: @christian-bromann on GitHub.

Issue: Agent can enter an infinite loop when handling large tool results with long lines

Summary

When a tool returns a large result composed of a small number of very long lines, the agent can enter an infinite eviction/read loop.

Root Cause

The issue arises from an interaction between:

  • toolTokenLimitBeforeEvict (default: 20000)
  • Eviction of large tool results to the filesystem
  • The read_file tool’s line-based limit (default: 2000 lines)

Detailed Explanation

  1. By default, toolTokenLimitBeforeEvict is set to 20000.

  2. If a tool returns a result containing very long lines (e.g. 5 lines totaling ~100,000 characters), the result exceeds the eviction threshold
    (100,000 chars > 20,000 tokens * 4 chars/token) and is evicted to the filesystem.
    See:
    https://github.com/langchain-ai/deepagentsjs/blob/main/src/middleware/fs.ts#L481

  3. The agent then attempts to read the evicted content using the read_file tool.

  4. read_file applies a line limit (default: 2000 lines):
    https://github.com/langchain-ai/deepagentsjs/blob/main/src/middleware/fs.ts#L193

  5. Because the file contains only a few lines (e.g. 5), all ~100,000 characters are returned in a single read_file result.

  6. The read_file tool result now again exceeds the eviction threshold and is evicted to the filesystem.

  7. The agent repeats the process, resulting in an infinite loop:

Trace

Reproduction trace:
https://smith.langchain.com/public/5ccfa3e5-07f0-4572-b205-22ae3d3d5689/r

Possible Solutions

  • Change read_file to enforce a character limit instead of (or in addition to) a line limit
  • Exempt read_file results from eviction. It doesn't make sense for the read_file tool to result in evictions, as this would result in files being duplicated.
  • Alternatively, cap the returned content from read_file based on token or character count to guarantee forward progress
Originally created by @KDKHD on GitHub (Dec 16, 2025). Original GitHub issue: https://github.com/langchain-ai/deepagentsjs/issues/82 Originally assigned to: @christian-bromann on GitHub. ## Issue: Agent can enter an infinite loop when handling large tool results with long lines ### Summary When a tool returns a large result composed of a small number of very long lines, the agent can enter an infinite eviction/read loop. ### Root Cause The issue arises from an interaction between: - `toolTokenLimitBeforeEvict` (default: `20000`) - Eviction of large tool results to the filesystem - The `read_file` tool’s **line-based** limit (default: `2000` lines) ### Detailed Explanation 1. By default, `toolTokenLimitBeforeEvict` is set to `20000`. 2. If a tool returns a result containing **very long lines** (e.g. 5 lines totaling ~100,000 characters), the result exceeds the eviction threshold (`100,000 chars > 20,000 tokens * 4 chars/token`) and is evicted to the filesystem. See: https://github.com/langchain-ai/deepagentsjs/blob/main/src/middleware/fs.ts#L481 3. The agent then attempts to read the evicted content using the `read_file` tool. 4. `read_file` applies a **line limit** (default: 2000 lines): https://github.com/langchain-ai/deepagentsjs/blob/main/src/middleware/fs.ts#L193 5. Because the file contains only a few lines (e.g. 5), all ~100,000 characters are returned in a single `read_file` result. 6. The `read_file` tool result now again exceeds the eviction threshold and is evicted to the filesystem. 7. The agent repeats the process, resulting in an infinite loop: ### Trace Reproduction trace: https://smith.langchain.com/public/5ccfa3e5-07f0-4572-b205-22ae3d3d5689/r ### Possible Solutions - Change `read_file` to enforce a **character limit** instead of (or in addition to) a line limit - Exempt `read_file` results from eviction. It doesn't make sense for the read_file tool to result in evictions, as this would result in files being duplicated. - Alternatively, cap the returned content from `read_file` based on token or character count to guarantee forward progress
yindo closed this issue 2026-02-16 06:16:54 -05:00
yindo changed title from Agent can enter an infinite loop when handling large tool results with long lines to [GH-ISSUE #82] Agent can enter an infinite loop when handling large tool results with long lines 2026-06-05 17:21:02 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagentsjs#26