September 1, 2026
Reduce Database Result Tokens: 7 High-Impact Tips (2026)
Learn how to Reduce Database Result Tokens by 60–90% using field filtering, token‑efficient formats, and query‑aware compression. See steps now.

TL;DR
Database result tokens are the tokens consumed when database query results (usually serialized as JSON) are injected into an LLM prompt. Most of these tokens are waste, coming from unnecessary fields, verbose formatting, and excessive rows. You can reduce database result tokens by 60-90% through techniques like field filtering, switching to token-efficient formats, query-aware compression, and SQL-side aggregation. These cuts directly lower LLM costs, reduce latency, and often improve answer accuracy.
Try compressing your own database output →
Direct Answer: Database result token optimization reduces the context window footprint consumed when database query outputs (like JSON payloads) are passed into an LLM prompt. Serialized JSON repeatedly duplicates key names, quotes, and structural syntax, making raw database outputs extremely redundant. You can reduce database result tokens by 60% to 90% using field filtering, token-efficient serialization formats (TSV, TOON, ONTO), SQL-side aggregation, and query-aware compression—significantly lowering LLM API costs and inference latency.
What Are Database Result Tokens?
Database result tokens are the tokens an LLM processes when database query results are included in its input. Every time an AI agent, text-to-SQL system, or RAG pipeline queries a database and feeds the output to an LLM, that output gets tokenized. Braces, quotes, key names, whitespace, and actual values all count.
A rough rule of thumb: one token equals about four characters in English, and 100 tokens cover roughly 75 words. That means a JSON response with 50 fields per row, multiplied across hundreds of rows, can easily eat tens of thousands of tokens before the LLM even reads the question it’s supposed to answer.
This is a distinct sub-problem within broader context compression. Database results are structured, highly repetitive, and almost always larger than what the model needs. That combination makes them one of the most compressible (and most wasteful) payload types in LLM applications.
Why Database Results Burn So Many Tokens
The waste comes from three specific places.
Unnecessary Fields
A single tool call returning a JSON blob with 50 fields when you only needed 3 can consume thousands of tokens in one shot. Practitioners working with MCP servers report that most servers “are built to return complete, accurate data, not minimal data.” That’s fine for humans reading output, but it’s wasteful when an LLM only needs a fraction of what comes back. Filtering to 3-5 relevant fields can reduce payload tokens by 80-90%.
Structural Overhead
JSON was designed 25 years ago for JavaScript interoperability, not token efficiency. When you send structured data to an LLM, JSON’s verbosity becomes expensive. Every record repeats every key name. Every string gets wrapped in quotes. Every object gets braces. Engineers at Halodoc found that “a significant chunk of those tokens was being consumed not by meaningful content, but by the structural overhead of JSON.”
Too Many Rows
A developer on the DEV Community described connecting an LLM to a SQL database via MCP, running a query, and getting back 50,000 records. This is the most common trigger for the problem. The LLM’s context window can only handle a fraction of that data, and even if it could fit, processing all of it is slow and expensive. Most of those rows add noise rather than signal.
These three factors compound. A query that returns 500 rows with 50 JSON fields each, pretty-printed with indentation, can easily hit six figures in token count. Understanding how input tokens drive costs makes it clear why this matters.
The Impact: Cost, Latency, and Accuracy
Most LLM APIs price usage per token. Tokens are the unit of cost and capacity for any LLM-based system, so bloated database results directly inflate your bill.
But it goes beyond money. Token usage in AI agents is a performance problem too. Bloated context slows inference (prefill time scales with input length), increases error rates, and hits limits on models with smaller windows. Studies consistently show that stuffing irrelevant data into a prompt degrades the quality of the model’s answers.
Seven Techniques to Reduce Database Result Tokens (Ranked by Impact)
Technique | Typical Token Reduction | Implementation Layer | Best Used For |
1. Field Filtering & Projection | 50% – 90% | MCP / API Tool Layer | Dropping unneeded attributes when required fields are known in advance. |
2. Token-Efficient Serialization (TSV/TOON/ONTO) | 30% – 61% | Serialization Layer | Multi-row record arrays with repetitive JSON keys. |
3. Query-Aware Compression | 50% – 99% | Context Compression API | Dynamic payloads where relevance depends on the user query. |
4. SQL-Side Aggregation | 90% – 99% | Database / SQL Engine | Analytical metrics (sums, counts, averages) where individual rows aren't needed. |
5. JSON Minification | 10% – 20% | Pre-processing Pipeline | Quick win to strip whitespace and line breaks from JSON payloads. |
6. Intelligent Sampling | Variable (50%+) | Pipeline Logic | Exploratory queries requiring data distribution over complete accuracy. |
7. Schema Compression | 66% – 98% (Schema only) | Text-to-SQL Prompt | Large multi-table schemas passed into Text-to-SQL agents. |
1. Field Filtering and Projection
This is the single highest-impact move. Return only the fields the agent actually needs.
The solution is field filtering at the tool or MCP server layer. Before the response hits the context window, strip it down to the required fields. One developer shared that they “wrote a simple extraction layer that maps tool names to field allowlists. Took about two hours. Saved more tokens than any other single technique.”
A concrete example: a Stripe payment object goes from 2,847 tokens with all fields to 891 tokens after filtering to only the relevant ones. That’s a 69% reduction from what amounts to a configuration change.
Typical reduction: 50-90%
2. Token-Efficient Serialization Formats
JSON repeats key names on every single record. For arrays of objects (which is what most database results are), this is the biggest structural tax. Several alternatives exist.
TSV/CSV/Markdown tables write field names once in a header row, then list only values. Benchmarks show a 2.6x gap between pretty JSON and TSV for identical information. Indents, repeated key names, quotes, and braces inflate the token count 2.6 times over.
TOON (Token-Oriented Object Notation) takes this further. A 500-row e-commerce dataset required 11,842 tokens in JSON but only 4,617 in TOON, a 61% reduction. Halodoc reported up to 15% overall cost savings across migrated production use cases in healthcare workflows.
ONTO (Object Notation for Token Optimization) uses a columnar approach, declaring field names once per entity and arranging values in pipe-delimited rows. Evaluations showed 46-51% token reduction versus JSON, with stable scaling from 100 to 1,000 records.
One practical note: TOON savings only materialize with 3+ objects having 4+ fields each. Below that threshold, header overhead dominates and the savings disappear. For small result sets, YAML only reduces tokens by 20-30% and doesn’t address the core problem of repeated keys.
Typical reduction: 30-61%
3. Query-Aware Compression
Field filtering works when you know in advance which fields matter. But what about cases where every field might be relevant, or where the relevance depends on the specific question being asked?
This is where query-specific compression comes in. Rather than hard-coding what to keep, a compression model identifies which spans of the database result are relevant to the current query and drops the rest.
Compresr’s query-aware compression API takes a query and text input, keeping only the spans needed to answer the query. On the FinanceBench benchmark at approximately 2x compression, accuracy improved from 73% to 77% versus full context, with roughly 47% cost savings. In a Boeing 10-K demonstration, the payload went from 112,552 tokens down to 498, a 226x reduction that was 86% cheaper.
This approach handles the cases that static filtering can’t, especially when database results contain variable-length text fields, nested objects, or results whose relevance shifts per query. You can compress results in batch for multi-row result sets.
See how query-aware compression fits into RAG pipelines →
Typical reduction: 50-99% (depending on compression ratio selected)
4. SQL-Side Aggregation and Summarization
Don’t send raw rows when aggregate statistics will do. Instead of dumping 10,000 sales records into the prompt, run a SQL query that computes the sum, average, count, and breakdown by category. This gives the LLM the complete statistical picture in a few dozen tokens instead of thousands.
The progressive disclosure pattern extends this idea: start with a high-level summary, then key trends, then segment analysis, then a deep dive. Each step is context-aware and builds on previous understanding. For agent workflows, this means the first LLM call gets a summary, and subsequent calls only request details if needed.
Typical reduction: 90-99% (when aggregates suffice)
5. JSON Minification
The simplest step, and the one most often overlooked. As one LinkedIn practitioner put it: “Pretty-printing is for humans. AI doesn’t need it!” Collapsing whitespace, removing newlines, and minifying JSON costs nothing in terms of accuracy but consistently saves tokens. One practitioner noted that “stripping spaces and compressing JSON felt like a tiny tweak, until I saw the token bill drop.”
Given the 2.6x gap between pretty-printed JSON and compact formats, minification alone can cut tokens significantly without any change to the data itself. If you’re still sending indented JSON to your LLM, this is the easiest win available. For more tactics like this, see this guide on reducing input tokens.
Typical reduction: 20-40%
6. Intelligent Sampling
When not all data points are equally important, send a representative sample rather than everything. Stratified sampling (selecting, say, 10 rows from each category) preserves the distribution of the data while dramatically cutting volume.
This works especially well for exploratory questions (“What kinds of products do we sell?”) where completeness matters less than coverage. For precise analytical questions, aggregation (technique #4) is usually the better choice.
Typical reduction: Variable, depends on sample size
7. Schema Compression for Text-to-SQL
This addresses a related but distinct problem: the database schema itself burns tokens. Describing table names, column names, data types, and relationships as part of the model input is expensive because LLM providers charge as a function of tokens read.
The Schemonic system automatically finds concise text descriptions of relational database schemas using abbreviations and grouping of schema elements. The YORO approach goes further, directly internalizing database knowledge into the model’s parametric memory during training, reducing input token length by 66-98%.
Schema compression matters most for text-to-SQL pipelines where the full schema is included in every prompt. It’s worth noting this is a separate concern from result compression. Many teams need both.
Typical reduction: 66-98% (for schema portion only)
How to Choose and Stack Techniques
No single technique covers every scenario. The right combination depends on your pipeline.
If you know which fields matter ahead of time: Start with field filtering (technique #1). It’s the highest-impact, lowest-effort change. Layer JSON minification on top for free savings.
If your result sets are large but only summaries are needed: Use SQL-side aggregation before the data ever leaves the database. This eliminates the problem at the source.
If payloads are dynamic or query-dependent: Query-aware compression handles what static filtering can’t. It’s particularly useful when every field might be relevant depending on the question, or when results contain long text values.
If you’re building text-to-SQL agents: Address both schema tokens and result tokens. Compress the schema description, filter the results, and consider a token-efficient format for the output.
Stacking works well. A typical high-performance pipeline might filter to relevant fields, serialize as TSV instead of JSON, then apply query-aware compression on the result. Each technique targets a different source of waste, so their savings compound rather than overlap.
For teams evaluating different compression approaches, the key question is whether your waste comes from structure (use format changes), from irrelevant fields (use filtering), or from irrelevant content within relevant fields (use query-aware compression).
Code Implementation: Before vs. After Optimization
1. Raw JSON vs. Tabular Representation
Passing standard raw JSON duplicates field names on every record, adding heavy token overhead:
BEFORE: Heavy JSON Overhead (~120 tokens) [ {"product_id": "P-101", "name": "Mechanical Keyboard", "category": "Electronics", "price": 120.00, "in_stock": true}, {"product_id": "P-102", "name": "Ergonomic Mouse", "category": "Electronics", "price": 55.00, "in_stock": true} ]
By switching to TSV or column-oriented representations, field definitions appear once:
AFTER: Columnar / Tabular Output (~48 tokens) product_id | name | category | price | in_stock P-101 | Mechanical Keyboard | Electronics | 120.00 | true P-102 | Ergonomic Mouse | Electronics | 55.00 | true
2. Field Filtering at the MCP Tool Layer
Filter API outputs down to specific keys before sending the response to the LLM context:
Python Example: def filter_tool_output(data: list[dict], allowed_fields: set[str]) -> list[dict]: return [ {k: v for k, v in record.items() if k in allowed_fields} for record in data ]
Usage
raw_stripe_response = fetch_stripe_charge(charge_id) clean_payload = filter_tool_output(raw_stripe_response, {"id", "amount", "status"})
4. Text Corrections to Update in Existing Paragraphs
-
Update Technique #5 (JSON Minification): Change the typical reduction statistic from
20-40%to10-20%(since removing whitespace/newlines rarely exceeds 20% on standard database records without structural key changes). -
Update Model Benchmark Reference in Technique #3: Ensure the FinanceBench evaluation notes modern LLM model context references like GPT-5.2 to keep the benchmark up to date.
Frequently Asked Questions
What counts as a “database result token”?
Any token the LLM processes that originated from a database query result. This includes the actual data values, but also every JSON key name, brace, bracket, quote, colon, and whitespace character in the serialized output. All of it gets tokenized and billed.
How much can I save by just switching from JSON to TSV?
Benchmarks show a 2.6x token gap between pretty-printed JSON and TSV for identical data. If your results are arrays of objects with repeated keys (which most database results are), switching to a tabular format like TSV or Markdown tables can cut tokens by 50-60% with no data loss.
Does reducing tokens hurt LLM accuracy?
Usually the opposite. Removing irrelevant fields, rows, and formatting noise lets the model focus on what matters. On FinanceBench, compressed input at 2x compression actually improved accuracy from 73% to 77% compared to full context. Noise hurts reasoning more than conciseness does.
Should I compress the schema, the results, or both?
Both, if you’re running a text-to-SQL pipeline. Schema compression reduces the tokens spent describing your database structure. Result compression reduces the tokens spent on query output. They target different parts of the prompt and their savings stack.
When is query-aware compression better than field filtering?
When you can’t predict ahead of time which parts of the result matter. Field filtering requires a static allowlist. Query-aware compression evaluates relevance dynamically based on the current question, making it essential for general-purpose agents that handle diverse queries.
What’s the minimum result size where compression is worthwhile?
For format changes (JSON to TSV), you need at least 3 objects with 4+ fields each before the header overhead pays off. For API-based query-aware compression, results under roughly 500 tokens may not justify the API call overhead. Above that threshold, savings scale linearly.
How do MCP servers contribute to this problem?
Most MCP servers return complete, richly structured responses because they’re built for accuracy and completeness. When an LLM agent makes a tool call through MCP and gets back a full database result as JSON, the structural overhead alone (repeated keys, nested objects) can account for 50-60% of the tokens. Field filtering at the MCP layer is one of the most effective interventions.
Can I combine multiple techniques?
Yes, and you should. Field filtering, format optimization, and query-aware compression each target different sources of waste. A pipeline that filters to relevant fields, serializes as TSV, and then applies compression will see compounding savings that no single technique achieves alone.