Building an AI Application with Web Search and File Understanding from Scratch
Conversational AI applications are evolving from simply "talking" to actually "doing" — searching the web in real time, parsing user-uploaded documents, and reading web pages. This article explains the five core capabilities behind such applications (web search, deep reasoning retrieval, file input, web page reading, and content safety), compares client-side and server-side orchestration architectures, and covers context management, citation traceability, cost control, stability, and compliance. A practical checklist is included to help developers ship a minimum viable product.
1. Background: From “Talking” to “Doing”
Early conversational applications relied solely on knowledge learned during model training, which meant knowledge cutoffs, no access to user files, and no way to verify up-to-date information. Over the past two years, LLM applications have widely adopted external capabilities: real-time web retrieval, parsing user-uploaded documents, reading the content of links, and speech recognition and synthesis. These capabilities make answers timely and traceable to concrete sources.
For developers, the challenge is not any single capability, but chaining multiple capabilities together. A single user question may trigger a search first, then fetch two web pages, then parse a PDF, and finally place all materials into the context to generate an answer. Each step requires its own handling of input/output formats, failure retries, timeout control, and cost accounting.
2. Five Common Capabilities and Their Boundaries
2.1 Web Search
The model decides whether retrieval is needed based on the question. Search results typically include a title, snippet, link, and publication time, which the model uses to compose an answer with sources attached. Design considerations include:
- Clearly define the scenarios where retrieval is allowed, avoiding unnecessary calls for chit-chat or common-sense questions;
- Limit the number of returned results to control context length;
- Preserve original links so users can verify the information.
2.2 Deep Reasoning Retrieval
For questions requiring multi-step reasoning, the model alternates between cycles of “think, search, read, think again.” This mode suits complex research tasks, but latency and call counts are significantly higher than single-round search. In engineering practice, you should set a maximum number of steps and a total time budget; when limits are exceeded, return the conclusions gathered so far and state the uncertainty of the information.
2.3 File Input
File parsing generally follows two paths:
- Plain text extraction: suitable for simply structured, text-based documents — fast and low-cost.
- Mixed text-and-image parsing: suitable for scanned documents or files containing tables and images, usually requiring OCR — slower and more expensive.
Parsing results should be annotated with page numbers or paragraph positions. Without positional information, the citations produced by the model cannot be traced back to the original text.
2.4 Web Page Reading
When a user message contains a link, the system must first determine the link type and then fetch the page content. In practice, three things must be handled: stripping noise such as navigation bars, ads, and scripts; summarizing overly long pages in segments; and blocking internal network addresses and loopback addresses to prevent server-side request forgery (SSRF). Crawling behavior should also comply with the target site’s access rules.
2.5 Content Safety Validation
Safety validation covers both the input and output sides. The input side blocks policy-violating questions and prompt injection; the output side filters non-compliant content. When a rule is triggered, the system should return a unified refusal identifier, letting the frontend display a friendly message rather than exposing internal moderation rules.
3. Two Integration Architectures
3.1 Client-Side Orchestration
The application service maintains the tool list itself. In each conversation turn, the application forwards the tool-call requests output by the model to the corresponding services, then feeds the execution results back to the model. This approach offers strong control and makes it easy to customize caching, rate limiting, and auditing, but it requires more code — you must handle call ordering, error retries, and context bloat yourself.
Pseudo-code example:
messages = [user message]for turn in range(max_turns):resp = model.generate(messages, tools=tool_list)if resp contains no tool calls:return resp.textfor call in resp.tool_calls:result = tool_executor.run(call.name, call.args)messages.append(tool_result(call.id, result))return fallback_answer(messages)
3.2 Server-Side Orchestration
The platform handles tool-call decisions, execution, and context assembly; the application only declares which capabilities to enable. Development cost is low, making this suitable for rapid launches. The trade-off is limited ability to intervene in intermediate steps, and cost statistics and log granularity depend on the dimensions the platform provides.
3.3 Selection Recommendations
- Prototype validation and small-to-medium team projects: prefer server-side orchestration to shorten time to launch.
- Systems with strict requirements on data flow, auditing, and caching: use client-side orchestration to control where data goes at every step.
- Hybrid approach: delegate general-purpose capabilities to the server side, while implementing sensitive or customized capabilities in your own services.
4. Context Management and Citation Traceability
Context quality directly determines answer quality. We recommend focusing on four areas:
- Unified message structure: user input, tool calls, tool results, and model answers each carry roles and identifiers for easier troubleshooting.
- Truncation strategy: when retrieval results exceed the budget, retain content by relevance, prioritizing titles, snippets, and key paragraphs.
- Citation format: assign a number to each source, mark answers with these numbers, and render them as clickable links on the frontend.
- Timeliness handling: keep publication times in results and prompt the model to down-weight outdated information.
Source data can be stored using a structure like the following:
source = {"id": 1,"title": "Article title","url": "https://example.com/article","published_at": "2026-11-01","snippet": "A snippet relevant to the question"}
5. Cost, Latency, and Stability
Call costs typically consist of two parts: the model is billed by tokens, and external tools are often billed by call count or usage volume. Refer to the pricing of your chosen services for specifics. When estimating costs, track the following metrics separately:
- The average number of tool calls per conversation;
- The average length of content returned per tool call, since it converts into model input tokens;
- The average number of pages or images per file.
At the policy level, three measures help: keep high-cost capabilities off by default, enabling them explicitly via user action or rules; apply short-term caching for identical queries; and set timeout limits and circuit-breaker thresholds for each tool category.
Stability matters just as much. The failure of a single tool should not cause the entire conversation turn to fail. A more robust approach is to feed the error message back to the model as a tool result, letting the model tell the user which information could not be retrieved and explaining the limitations of the answer.
6. Security and Compliance in Practice
Applications serving the public need to implement the following:
- Enable input and output safety validation and retain audit logs;
- Establish a content safety review mechanism, with manual review or periodic sampling of generated content;
- Complete service registration or filing as required by generative AI regulations in your operating regions;
- Add compliant AI-generated labels to generated text, images, audio, and video;
- Follow the data minimization principle, define retention periods for user files, and keep unrelated data out of long-term storage.
Additionally, both web page content and user-uploaded files are untrusted data. The system prompt should explicitly state that such content serves only as reference material and that the model must not execute any instructions contained within it. This is a fundamental measure against prompt injection.
7. Implementation Checklist
- Define scenarios: distinguish which questions require retrieval and which can be answered with the model’s own knowledge.
- Choose an orchestration approach and build a minimum viable version first.
- Set timeouts, retry counts, call limits, and degradation strategies for each capability.
- Establish a citation format and display sources on the frontend.
- Integrate safety validation and audit logging.
- Run regression evaluations with a real question set, focusing on answer accuracy, citation correctness, average latency, and per-request cost.
