RAG context
Retrieved chunks arrive as lists of records with the same metadata keys. JDogo writes those keys once, so the same budget holds more context.
Measured
| Benchmark case | JSON | TOON | JDogo | vs JSON |
|---|---|---|---|---|
| Sparse RAG chunks | 836 | 1,064 | 465 | −44% |
| RAG vector retrieval, top-K | 570 | 611 | 332 | −42% |
| RAG chunks with metadata (3 chunks) | 93 | 79 | 74 | −20% |
GPT-4 tokens (cl100k) for the JDogo document. The savings grow with the number of chunks: three chunks have little to share, twenty have a lot. TOON is larger than JSON on two of these because the metadata is nested or sparse.
How
from jdogo import compress_context
context = compress_context(retrieved_chunks) # instruction + data, ready for the prompt
prompt = f"{context}\n\nQuestion: {question}"import { compressContext } from "jdogo";
const context = compressContext(retrievedChunks);from jdogo import JDogoDocumentCompressor context = JDogoDocumentCompressor().compress_documents(docs) # page_content + metadata as rows
The default layout is the one measured in live model runs: full ids and values, so the model can quote them back. Models read it about as accurately as JSON, for about half the tokens.
When it helps less
- A few long chunks of free text: the text itself dominates, and there are few keys to save.
- Chunks whose metadata keys differ widely: JDogo falls back to record lines, which save less.
Paste your own retrieval results into the playground to see the saving column by column.