API Responses Worse Than the Web UI? Troubleshoot the Configuration Step by Step
API and web interfaces may use different models, system instructions, tools, sampling settings, and conversation history. This guide explains how to align comparison conditions, provide date and client context, preserve multi-turn history, and correctly define and handle tool calls when building an API-based application.
1. Align the Comparison Conditions First
Before comparing an API with a web interface, make sure the variables on both sides are consistent. Even within the same model family, different tiers and versions may be available. A web product may also add provider-specific system instructions, tools, and safety policies that are not publicly disclosed. As a result, comparing one API response with one web response often leads to unreliable conclusions.
For a meaningful comparison, keep the following variables fixed:
- Model name and version
- System message content
- Whether tools are enabled
- Sampling parameters such as
temperature - The test question and conversation context
Record these settings and change only one variable at a time. This makes it possible to determine which setting actually affects the result.
2. Add the System Message and Current Date
A large language model does not inherently know what date it is, which language the user prefers, or what type of client is being used. The calling application must provide this information through a system message.
Without a system message, the model may produce a cautious, generic response, and its tone may default to something resembling a templated customer-service reply.
Many providers’ web products may automatically add similar context before a conversation begins, but the exact content is usually not public. Therefore, it should not be assumed that every web interface works this way. As an API user, you can provide relevant context yourself, including:
- The current date and day of the week
- The time zone
- The client type
- The interface language
- Any necessary role definition
Only coarse-grained location information, such as a city or time zone, should be provided when needed. Precise location data is sensitive personal information and is generally unnecessary for the model.
Here is an example request structure. Replace the model name and date with the values used in your application:
{"model": "<model name provided by the platform>","messages": [{"role": "system","content": "You are a helpful assistant. Current date: {today's date}, {day of the week}. Time zone: {time zone}. The user speaks Chinese, and the client is a web application."},{"role": "user","content": "Please summarize the key points from these meeting notes."}]}
Generate this system message again for every request. If the date is hard-coded in the application, the model will eventually respond based on an outdated date.
3. Preserve Multi-Turn Conversation History
Interfaces such as /chat/completions use a stateless request-and-response model. The server does not automatically retain the previous conversation. The client must send the relevant message history with every request.
In practice, maintain a message list for each conversation. This list can be stored in memory, Redis, or a database. After each turn, append the model’s response exactly as returned.
For non-streaming requests, use choices[0].message. For streaming requests, concatenate all delta.content fragments into the complete response before appending it to the history.
A simplified implementation looks like this:
history = load_history(session_id)history.append({"role": "user", "content": user_text})reply = call_model(messages=[system_msg] + history)history.append({"role": "assistant", "content": reply})save_history(session_id, history)
Conversation history cannot grow indefinitely. It consumes the context window and increases both cost and latency. A common approach is to retain the most recent turns in full, compress earlier content into a summary, and place that summary near the beginning of the message list. The appropriate number of retained turns should be determined through testing for the specific application.
Conversation history also creates privacy and security risks. It may contain personal information, credentials, contracts, or internal data. When handling history:
- Redact sensitive fields that are unrelated to the current request before sending them.
- Encrypt stored data, define a clear retention period, and delete it when that period expires.
- Do not log complete request bodies. Log only information such as the session ID, model version, duration, and error code.
- Apply least-privilege access controls.
4. Declare and Return Tools Correctly
Some developers assume that adding a tools field to a request automatically enables web browsing or document parsing. This assumption is unreliable.
The tools field only tells the model which functions are available and what parameter format they use. The actual work—such as searching, reading a web page, or parsing a file—is normally performed by a platform-provided tool or by code in the calling application.
Whether a tool works depends on factors such as platform support, enabled permissions, and quota limits. Always refer to the relevant platform documentation.
For example, an application that implements its own web-page retrieval function might declare it as follows:
{"tools": [{"type": "function","function": {"name": "fetch_url","description": "Retrieve the main content of a specified web page to answer questions that require up-to-date information","parameters": {"type": "object","properties": {"url": {"type": "string", "description": "The complete URL to retrieve"}},"required": ["url"]}}}]}
When the model decides to call a tool, it does not return the final answer immediately. Instead, it returns a tool-call request containing a call ID, function name, and arguments. The calling application must then:
- Parse the arguments, execute the corresponding function, and obtain the result.
- Append the assistant message containing
tool_callstomessages. - Append a message with
roleset totool. Itstool_call_idmust match the ID in the request, and itscontentmust contain the tool execution result. - Call the model again so it can generate a final response based on the tool result.
The process can be represented as follows:
resp = call_model(messages=messages, tools=tools)while resp.tool_calls:messages.append(resp.message) # Assistant message containing tool_callsfor call in resp.tool_calls:result = run_tool(call.name, call.arguments)messages.append({"role": "tool","tool_call_id": call.id,"content": result,})resp = call_model(messages=messages, tools=tools)final_answer = resp.message.content
This example omits several protections that are required in a production implementation.
