Overview
ReadingTime is an output scanner that enforces a maximum reading time on LLM responses with optional truncation.
Description
The ReadingTime output scanner ensures that LLM outputs do not exceed a specified maximum reading time. It calculates the estimated reading time based on an average reading speed of 200 words per minute. The scanner counts the words in the output and divides by this rate to determine the estimated reading time. If the output exceeds the max_time parameter (specified in minutes), the scanner flags the output as invalid. When truncate is enabled, the scanner will also truncate the output to fit within the allowed reading time instead of just flagging it. This is a simple but effective scanner that does not require any ML models, making it lightweight and fast.
Usage
Use this scanner to enforce length constraints on LLM outputs based on reading time rather than raw character or token counts. This is useful for applications with strict UX requirements (e.g., chat interfaces with limited display space), for ensuring concise responses in customer service bots, and for controlling costs in scenarios where longer outputs incur higher downstream processing costs.
Code Reference
Source Location
Signature
class ReadingTime(Scanner):
def __init__(
self,
max_time: float,
*,
truncate: bool = False,
) -> None: ...
def scan(self, prompt: str, output: str) -> tuple[str, bool, float]: ...
Import
from llm_guard.output_scanners import ReadingTime
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| prompt |
str |
Yes |
The input prompt
|
| output |
str |
Yes |
The LLM output to check for reading time
|
Constructor Parameters
| Name |
Type |
Required |
Default |
Description
|
| max_time |
float |
Yes |
N/A |
Maximum allowed reading time in minutes
|
| truncate |
bool |
No |
False |
Whether to truncate the output to fit within max_time
|
Outputs
| Name |
Type |
Description
|
| sanitized_output |
str |
The output (truncated if truncate is enabled and output exceeds max_time)
|
| is_valid |
bool |
Whether the output reading time is within the allowed limit
|
| risk_score |
float |
Risk score (-1.0 to 1.0)
|
Usage Examples
Basic Usage
from llm_guard.output_scanners import ReadingTime
# Allow maximum 1 minute of reading time (approx. 200 words)
scanner = ReadingTime(max_time=1.0)
prompt = "Summarize this article"
output = "This is a brief summary of the article content."
sanitized_output, is_valid, risk_score = scanner.scan(prompt, output)
if is_valid:
print("Output is within acceptable reading time")
else:
print(f"Output exceeds maximum reading time (risk: {risk_score})")
With Truncation
from llm_guard.output_scanners import ReadingTime
# Allow 30 seconds of reading time with truncation enabled
scanner = ReadingTime(max_time=0.5, truncate=True)
prompt = "Explain quantum physics"
output = "Quantum physics is..." + " word" * 500 # Very long output
sanitized_output, is_valid, risk_score = scanner.scan(prompt, output)
# sanitized_output will be truncated to approximately 100 words (0.5 * 200 wpm)
print(f"Original length: {len(output.split())} words")
print(f"Truncated length: {len(sanitized_output.split())} words")
print(f"Valid: {is_valid}")
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.