ToolOutputCompressor

org.llm4s.context.ToolOutputCompressor

Handles intelligent compression and externalization of tool outputs.

Tool outputs (from function calls, API responses, file reads) can be very large and quickly consume the context window. This compressor applies content-aware strategies to reduce their size while preserving essential information.

==Content Type Detection==

The compressor automatically detects content types:

  • '''JSON/YAML''': Removes null values, empty strings, truncates large arrays
  • '''Logs''': Keeps head/tail, collapses repeated lines
  • '''Errors''': Preserves error message and top stack frames
  • '''Binary''': Replaces with placeholder (always externalized)
  • '''Text''': Generic word-based truncation

==Size Thresholds==

Content is processed based on size:

  • '''< 2KB''': Kept as-is (no compression)
  • '''2KB - 8KB''': Inline compression (type-specific)
  • '''> 8KB''': Externalized to ArtifactStore with content pointer

==Externalization==

Large content is stored in an ArtifactStore and replaced with a pointer:

[EXTERNALIZED: abc123... | JSON | JSON object with 42 fields, 15234 bytes]

The original content can be retrieved using the artifact key.

Attributes

See also

ArtifactStore for content storage interface

DeterministicCompressor which uses this for the tool compaction phase

Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def compressToolOutputs(messages: Seq[Message], artifactStore: ArtifactStore, threshold: ExternalizationThreshold): Result[Seq[Message]]

Compress tool messages using externalization and schema-aware strategies.

Compress tool messages using externalization and schema-aware strategies.

Processes all ToolMessage instances in the sequence, leaving other message types unchanged. Each tool message is analyzed for content type and size, then compressed or externalized accordingly.

Value parameters

artifactStore

Storage for externalized content

messages

The messages to process

threshold

Size threshold for externalization (default: 8KB)

Attributes

Returns

Processed messages with compressed tool outputs