On-Demand File Slicing: A Practical Approach to Lower Token Costs

    This article explains how on-demand file slicing reduces token usage and improves answer quality in LLM applications. It covers the core model of range, granularity, and layer parameters, slicing strategies for documents, spreadsheets, and audio/video, expected default behavior for backward compatibility, and engineering recommendations such as configuration-driven parameters, two-stage calls, caching, and token monitoring.

    1. Why Large Files Are Expensive

    In enterprise applications, long-document Q&A, financial report analysis, and meeting recording search are common requirements. Sending an entire document of hundreds of pages, a workbook with multiple sheets, or a video several hours long into the model context has two direct consequences. First, the input token count grows sharply, and so does the cost per call. Second, irrelevant content consumes attention budget, diluting the information that is truly relevant to the question and lowering answer quality.

    The traditional approach preprocesses files on the application side: split pages, extract text, cut snippets, and then assemble them into the prompt. The problem with this path is that parsing logic is scattered across business code, structural information such as layout, formulas, and timelines is easily lost during preprocessing, and every change in requirements forces a rewrite of the pipeline.

    2. Core Idea: Let the Caller Declare What to Load

    A common trait of newer file processing capabilities is moving the slicing decision from the application side up to API parameters. Instead of uploading a whole file and trimming it themselves, developers declare in the request the scope the model should inspect on the server side. The abstraction can be summarized in three layers:

    • Range layer: select page ranges for documents and slides; select sheets and cell ranges for spreadsheets; select time ranges for audio and video.
    • Granularity layer: adjust sampling precision such as resolution and frame rate, which determines information density per unit of content.
    • Layer layer: the same file can provide only the text layer, only the image layer, or parallel content such as data, formulas, transcripts, and video frames.

    An illustrative request structure is shown below (parameters are generic expressions, not a specific API):

    1. {
    2. "file": "quarterly_report.pdf",
    3. "range": { "pages": "12-18" },
    4. "resolution": "medium",
    5. "layer": ["page_text"]
    6. }
    1. {
    2. "file": "budget.xlsx",
    3. "range": { "sheet": "Q3", "cells": "B2:F40" },
    4. "layer": ["values", "formulas"]
    5. }
    1. {
    2. "file": "meeting.mp4",
    3. "range": { "start": 1200, "end": 1560 },
    4. "fps": 1,
    5. "layer": ["transcript", "frames"]
    6. }

    3. Slicing Strategies by Modality

    Documents and slides: locate content by page first, then use resolution to control information density. Low resolution is enough for searching a table of contents or locating a section; increase it when chart details need to be read. For layer selection, plain text Q&A is cheapest with the text layer, while layout-sensitive tasks should keep the page image.

    Spreadsheets: use the sheet name plus a cell range as the minimum unit to avoid loading the entire workbook. Analysis tasks take calculated values; audit and troubleshooting tasks take formulas as well; add visualization elements when trend judgment is needed.

    Video and audio: time range trimming is the main cost reduction measure, while frame rate and resolution determine the overhead on the visual side. Take the transcript layer when only semantic content is needed, and take video frames when actions or scenes must be confirmed. Audio usually only needs range trimming.

    4. Default Behavior and Compatibility

    When processing options are omitted, there should be predictable defaults: documents and slides default to page images that preserve layout, and spreadsheets default to calculated values, formulas, and sheet names for all sheets. This default design lets existing calls migrate with zero changes, while existing constraints such as file size and page count limits remain unchanged, so capacity planning does not need to be redone. The capability covers all generations of models, making unified wrapping easier.

    5. Sources of Savings and Engineering Recommendations

    Savings come from two places: billing covers only the part actually inspected, and the context is more focused, reducing wasted attention. Publicly reported practice shows that fetching text by page can use significantly fewer tokens than rendering the whole file, with sliced long-document prompts dropping by more than half and workbooks by about 30%, depending on file structure.

    Engineering recommendations: make slicing parameters business configuration rather than hard-coded values; locate at low precision first and verify at high precision, forming a two-stage call pattern; cache results for repeatedly accessed slices; use monitoring and alerts to track the input token distribution per request and trace abnormal growth to a specific file type. Baidu AI Cloud’s related product system likewise emphasizes on-demand access and cost observability, consistent with the practices described above.