fix(tools): implement dynamic tool output truncation to prevent LLM context overflow#317
Open
Adar5 wants to merge 1 commit intojenkinsci:mainfrom
Open
fix(tools): implement dynamic tool output truncation to prevent LLM context overflow#317Adar5 wants to merge 1 commit intojenkinsci:mainfrom
Adar5 wants to merge 1 commit intojenkinsci:mainfrom
Conversation
0e37ac6 to
882c508
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Problem
By default, LangChain attempts to inject the raw string output of any executed tool directly into the context window. If a tool returns a massive un-chunked payload (e.g., a large plugin document or a future Jenkins log fetcher), it instantly crashes the LLM provider with a TokenLimitExceeded or OOM error, freezing the chat session.
The Solution
This PR implements a lightweight, dynamically configured defensive wrapper (truncate_tool_output decorator) around the TOOL_REGISTRY.
Key Changes:
Configuration: Added max_tool_output_length to config.yaml (default: 4000), allowing server admins to control memory limits without touching Python code.
Interception: The decorator intercepts all tool executions before they reach the LangChain agent, hard-capping string outputs at the configured limit.
Observability: Added standard server-side logging (logger.warning) so DevOps is notified when a truncation event occurs.
Context Awareness: Appends a clean [SYSTEM WARNING] so the LLM context is explicitly aware that the data was truncated and can inform the user.
Testing
[x] Added test_tool_overflow.py to assert massive 50,000+ character strings are safely truncated based on the config.yaml limit without blocking the event loop.