Server-Side Tool Calling and Conversation State Management for Large Language Model Services

    Learn how server-side tool execution changes retrieval workflows, how models combine reasoning with search and page fetching, and why preserving every structured response block is essential for reliable multi-turn conversations. This guide covers protocol selection, context handling, security, privacy, cost, latency, implementation patterns, and troubleshooting.

    1. Background: Moving Tool Calling from the Client to the Server

    In the past, developers typically had to build a complete retrieval pipeline to help a model answer questions that required up-to-date information. A common workflow involved calling a search API, fetching web pages, extracting and cleaning their content, truncating long text, ranking the results, and finally inserting the selected material into the prompt.

    Every step could fail. Changes to a page’s structure might break content extraction, while overly long pages could consume too much of the model’s context window.

    Increasingly, model services are handling these steps on the server. The client submits the question and configuration, while the model decides whether to search, how many searches to perform, and which links to open. This approach can be described as server-side tool execution.

    It introduces three major changes:

    1. Client-side integration becomes much simpler because applications no longer need to maintain crawling and parsing logic.
    2. Reasoning and retrieval can be interleaved, allowing the model to decide what to investigate next based on intermediate results.
    3. Source information is returned with the answer, allowing the frontend to display citations directly.

    2. How Server-Side Tools Work

    A tool-enabled request is typically implemented as a loop:

    1. The model generates an intention to call a tool, such as searching for a keyword or reading a URL.
    2. The server executes the tool and writes the result into the conversation as structured blocks.
    3. The model reads the result and determines whether the available information is sufficient.
    4. If the information is insufficient, the model calls another tool. If the information is sufficient, it generates the final answer and includes the sources.

    The key step is the model’s decision in step three. It does not necessarily perform exactly one search. Instead, it decides whether additional queries are needed based on what it has already found.

    For example, when comparing two entities, the model might first search for general background information and then open an official page to find specific figures. If a page does not contain a required data point, it might search within the page for a relevant keyword or retry the search using different wording.

    The loop must have an upper bound. Without a limit on the number of calls or total execution time, the model may repeat similar searches, increasing both latency and cost. In production systems, conservative limits are usually selected based on task complexity.

    When the limit is reached, the model should answer using the information already available and clearly indicate which parts lack complete supporting evidence.

    3. Interleaving Reasoning and Retrieval

    When reasoning is enabled, the model can make tool calls during its reasoning process. This is useful for multi-hop questions where the final answer depends on connecting several facts.

    When reasoning is disabled, the model will often complete the necessary retrieval within a single round and then respond directly. This generally provides lower latency and is suitable for factual questions, news summaries, and simple verification tasks.

    The choice should depend on the nature of the question. Enabling deep reasoning for a straightforward query may only waste time and computational resources.

    4. Choosing an Integration Protocol

    Model APIs generally fall into three broad categories:

    • Message-list-based APIs
    • Response-object-based APIs
    • APIs compatible with chat-completion formats

    The names and structures used for tool-call blocks vary between protocols, but the core requirement is the same: every tool call and every corresponding tool result must be fully recorded in the conversation history.

    The following principles can help guide protocol selection:

    • If an existing system already uses a particular protocol, continue using it where possible to reduce integration work.
    • If the application must support multiple model providers, perform protocol conversion at the gateway layer instead of adding provider-specific branches throughout the business logic.
    • The conversion layer should preserve the original structure. Fields should not be discarded simply to simplify the representation.

    5. Returning Context in Multi-Turn Conversations

    This is one of the most commonly overlooked aspects of server-side tool execution. A server response may contain reasoning segments, search requests, fetched content, and final text. These elements are usually stored as different types of blocks.

    When the next turn begins, the client must put these blocks back into the conversation history in their original form.

    The reason is that the model relies on historical tool results to understand the information it has already obtained. If an intermediate gateway keeps only the final text and discards the retrieval result blocks, the request may still succeed and appear normal. However, the model has lost the web content it previously read. Later answers may therefore be based on incomplete understanding, or the model may repeat the same retrieval operations.

    This type of issue is difficult to detect because it does not necessarily produce an error. It usually appears as a gradual decline in answer quality.

    Gateways and frontend systems should therefore follow these rules when processing conversation history:

    • Do not filter or rewrite unknown block types, even if the current interface does not display them.
    • Do not reorder blocks. Their sequence represents the order of the reasoning and tool-execution chain.
    • Store the complete original payload for each conversation, and render it separately in the presentation layer.
    • Validate imported conversation histories, while preserving the structure of tool results whenever possible.

    6. Engineering Considerations

    Security. Web content is untrusted data. Pages may contain text designed to change the model’s behavior, such as prompt injection attempts. The system prompt should explicitly instruct the model to treat tool results as reference material rather than executable instructions. Suspicious content should also be marked where appropriate.

    Privacy. User-provided URLs and search queries may contain sensitive information. Sensitive fields should be redacted in logs, and retention periods should be defined according to applicable compliance requirements.

    Cost and latency. Record the number of tool calls, total execution time, and input and output sizes for every request. After establishing a baseline, adjust the call limits and timeout settings based on actual usage.

    Presentation. Citations should be clickable and should include the retrieval time. If a source cannot be accessed, the answer should say so instead of silently omitting it.

    7. Example Implementation

    The following pseudocode illustrates how a client can maintain conversation history. The function names are illustrative and do not correspond to a specific API.

    1. history = []
    2. def ask(user_text):
    3. history.append({"role": "user", "content": user_text})
    4. response = call_model(messages=history, tools_enabled=True)
    5. # Save all content blocks returned by the server, including reasoning,
    6. # retrieval requests, and fetched results.
    7. history.append({"role": "assistant", "content": response.blocks})
    8. return extract_final_text(response.blocks)

    The critical line is the one that saves response.blocks. If the client stores only the result of extract_final_text(...), the context loss described above will occur.

    8. Testing and Troubleshooting

    Before deployment, use a fixed set of questions for regression testing. Each question should have predefined points to verify. Check whether the answer covers those points and confirm that the cited sources actually exist.

    When problems occur during a multi-turn conversation, investigate them in the following order:

    1. Check whether the history still contains the tool-result blocks.
    2. Compare the messages entering and leaving the gateway to confirm that no blocks were filtered out.
    3. Check whether the order of the blocks changed.
    4. Verify that conversation storage and retrieval preserve the complete history.

    9. Conclusion

    Server-side tool execution moves the complexity of retrieval, page fetching, and follow-up searches from the client to the model service, making client applications lighter.

    This division of responsibilities also requires developers to understand the internal execution loop, especially the importance of complete context propagation. As long as the conversation history remains intact, the model can continue reasoning from the material it has already read.

    Once structured content is lost in an intermediate layer, the problem may appear quietly as declining answer quality rather than as an explicit error.