eca icon indicating copy to clipboard operation
eca copied to clipboard

Tool output truncation needed to prevent context overflow

Open davidwuchn opened this issue 10 months ago • 12 comments

Problem

ECA currently concatenates tool outputs without any truncation. When tools return large outputs (e.g., file searches, grep results, command outputs), these exceed the LLM context window limits, causing failures or degraded performance.

Current Behavior

  • Tool outputs are concatenated directly into the context
  • No size limits or truncation applied
  • Large outputs cause context overflow errors

Expected Behavior

  • Tool outputs should be truncated when they exceed a reasonable size threshold
  • Truncation should be indicated clearly (e.g., "... [truncated N lines] ...")
  • Configurable truncation limits (optional)

Impact

  • Context window errors on large codebases
  • Wasted tokens on irrelevant output
  • Poor user experience when tools return unexpectedly large results

Possible Solutions

  1. Hard limit on output size (e.g., 10KB per tool)
  2. Smart truncation that preserves most relevant parts
  3. Configurable limits via CLI flag or config

davidwuchn avatar Feb 04 '26 13:02 davidwuchn

I believe the easiest solution is to have a config toolCall outputLimiter which for now can be a number of characters (easier to count then tokens), and maybe we could have a high default, in the future we could have others values like :smart to do whatever we think it's best. WDYT @zikajk ?

ericdallo avatar Feb 04 '26 18:02 ericdallo

@ericdallo It would be simple, but in my opinion, not very useful. I often need to debug massive files and the like. Personally, I see a problem in this specific scenario:

I ask the LLM for something, and it calls six tools in a row, but the last one exceeds the limit. To fix it now, I have to perform a rollback, but that causes me to lose the previous five calls. What I’d actually like is for that last tool call to be reverted, followed by either an auto-compact or the LLM being told that the output was too large and needs to be shortened. Personally, I lean toward the first option.

Honestly, I haven't seen many scenarios where I’d want a shorter output. It usually happened when using eca__grep or eca__ls —I can't recall exactly because I solved it by telling the LLM to use rgrep and the shell instead of those specific tools.

zikajk avatar Feb 05 '26 11:02 zikajk

Hum, rollback a tool automatically is dangerous as we don't know if it had side effects, also how to know if we should call autoCompact after a huge output? only when reach the context limit error? not sure the best way to solve this yet

ericdallo avatar Feb 05 '26 12:02 ericdallo

True. I wonder if other tools (where this never happens) solve this by knowing how much output from the tool consumes tokens and therefore perform compaction beforehand (which would explain why Codex performs compaction at different consumption percentages). It might be nice to take a look at the source code of the codex (I assume that calling the /compact endpoint doesn't change the problem).

zikajk avatar Feb 05 '26 12:02 zikajk

I found interesting how opencode solves that:

  • if the tool (customizable by tool) output passes a 2000 lines/50kb it truncates and add:
The tool call succeeded but the output was truncated. Full output saved to: {filepath}
Use Grep to search the full content or Read with offset/limit to view specific sections.

then save the output content in that filePath for 7 days of retention (looping hourly to remove the old ones). Not sure about the retention, but the idea is quite clever, WDYT @zikajk ?

Also it has a custom truncation logic for some tools:

  • bash: 50KB/2000 lines
  • grep: 100 results
  • read: 50KB/2000 lines
  • ls: 100 files -glob: 100 files

Not sure we need that tho

ericdallo avatar Feb 05 '26 13:02 ericdallo

I like that. But I think that we should first improve our GREP tool (or profit from rgrep like Codex / CC ae doing).

zikajk avatar Feb 05 '26 13:02 zikajk

@zikajk please open a new issue for that, I'm not aware of what shoud be improved

ericdallo avatar Feb 05 '26 13:02 ericdallo

It probably doesn't solve the problem with MCP Tools though.

zikajk avatar Feb 05 '26 13:02 zikajk

Why not @zikajk ? It's generic by tool the truncation, with specific handling for built-in ones

ericdallo avatar Feb 05 '26 13:02 ericdallo

@ericdallo Ah, cool, I misread it. https://github.com/editor-code-assistant/eca/issues/289

But yeah, that + better GREP (or just delegating GREP to bash) will improve the situation a lot. It can also save a lot of tokens in a long-run.

zikajk avatar Feb 05 '26 13:02 zikajk

Ok, I will start work on this, for now generic by tool and with no special customization for now

ericdallo avatar Feb 05 '26 13:02 ericdallo

Done, now we have toolCall outputTruncation config with default to 2000 lines/50kb

ericdallo avatar Feb 05 '26 20:02 ericdallo