Skip to content

trulens.core.utils.trace_compression

trulens.core.utils.trace_compression

Trace compression utilities for reducing token usage in LLM feedback functions.

This module provides functionality to compress trace data while preserving essential information like plans, key decisions, and error details.

Classes

TraceCompressor

Compresses trace data for LLM context windows while preserving critical information.

Uses a provider-based approach to handle different trace formats (LangGraph, etc.) while maintaining plan preservation as the highest priority.

Functions
__init__
__init__()

Initialize the trace compressor.

compress_trace_with_plan_priority
compress_trace_with_plan_priority(
    trace_data: Any,
    target_token_limit: int = DEFAULT_TOKEN_LIMIT,
) -> Dict[str, Any]

Compress trace with plan preservation as highest priority. If context window is exceeded, compress other data more aggressively.

compress_trace
compress_trace(trace_data: Any) -> Dict[str, Any]

Compress trace data using the appropriate provider.

PARAMETER DESCRIPTION
trace_data

The trace data to compress

TYPE: Any

RETURNS DESCRIPTION
Dict[str, Any]

Compressed trace data

Functions

safe_truncate

safe_truncate(s: str, max_len: int) -> str

Safely truncate a string without breaking JSON structure. Ensures we don't cut mid-escape sequence or leave unclosed quotes.

PARAMETER DESCRIPTION
s

String to truncate

TYPE: str

max_len

Maximum length

TYPE: int

RETURNS DESCRIPTION
str

Truncated string with "..." suffix if truncated, or original if not a string

compress_trace_for_feedback

compress_trace_for_feedback(
    trace_data: Any,
    preserve_plan: bool = True,
    target_token_limit: int = DEFAULT_TOKEN_LIMIT,
) -> Dict[str, Any]

Convenience function to compress trace data for feedback functions.

PARAMETER DESCRIPTION
trace_data

The trace data to compress

TYPE: Any

preserve_plan

Whether to preserve complete plan (recommended for metrics)

TYPE: bool DEFAULT: True

target_token_limit

Target token limit for context window management

TYPE: int DEFAULT: DEFAULT_TOKEN_LIMIT

RETURNS DESCRIPTION
Dict[str, Any]

Compressed trace data with plan preservation prioritized