Adding Search and File Reading to LLM APIs: A Practical Guide
This article breaks down how modern LLM APIs integrate web search, document reading, and related capabilities through tool calling and standalone endpoints. It explains the two-layer architecture, how the cost structure shifts from self-built glue code to per-call billing, a typical request flow with sample payloads, and key engineering practices around context budgets, caching, fallback, citations, and observability. Developers will find guidance on choosing between in-chat tool switches and independent services based on coverage, cost predictability, and compliance needs.
1. Background: From Pure Chat to Action-Ready APIs
Over the past two years, the core capability of LLM APIs has mostly stayed at the text generation layer: you provide a prompt, and the model returns a completion. This works well enough for Q&A, summarization, and rewriting. In real business scenarios, however, the limitations become obvious—the model has no access to real-time information, cannot interpret documents uploaded by users, and cannot proactively fetch external web content.
The industry has converged on a common approach: expose external capabilities such as retrieval and file parsing to the chat API through tool calling. A recent example is a cloud vendor making search, retrieve-while-reasoning, document reading, and link reading available on demand within the chat API, while also splitting search crawling, document parsing, speech recognition, and speech synthesis into standalone endpoints. This reflects the broader trend.
It is worth stressing that these capabilities are not unique to any single vendor. They are combinations of common industry techniques: retrieval-augmented generation, tool calling, multimodal document parsing, and service-oriented speech pipelines. Understanding the architectural logic is more valuable than memorizing a specific endpoint name.
2. Capability Layers: In-Chat Tools vs. Standalone Endpoints
Such offerings typically come in two layers, and they carry very different engineering implications.
The first layer consists of tool switches inside the chat API. Assuming compatibility with mainstream API protocols, enabling the relevant tools on demand gives you several capabilities:
- Web search: the model triggers retrieval before or during generation and incorporates external results into the context;
- Deep retrieval: the model decides what to search next while reasoning, and a single round can include multiple tool operations such as searching, opening pages, and in-page lookup;
- File input: support for common office document formats, with limits on per-file size and page count;
- Link reading: fetching the content at a given URL, structuring it, and injecting it into the context;
- Content safety checks: compliance validation on both input and output.
The second layer consists of five independently callable services: search, crawling, document parsing, speech recognition, and speech synthesis. They do not depend on chat context and can be embedded directly into existing business systems.
The key difference: the first layer performs tool invocation and context assembly on the server side, while the second layer exposes each capability as an atomic service that can be orchestrated. The former lowers the barrier to entry; the latter improves architectural flexibility.
3. Cost Structure Shift: From Glue Code to Per-Call Billing
The most notable point in the original discussion is the change in cost structure, which can be broken down more rigorously.
A traditional self-built solution incurs three types of cost: model invocation fees, self-built toolchain costs, and R&D plus maintenance labor. The self-built toolchain includes retrieval services, parsing services, a proxy layer, and retry and rate-limiting logic—and labor cost often dominates over the long term.
Once tooling moves server-side, the cost structure becomes: model invocation fees + tool invocation fees + a small amount of client-side logic cost. Three things change:
- Fixed labor investment turns into variable invocation spending; upfront cost drops, and spending grows linearly as scale increases;
- Billing becomes more granular—retrieval and crawling may be billed per call, on top of token-based billing;
- Token consumption rises due to context expansion, which is especially noticeable in long-document and multi-round retrieval scenarios.
An illustrative cost model can be expressed as:
Cost per request ≈ input tokens × unit price + output tokens × unit price+ number of retrievals × price per retrieval+ number of crawls × price per crawl+ fee based on pages or size parsed
The engineering takeaway: tool invocation counts and injected context length must be included in observability metrics. Otherwise, the bill can spiral quickly once traffic grows.
4. Typical Call Flow and Sample Code
Take “Q&A with web access” as an example. Client logic can be simplified into four steps: build the request, enable tools, receive structured results, and render citations.
{"model": "<model identifier>","messages": [{"role": "user", "content": "Look up the latest industry data and summarize it"}],"tools": ["web_search"],"max_tool_rounds": 5}
The response typically includes the main text, a tool invocation trace, and a citation list. The client needs to render citations as clickable sources, collapse the tool trace for display, and truncate or summarize overly long context.
With standalone endpoints, the flow changes: the business system first calls retrieval to obtain snippets, then assembles a prompt and calls the chat API. The chain is longer but offers more control.
5. Context and Engineering Best Practices
- Context budget: set caps on the number and length of retrieved snippets so a single injection does not crowd out conversation history.
- Caching strategy: short-term caching of retrieval results for high-frequency queries can reduce both latency and cost.
- Failure fallback: when retrieval returns nothing or times out, fall back to a pure model answer and clearly tell the user the information may be outdated.
- Traceable citations: all external information must carry sources for verification and compliance auditing.
- Security boundaries: file input must restrict type, size, and page count; link reading must guard against prompt injection.
- Observability: bring tool invocation counts, token consumption, and failure rates into monitoring and alerting, with log services for root-cause analysis.
6. Use Cases and Selection Guidance
Scenarios suited to this approach include customer service and operations assistants that need real-time information, knowledge base Q&A that requires long-document understanding, and research-oriented applications that need source citations.
When choosing, evaluate against three criteria: whether the capabilities cover the business workflow, whether billing is predictable, and whether data compliance requirements are met. If the business already has mature retrieval and parsing systems, standalone endpoints are the better fit; if rapid launch is the priority, in-chat tool switches are more efficient.
Overall, connecting search and file reading to a chat API is essentially standardizing part of how an agent runs and moving it server-side. Developers need to redesign their cost model and context strategy to strike a balance between convenience and control.
