Mastering LLM API Integration with Multimodal Input and Tool Calling: A Practical Guide

    This article explains how to build conversational LLM applications that handle multimodal input and tool calling by moving orchestration to the server side. It groups capabilities into conversational enhancement, file and image input, and speech, then compares client-side and server-side tool orchestration. It also covers the design of web search and page reading, document parsing pipelines, and voice pipeline optimization, with attention to security and compliance.

    1. Background: From Single-Turn Q&A to Full Conversational Capability

    LLM APIs initially offered only text input and text output. To build an experience close to a mature conversational product, developers had to handle web search, document parsing, web page fetching, and speech conversion on their own. Each step required managing call order, context assembly, error retries, and result citations, and the engineering effort grew quickly as features were added.

    A pattern that is taking shape is to move these capabilities to the server side. Developers declare the capabilities they need through parameters or tool switches. The server decides when to search, how to assemble context, and when to read links. The application layer can focus on business logic and presentation. The trade-off is that developers must understand the boundaries and controllability of server-side orchestration. This article follows that pattern and explains capability layers, integration methods, security and compliance, and key points for engineering implementation.

    2. Capability Layers: Conversational Enhancement, Input Extension, and Basic Speech

    Based on how tightly each capability couples to the model, these capabilities fall into three layers.

    The first layer is conversational enhancement. It includes web search, information retrieval during multi-step reasoning, and web page reading. These capabilities directly affect the timeliness and reliability of answers.

    The second layer is input extension, which covers parsing files such as PDFs, Office documents, and images. Files first go through plain text extraction or mixed text-and-image parsing. The results are then added to the context as text.

    The third layer is basic speech capabilities, including automatic speech recognition (ASR), text-to-speech (TTS), and optical character recognition (OCR). These can be called independently or combined into voice conversation or image-based Q&A pipelines.

    Each layer fails differently, so monitoring and fallback strategies should be designed separately for each one. A search timeout usually affects only timeliness. A file parsing failure, however, can leave the model without key information, so the system should clearly inform the user.

    3. Tool Calling and Server-Side Orchestration

    The core of tool calling is letting the model request external actions during generation, then feeding the results back to the model. In the traditional approach, the client runs the loop. The model returns a tool call request, the client executes it, appends the results to the message list, and sends the request again. Server-side orchestration moves this loop inside the platform.

    The two approaches involve the following trade-offs.

    • Client-side orchestration gives fine-grained control and makes it easy to audit each external call. It suits scenarios with strict requirements on data flow. The cost is higher implementation complexity, because the team must manage context length and retry logic.
    • Server-side orchestration is faster to integrate and provides unified context assembly. It suits general conversational scenarios. The cost is that observability depends on the logs the platform provides, so teams must confirm which intermediate results are visible to the application.

    When choosing an approach, first ask whether tool call results contain sensitive data and whether the audit requirements call for keeping a record of each call. If the answer is yes, use client-side orchestration even though integration costs more. Alternatively, add a separate logging layer on top of server-side orchestration.

    The following illustrative code uses placeholder implementations for the tools and the model client:

    1. def answer(user_input: str, history: list[dict]) -> str:
    2. tools = [web_search_tool, url_reader_tool, file_parser_tool]
    3. messages = history + [{"role": "user", "content": user_input}]
    4. response = model_client.generate(messages=messages, tools=tools)
    5. # Loop over tool calls until the model returns a final answer
    6. while response.has_tool_calls():
    7. results = [run_tool(call) for call in response.tool_calls]
    8. messages += response.as_messages() + results
    9. response = model_client.generate(messages=messages, tools=tools)
    10. return response.text

    In production, add a maximum number of loop iterations. Without a limit, the model could call tools repeatedly and drive up costs.

    4. Web Search and Page Reading

    Web search adds information beyond the model’s training data. Three points matter in the design.

    The first is the trigger condition. The model can start a search based on the question, but for questions that are not time-sensitive, excessive searching adds latency and cost. The application layer can use system instructions or tool descriptions to specify when retrieval should happen.

    The second is result citation. Answers should indicate their information sources so users can verify them. A citation structure with three fields (title, link, and excerpt) works well. The display should distinguish the model’s inferences from facts stated in the source.

    The third is the boundary of page reading. Automatically fetching links in user messages means the application actively accesses external addresses. Restrict the protocols and address ranges the network layer allows to prevent internal network probing. Also set timeouts and content size limits.

    Web pages often contain navigation bars, ads, and leftover scripts. Inserting them directly into the context wastes many tokens. A common approach is to extract the main text first, keep headings, paragraphs, and tables, and then truncate by length.

    5. Parsing File and Image Inputs

    File parsing generally involves four steps: type identification, content extraction, structuring, and context injection.

    1. Type identification: Determine the format from the file header rather than the file extension. This keeps disguised files from reaching the parser.
    2. Content extraction: Extract text directly from text-based documents. Scanned documents and images require OCR. Documents with charts require layout analysis to preserve the position of text relative to graphics.
    3. Structuring: Convert heading hierarchies and tables into Markdown or plain-text tables. Remove repeated content such as headers and footers.
    4. Context injection: Calculate the token count of the parsed result. If it exceeds the budget, use segmented summarization or retrieve only the passages relevant to the question.

    For long documents, injecting the entire document often performs poorly. A better approach is to split the content into semantic segments, build a vector index, and then retrieve the segments relevant to the question. This is a general retrieval-augmented generation (RAG) approach. It can reuse common infrastructure such as object storage and vector search.

    6. Combined Speech Pipeline

    A voice conversation typically has three stages. Speech recognition converts audio to text, the model generates a response, and speech synthesis converts the response to audio. Each stage adds latency, and total latency is the sum of all three. Focus on the time to first packet for each stage.

    You can apply the following optimizations:

    • Streaming recognition: Recognize speech as the user talks, which reduces the wait after the user pauses.
    • Sentence-level synthesis: Start synthesizing as soon as the model outputs a sentence, without waiting for the full response.
    • Interruption handling: When the user interrupts playback, stop the current synthesis and discard audio that has not been played.

    Common OCR problems include skewed, blurry, and low-resolution images. Denoising, deskewing, and binarization during preprocessing can noticeably improve recognition accuracy. Before launch, evaluate with samples from real business data rather than relying only on metrics from public datasets.

    7. Security Checks and Compliance Requirements

    When generative AI services are offered to the public