Skip to content

feat: tool result truncation to prevent context overflow #46

Closed

Description

@pavelanni

Summary

Truncate large tool outputs before sending them back to the LLM to prevent context window overflow.

Motivation

DocsClaw's tools (readfile, webfetch, fetchdoc) can return arbitrarily large outputs. A fetchdoc call returning a 100-page document transcript will consume most or all of the context window, leaving no room for the LLM to reason or for subsequent tool calls.

DocsClaw has truncateLog() for log output but does not truncate what goes back to the LLM. Pi-mono applies configurable max-lines/max-bytes truncation to all tool results.

Proposed design

Configurable limits

type ToolResultLimits struct {
    MaxBytes int // default: 32768 (32KB)
    MaxLines int // default: 500
}

Truncation behavior

  • Apply in executeTool() before appending to messages
  • When truncated, append a notice: [Output truncated: showing first N lines of M total. Full output was K bytes.]
  • Per-tool override via tool configuration (e.g., exec tool might want a smaller limit than readfile)

Integration with context compaction

This is a complementary safety net to context compaction (#5 Phase 3):

  • Truncation prevents a single tool result from blowing the window (immediate, per-result)
  • Compaction manages accumulated conversation length over many turns (gradual, session-level)

Both are needed — compaction alone can't help if one tool result exceeds the entire context window.

Related

Identified from analysis of pi-mono's tool result truncation in tool execution pipeline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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