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¶
safe_truncate
¶
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:
|
max_len
|
Maximum length
TYPE:
|
| 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:
|
preserve_plan
|
Whether to preserve complete plan (recommended for metrics)
TYPE:
|
target_token_limit
|
Target token limit for context window management
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
Compressed trace data with plan preservation prioritized |