Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(rag): exclude overflow chunk from kb_prompt token budget (#16377)

Harsh Kashyap3cbf61f5f2e0
diff --git a/rag/prompts/generator.py b/rag/prompts/generator.py
index b25af285dc..06a91d42b5 100644
--- a/rag/prompts/generator.py
+++ b/rag/prompts/generator.py
@@ -137,19 +137,20 @@ def message_fit_in(msg, max_length=4000):
def kb_prompt(kbinfos, max_tokens, hash_id=False):
- knowledges = [get_value(ck, "content", "content_with_weight") for ck in kbinfos["chunks"]]
+ chunks = kbinfos["chunks"]
+ knowledges = [get_value(ck, "content", "content_with_weight") for ck in chunks]
kwlg_len = len(knowledges)
used_token_count = 0
- chunks_num = 0
- for i, c in enumerate(knowledges):
+ selected_chunks = []
+ for ck, c in zip(chunks, knowledges):
if not c:
continue
- used_token_count += num_tokens_from_string(c)
- chunks_num += 1
- if max_tokens * 0.97 < used_token_count:
- knowledges = knowledges[:i]
- logging.warning(f"Not all the retrieval into prompt: {len(knowledges)}/{kwlg_len}")
+ chunk_tokens = num_tokens_from_string(c)
+ if max_tokens * 0.97 < used_token_count + chunk_tokens:
+ logging.warning(f"Not all the retrieval into prompt: {len(selected_chunks)}/{kwlg_len}")
break
+ used_token_count += chunk_tokens
+ selected_chunks.append(ck)
def draw_node(k, line):
if line is not None and not isinstance(line, str):
@@ -159,7 +160,7 @@ def kb_prompt(kbinfos, max_tokens, hash_id=False):
return f"\n├── {k}: " + re.sub(r"\n+", " ", line, flags=re.DOTALL)
knowledges = []
- for i, ck in enumerate(kbinfos["chunks"][:chunks_num]):
+ for i, ck in enumerate(selected_chunks):
cnt = "\nID: {}".format(i if not hash_id else hash_str2int(get_value(ck, "id", "chunk_id"), 500))
cnt += draw_node("Title", get_value(ck, "docnm_kwd", "document_name"))
cnt += draw_node("URL", ck.get("url", ""))
diff --git a/test/unit_test/rag/prompts/test_kb_prompt_metadata.py b/test/unit_test/rag/prompts/test_kb_prompt_metadata.py
index 86d96eeec3..e2a4bf5e33 100644
--- a/test/unit_test/rag/prompts/test_kb_prompt_metadata.py
+++ b/test/unit_test/rag/prompts/test_kb_prompt_metadata.py
@@ -85,3 +85,67 @@ class TestKbPromptDocumentMetadata:
assert len(rendered) == 1
assert "author: alice" in rendered[0]
assert "year: 2026" in rendered[0]
+
+
+class TestKbPromptTokenBudget:
+ """Regression tests for kb_prompt token-budget trimming."""
+
+ @pytest.mark.p1
+ def test_excludes_chunk_that_exceeds_token_budget(self):
+ """The chunk that pushes usage over the budget must not be rendered."""
+ kbinfos = {
+ "chunks": [
+ {
+ "id": "chunk-1",
+ "content_with_weight": "fits " * 20,
+ "docnm_kwd": "doc1.pdf",
+ },
+ {
+ "id": "chunk-2",
+ "content_with_weight": "overflow " * 200,
+ "docnm_kwd": "doc2.pdf",
+ },
+ {
+ "id": "chunk-3",
+ "content_with_weight": "never " * 200,
+ "docnm_kwd": "doc3.pdf",
+ },
+ ]
+ }
+
+ rendered = kb_prompt(kbinfos, max_tokens=50)
+
+ assert len(rendered) == 1
+ assert "doc1.pdf" in rendered[0]
+ assert "overflow" not in rendered[0]
+ assert "never" not in rendered[0]
+
+ @pytest.mark.p1
+ def test_empty_chunk_does_not_shift_selected_chunks(self):
+ kbinfos = {
+ "chunks": [
+ {
+ "id": "chunk-1",
+ "content_with_weight": "first chunk",
+ "docnm_kwd": "doc1.pdf",
+ },
+ {
+ "id": "chunk-2",
+ "content_with_weight": "",
+ "docnm_kwd": "empty.pdf",
+ },
+ {
+ "id": "chunk-3",
+ "content_with_weight": "second chunk",
+ "docnm_kwd": "doc2.pdf",
+ },
+ ]
+ }
+
+ rendered = kb_prompt(kbinfos, max_tokens=10000)
+
+ assert len(rendered) == 2
+ assert "doc1.pdf" in rendered[0]
+ assert "doc2.pdf" in rendered[1]
+ assert "empty.pdf" not in "\n".join(rendered)