Server-Side Tool Calling for LLMs: Engineering Practices and Conversation State Management
A practical guide to moving LLM tool calling—web search, page reading, multimodal parsing, and voice—from the client to the server. This article covers the tool-calling loop, why full conversation state must be replayed across turns, how to handle server-side takeover of sampling parameters, protocol compatibility at the gateway, observability, cost control, and a deployment checklist.
1. Background: Moving Tool Calling from Client to Server
When building conversational applications with web access, developers traditionally maintain the entire pipeline themselves: decide whether retrieval is needed, call the search API, fetch candidate pages, extract body text, truncate it, splice it into the context, and hand the results to the model for answer generation. Every step can fail—search timeouts, anti-scraping measures, poor text extraction quality, or context length limits.
The server-managed approach delegates these steps to the platform. The client only submits the user’s question and a set of tool declarations, while the server decides whether to call tools, how many rounds to run, and in what order, then returns the results along with source information. This reduces the client’s burden, but it also moves critical behavior into the server, so developers need to re-examine where the system boundaries now lie.
2. The Basic Tool-Calling Loop
Regardless of who executes the tools, model-driven tool calling follows the same loop:
- The model emits a tool-call request based on the current context, including the tool name and structured parameters.
- The executor runs the tool and produces a structured result.
- The result is appended to the message history, and the model continues reasoning over the updated context.
- The model or the loop controller decides whether to terminate; otherwise it returns to step 1.
Understanding this loop matters because it determines three things: who executes the tools, where the loop terminates, and where intermediate artifacts are stored. When execution happens server-side, the loop completes on the server, and the client only sees the final output plus structured content blocks emitted along the way.
Here is a sketch: the client only declares capabilities, and the loop is handled by the server. Tool type names are placeholders—refer to your platform’s documentation for the actual values:
request = {"model": "<model name>","messages": history + [{"role": "user", "content": question}],"tools": [{"type": "<built-in web search tool>"},{"type": "<built-in web page reading tool>"},],"stream": True,}
3. Map Out the Capability Boundaries First
Managed capabilities generally fall into four categories:
- Web search: generates query terms from the question and returns web page summaries and links.
- Page reading: given a URL, extracts body text and metadata.
- Multimodal parsing: handles text and layout information in documents and images.
- Voice capabilities: speech-to-text and text-to-speech.
When selecting capabilities, verify a few details: whether reverse image search is supported, whether there is a cap on tool-call rounds per conversation, how timeouts and retries for page reading are handled, and whether returned results include source citations. Some capabilities may only be available for specific model versions, so cross-check the model list against the capability matrix. Documentation is only a starting point—test the critical paths in a staging environment before going to production.
4. Full State Must Be Replayed in Multi-Turn Reasoning
Models with reasoning enabled typically emit thinking content, and managed tools also produce retrieval records and fetched page results. In the next turn, this content often remains essential context for the model. If the client or an intermediate gateway keeps only the final text and drops the thinking blocks and tool result blocks, the request will still succeed—but the model loses its memory of what was retrieved. Typical symptoms: repeated searches, contradictory conclusions across turns, or fabricated sources that do not exist.
The difficulty with this class of bugs is that they raise no errors, so prevention must be proactive:
- Store messages in the platform’s original structure; do not save only the text field.
- Have the proxy layer pass content through without filtering unknown block types.
- Write regression tests: in a second turn, ask about specific information retrieved in the first turn and verify the model answers correctly.
Example code:
def append_turn(history, assistant_message):# Preserve full content blocks, including reasoning and tool result# blocks, and replay them in their original order.history.append({"role": "assistant","content": assistant_message["content"], # save as-is, no field trimming})return history
5. When Sampling Parameters Are Taken Over
When features such as tool calling, automatic title generation, or stricter safety levels are enabled, the server may take over the sampling process, and client-supplied parameters like temperature and top-p are ignored. Rules differ across platforms—consult the official documentation before integrating rather than assuming parameters will take effect.
Mitigations include:
- Treat “sampling parameters are honored” as a hypothesis to verify, not a default assumption.
- For tasks that depend on fixed output formats, such as structured extraction, separately evaluate stability with tools enabled.
- Run the same batch of inputs with tools on and off, record output differences, and then decide on your configuration strategy.
6. Integration Layer and Protocol Compatibility
Enterprise systems often run multiple client SDKs, each with different message formats. Perform protocol adaptation at the gateway layer and normalize on an internal data model. The adaptation layer must meet three requirements:
- Pass unknown content types through instead of erroring out or dropping them.
- Record version information from raw responses for later traceability.
- Handle streaming events separately from the final structure, so intermediate fragments are not mistakenly stored as complete results.
7. Observability and Cost Control
With managed tool calling, a single user request can trigger multiple searches and page fetches, amplifying both latency and cost. Establish metrics such as:
- Tool-call count per request and its distribution;
- Latency and failure rates for search and page reading;
- Average turns and token consumption per session;
- Number of cited sources and their validity.
Also set protective measures: session-level timeouts, and tool-call count limits where the platform supports them. User-submitted URLs require security validation—restrict protocols and block access to internal network addresses to mitigate server-side request forgery (SSRF) and similar risks.
8. Deployment Checklist
- Map each tool capability to its supported model versions.
- Confirm the required protocol formats and streaming events are supported.
- Store and replay all content blocks exactly as received.
- Verify whether sampling parameters take effect, and build comparison tests.
- Apply security validation to external URLs and restrict access scope.
- Set up monitoring and alerting for call counts, latency, and cost.
- Write multi-turn regression tests for key business scenarios.
