Integrating Search and File Parsing into LLM APIs: A Practical Guide

    This guide explains how LLM APIs are evolving from plain text completion services into runtime environments with built-in tools. It covers the shift of orchestration and context assembly to the server side, the move from linear to hybrid cost models, a reusable cost estimation framework, testing approaches for six common scenarios, and an engineering checklist for budgets, observability, security, and graceful degradation.

    1. The Trend: From Model Endpoint to Runtime Endpoint

    A clear product direction has emerged in the industry: a cloud vendor has started exposing capabilities that previously existed only inside its own applications—live web search, search during reasoning, document reading, and link fetching—as optional features of the chat endpoint, while speech recognition and speech synthesis remain separate endpoints. For developers, this is not just a few extra switches. It is a shift in where the API boundary sits: the LLM API is evolving from a pure text completion service into a runtime environment that ships with its own tool chain.

    To understand this, split the capabilities into two layers.

    The first layer is the tool capabilities built into the chat endpoint, typically including: web search; deep research (the model decides on its own what to search next during reasoning, and runs multiple rounds of search, page opening, and in-page locating within a single task); file input (commonly PDF, Word, and presentation formats, with limits on per-file size and page count); link reading; and content safety checks. The second layer is a set of atomic endpoints that can be called independently, such as search, web page fetching, optical character recognition, speech recognition, and speech synthesis.

    The real value is that tool orchestration and context assembly have moved to the server side. Building a usable retrieval-augmented application used to require developers to wire up search, parse documents, chunk and rerank, and then write a layer of “glue code” to stuff the results back into the prompt. The platform now takes on part of that work, while the client still owns conversation state management, streaming rendering, and result display.

    2. Architecture: Who Owns the Glue Code

    The traditional pipeline usually looks like this:

    1. User question -> client decides whether to search -> call search API
    2. -> fetch page content -> parse documents -> chunk and rerank
    3. -> manually assemble context -> call model -> parse tool calls -> back to step one

    With tools embedded, the pipeline shrinks to:

    1. User question -> enable the relevant tools -> model orchestrates search and reading on its own
    2. -> server assembles the context -> stream results and citations back

    The change brings three direct benefits. First, intent detection moves from “rules up front” to “model decision,” reducing both misses and false triggers. Second, consistency of context assembly is guaranteed by the platform, lowering the maintenance cost of prompt engineering. Third, citation sources and intermediate steps can be returned in a structured form, which makes it easier for the frontend to show provenance.

    The costs are equally clear: less controllability, coarser observability granularity, and a cost model that shifts from “linear per token” to a hybrid of “per token plus per tool call.” Engineering therefore has to add three things: budget caps, process logs, and a fallback path.

    3. Cost Structure: From Linear to Hybrid Billing

    Under the traditional model, cost is roughly: input tokens × unit price + output tokens × unit price. Once server-side tools are involved, at least three more cost items appear: fees per tool call, extra tokens from fetching and parsing, and repeated consumption from failed retries.

    A reusable estimation framework:

    1. def estimate_cost(q, tool_calls, fetch_pages, retry=0):
    2. # q: base conversation tokens; tool_calls: number of tool operations
    3. # fetch_pages: extra tokens converted from fetched pages of web pages/documents
    4. base = q * PRICE_TOKEN
    5. tools = tool_calls * PRICE_TOOL
    6. extra = fetch_pages * AVG_TOKEN_PER_PAGE * PRICE_TOKEN
    7. total = (base + tools + extra) * (1 + retry)
    8. return total

    Practical recommendations:

    • Set a hard cap on tool calls and total budget per session, and fall back to a pure model answer automatically when the cap is exceeded;
    • Build a semantic cache for high-frequency repeated questions, and skip the tool chain on a cache hit;
    • Summarize fetched page content before putting it into the context instead of inserting the whole document;
    • Separate “exploratory requests” from “factual requests”: allow deep research for the former, and prefer cache and lightweight search for the latter.

    4. Testing Six Typical Scenarios

    Even for general-purpose capabilities, run a baseline comparison with a consistent method rather than shipping on gut feeling.

    1. Baseline chat: turn off all tools and record time to first token, total latency, and token consumption as the control.
    2. Web search: check whether search is actually triggered, whether citations are clickable, and whether the answer contains unsourced assertions.
    3. Deep research: track the distribution of tool operations within a single task and evaluate whether “more searching means better accuracy” holds.
    4. Link reading: test content extraction quality, especially for pagination, tables, and code blocks.
    5. File input: cover very long documents, scanned PDFs, and presentations with charts, and record the parsing failure rate.
    6. Standalone atomic endpoints: call the search or recognition endpoint on its own to verify it works independently of the chat pipeline.

    Four metrics should stay fixed: task success rate, factual consistency, end-to-end latency, and cost per task. All four must be read together; looking at any single one leads to the wrong conclusion.

    5. Engineering Checklist

    • Idempotency and retries: tool calls should carry a request identifier to avoid duplicate billing from retries;
    • Streaming and interruption: let users stop mid-way and release the remaining tool budget promptly;
    • Citation conventions: bind each factual point in the answer to a source snippet where possible, to make auditing easier;
    • Security boundaries: files and links are untrusted input and need prompt injection filtering and content safety checks;
    • Observability: bring tool call counts, cache hit rate, and parsing failure rate into monitoring and alerting;
    • Cost control: enforce tenant-level quotas at the gateway layer, and close the loop with logging and monitoring/alerting services.

    6. Conclusion

    Wiring search and file parsing into the chat endpoint is essentially standardizing the dirtiest, most tedious part of agent engineering. The developer’s focus therefore moves forward to three places: budget governance, result trustworthiness, and experience degradation. If an organization already uses Baidu AI Cloud’s model services along with general capabilities such as its logging service, monitoring and alerting, and object storage, it can build gateways and quota systems with the same layered approach, moving from “it works” to “controllable, measurable, and operable.”