-
Notifications
You must be signed in to change notification settings - Fork 85
Description
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_filetool’s line-based limit (default:2000lines)
Detailed Explanation
-
By default,
toolTokenLimitBeforeEvictis set to20000. -
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 -
The agent then attempts to read the evicted content using the
read_filetool. -
read_fileapplies a line limit (default: 2000 lines):
https://github.com/langchain-ai/deepagentsjs/blob/main/src/middleware/fs.ts#L193 -
Because the file contains only a few lines (e.g. 5), all ~100,000 characters are returned in a single
read_fileresult. -
The
read_filetool result now again exceeds the eviction threshold and is evicted to the filesystem. -
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_fileto enforce a character limit instead of (or in addition to) a line limit - Exempt
read_fileresults 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_filebased on token or character count to guarantee forward progress