app icon
Text Fitter
0.0.1

Smart text trimming to keep input within LLM context window token limits.

alexmultiagent/text_fitter148 installs

Text Fitter

A Dify tool plugin that ensures text fits within LLM context window limits
via intelligent extractive summarization. Supports Chinese (Simplified &
Traditional), Japanese, and English text.

Why

Locally deployed or resource-constrained LLM instances often have a smaller
effective context window than the model's official specification — due to
hardware limits (GPU VRAM), concurrency requirements, or serving parameters
like . When input text exceeds the window, the LLM fails
with a context-length error.

This plugin acts as a pre-processing guard: it measures the input, and if it
exceeds a user-configured threshold, trims it by extracting only the
highest-scoring sentences — before the text ever reaches the LLM. No API
keys, no network calls, no external dependencies.

Installation

From Dify Marketplace

  1. In your Dify workspace, go to PluginsMarketplace.
  2. Search for Text Fitter and click Install.
  3. The plugin will appear in your workflow tools as Smart Trim.

Manual Installation

  1. Download the file from the GitHub releases page.
  2. In Dify, go to PluginsInstall PluginUpload Package.
  3. Upload the file.

Usage

  1. In a workflow, add the Smart Trim node from the tool palette.
  2. Wire to your upstream content source (document parser, HTTP input, etc.).
  3. Set to a value below your LLM's effective context limit.
  4. Connect the node's text output to your downstream LLM node.
  5. Optionally use to branch logic (e.g., log a warning when trimming occurred).

Choosing max_chars

is a character count threshold — the plugin checks
against it. It does not measure tokens.

As a rough guide, using the Qwen3 BBPE tokenizer (~151K vocab; Qwen3 Technical Report, 2025):

LanguageTokens per charChars fitting in 20K tokens
English~0.25 (1 token ≈ 4 chars)~80,000
Chinese~0.6 (1 token ≈ 1.7 chars)~33,000
Japanese~0.8 (1 token ≈ 1.3 chars)~25,000

Token-to-character ratios vary across tokenizers (source: TokLens, ACL 2026 SRW).
Always verify with your specific model when precise budgeting is critical.

Reserve ~80% of the context window for input text, leaving headroom for
prompt templates and output generation.

Parameters

ParameterTypeRequiredDefaultDescription
stringYesInput text to process
numberYes30000Character threshold; exceeding triggers trimming
selectNoSentence selection: (diverse) or (fast)
selectNoMMR relevance weight. 0.0 (diversity) to 1.0 (relevance). Ignored with

Outputs

OutputTypeDescription
stringThe processed text (original or trimmed)
numberCharacter count of the original input text
numberCharacter count of the output text
booleanWhether the text was trimmed
numberCompression ratio (original / processed). 1.0 when not trimmed
stringAlgorithm actually used. See Algorithm

Language Support

The plugin interface supports four locales:

LocaleLanguage
English
Simplified Chinese
Traditional Chinese
Japanese

All parameter labels, descriptions, and option values are translated across
the supported locales.
Text processing handles Chinese, Japanese, and English, with CJK-aware
sentence splitting and abbreviation protection.

Effectiveness & Boundaries

This plugin is not a replacement for LLM summarization. It selects complete
verbatim sentences from the original text using extractive summarization, a
long-established NLP approach — it never rewrites or paraphrases.

When It Works Well

  • Structured documents (reports, papers, contracts) where key points are concentrated in topic sentences
  • Dialogue / transcripts — removing filler and repeated ideas
  • Moderate compression — enough budget for main points across different sections

When It Doesn't

  • Narrative / creative text — information is spread across descriptions
  • Aggressive compression — any extractive method will lose significant content
  • When you need synthesis — this tool selects sentences; it cannot merge or rephrase them

Algorithm

This plugin uses extractive summarization — Python standard library only,
no external NLP dependencies. It selects complete sentences from the original
text; it never rewrites, paraphrases, or cuts mid-sentence.

Sentence Segmentation

Regex-based sentence splitting aware of CJK, Japanese, and English
punctuation conventions, with abbreviation protection.

LanguageSentence-ending markers
Chinese
Japanese
English followed by uppercase or CJK character

Sentence Scoring

  • Position (0.3): Intro (first 20%) and conclusion (last 10%) weighted higher.
  • Keyword Density (0.5): Normalized TF-IDF. Rare tokens get higher weight.
  • Length (0.2): Penalizes very short (< 10 chars) and very long (> 200 chars) sentences.

Sentence Selection

Two strategies via the parameter:

Greedy — Sort sentences by score descending, pick top ones until the
character budget is exhausted. O(n log n).

MMR (Maximal Marginal Relevance) — Iteratively selects sentences that
maximize:

where λ () controls the relevance–diversity trade-off:

  • λ = 1.0 → pure relevance (same as Greedy)
  • λ = 0.7 → moderately favors relevance (default)
  • λ = 0.0 → pure diversity (maximum topical variation)

Diversity is measured as token overlap (Jaccard-like) between candidate and
already-selected sentences, updated incrementally per round. O(k × n) overall.

The output variable records which variant was actually used:

valueTriggerBehavior
text ≤ max_charsNo processing; return original text
method = Pure score-ranked selection
method = , ≤ 5000 sentencesFull MMR on all sentences
method = , > 5000 sentencesScore-ranked pre-filter to top 5000 candidates, then MMR
emergency fallbackNo sentence fits budget; cut at sentence/word boundary

Reordering & Fallback

Selected sentences are re-sorted by original document order for coherent
output. If no sentence fits within , a boundary-aware fallback
truncates at sentence-ending punctuation → whitespace → hard cut with
ellipsis.

Complexity

MetricValue
Worst-case timeO(k × n) for MMR, O(n log n) for Greedy (n = candidates, k = selected sentences)
SpaceO(n)
DependenciesNone (Python stdlib only)

Privacy

This plugin processes all text locally. No data is transmitted to external
servers, APIs, or third-party services. See PRIVACY.md [blocked] for details.

Support

GitHub profile: https://github.com/AlexMultiAgent

GitHub Issues: https://github.com/AlexMultiAgent/dify-plugin-text-fitter/issues

License

MIT [blocked]

CATEGORY
Tool
TAGS
PRODUCTIVITYUTILITIES
VERSION
0.0.1
alexmultiagent·05/27/2026 02:02 AM
REQUIREMENTS
Tool invocation
Maximum memory
256MB