Skip to main content
CompressContextTool is a LangChain BaseTool that reduces prompt size without any external LLM call. It collapses whitespace, removes common English stop words, and optionally truncates to a maximum character length. It is compatible with LangChain tool runners and can be passed directly to an ExecutionAgent. Tool name: compress_context

Import

from tools.compress_context_tool import CompressContextTool

Extends

langchain_core.tools.BaseTool

Constructor

max_length
Optional[int]
default:"4000"
Default truncation limit in characters. Applied when _run is called without an explicit max_length override. Set to None to disable truncation entirely.

Methods

_run

Synchronously compresses the input text.
def _run(self, text: str, max_length: Optional[int] = None) -> str
text
str
required
The raw text to compress.
max_length
Optional[int]
default:"None"
Per-call character limit override. When provided, takes precedence over the instance-level max_length. When omitted, the instance default is used.
returns
str
The compressed string. If truncation occurred, the string ends with "... [TRUNCATED]".

Compression pipeline

  1. Collapse all whitespace sequences (including newlines) to a single space and strip leading/trailing whitespace.
  2. Remove the following stop words (case-insensitive): a, an, the, is, are, was, were, and, or, but.
  3. Collapse any double spaces introduced by stop-word removal.
  4. Truncate to max_length characters and append "... [TRUNCATED]" if the result exceeds the limit.
Stop words are matched with surrounding spaces (e.g., " the ") to avoid partial-word removal. A word like "theater" is not affected.

_arun

Async version. Delegates directly to _run.
def _arun(self, text: str, max_length: Optional[int] = None) -> str
text
str
required
The raw text to compress.
max_length
Optional[int]
default:"None"
Per-call character limit override.
returns
str
Same output as _run.

Utility Function

A standalone helper is also exported for use without instantiating the full BaseTool:
def compress_text_locally(text: str, max_length: int = 4000) -> str
Internally creates a CompressContextTool(max_length=max_length) and calls _run.

Usage Example

from tools.compress_context_tool import CompressContextTool, compress_text_locally

# --- As a LangChain tool (e.g. passed to ExecutionAgent) ---
compressor = CompressContextTool(max_length=3000)

long_text = """
The quick brown fox is jumping over the lazy dog and the cat was sitting
on the mat.  It was a warm and sunny day in the park, but the wind was
starting to pick up.
"""

compressed = compressor.invoke(long_text)
print(compressed)
# => "quick brown fox jumping over lazy dog cat sitting mat. warm sunny day park, wind starting pick up."

# --- Per-call length override ---
short = compressor._run(long_text, max_length=50)
print(short)
# => "quick brown fox jumping over lazy dog cat... [TRUNCATED]"

# --- As a standalone utility ---
result = compress_text_locally(long_text, max_length=2000)
print(result)

# --- Pass to SequentialWorkflow as context compressor ---
from workflows import SequentialWorkflow

workflow = SequentialWorkflow(
    agents={"planner": planner, "executor": executor, "monitor": monitor},
    tools=[compressor],  # tools[0] is used as the compressor
)

Build docs developers (and LLMs) love