Skip to content

Token Reduction

Reduce token count while preserving meaning for LLM pipelines.

Set the reduction mode on token_reduction (a TokenReductionOptions).

Mode Effect
off No reduction; text returned as-is.
light Remove only the most common stopwords.
moderate Balanced stopword removal and redundancy filtering.
aggressive Aggressive filtering; may remove less common content words.
maximum Maximum compression; prioritizes brevity over completeness.
Python
from xberg import ExtractionConfig, TokenReductionConfig
config: ExtractionConfig = ExtractionConfig(
token_reduction=TokenReductionConfig(
mode="moderate",
preserve_important_words=True,
)
)
Python
import asyncio
from xberg import ExtractInput, extract, ExtractionConfig, TokenReductionConfig
async def main() -> None:
config: ExtractionConfig = ExtractionConfig(
token_reduction=TokenReductionConfig(
mode="moderate", preserve_important_words=True
)
)
result = await extract(ExtractInput.from_uri("verbose_document.pdf"), config)
original: int = result.results[0].metadata.get("original_token_count", 0)
reduced: int = result.results[0].metadata.get("token_count", 0)
ratio: float = result.results[0].metadata.get("token_reduction_ratio", 0.0)
print(f"Reduced from {original} to {reduced} tokens")
print(f"Reduction: {ratio * 100:.1f}%")
asyncio.run(main())