Skip to content

Agent can enter an infinite loop when handling large tool results with long lines #82

@KDKHD

Description

@KDKHD

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions