Connecting AI Agents to LLMs: Configuration Checks and Session Troubleshooting Guide
This article explains how to verify LLM integration configuration for AI agents and troubleshoot session-related problems. It categorizes connection, protocol, and state failures; describes a five-layer integration model and key observability fields; provides a pre-launch configuration checklist; and outlines a layered approach to diagnosing message-order errors, beginning with a minimal request.
1. Background and Three Types of Failures
An AI agent is essentially a loop. It receives user input, calls an LLM to generate a decision, and then either executes tool calls or returns text based on that decision. If model access fails, the entire loop stops.
Integration problems in real projects generally fall into three categories, each with a different troubleshooting approach:
| Category | Typical symptoms | Possible causes | Verification method | Action |
|---|---|---|---|---|
| Connection | Authentication failure, request timeout | Incorrect URL, invalid key, network policy blocking | Use curl to connect directly to the service address and observe the status code and latency | Verify the base path and key; check the egress network |
| Protocol | Parameter validation errors | Request body fields or roles do not match server expectations | Send a minimal request and compare it item by item with the provider documentation | Adjust fields, roles, or tool definition format |
| State | Conversation cannot continue mid-way | Historical messages out of order; tool results missing or orphaned | Export the session message sequence and check roles and pairing relationships | Back up the session and then repair or clean abnormal messages |
This article focuses on protocol and state issues. The diagnostic rule is simple: if a new session still fails, rule out protocol and configuration issues first; if a new session works normally, the problem is likely in the stored content of the old session.
2. Integration Layers and Configuration Checklist
A maintainable integration setup usually consists of five layers:
- Agent runtime: organizes prompts, manages session history, and schedules tool calls.
- Model provider configuration: records the service address, authentication information, model ID, and capability declarations.
- Gateway or proxy layer (optional): handles authentication, rate limiting, logging, and failover in a unified way.
- Model service: the remote or local service that performs inference.
- Session storage: persists message history and is a high-risk area for state-related issues.
The value of layering is isolation. If the problem disappears after bypassing the gateway and connecting directly to the model service, the fault is in the gateway’s protocol conversion stage. Each layer should produce observable information. At minimum, record the following fields:
- request_id: a unique identifier for a single request, used to correlate logs across the agent, gateway, and server.
- session_id and message_seq: the session identifier and message sequence number, used to replay conversations.
- role and tool_call_id: the message role and tool call ID, used to check pairing relationships.
- Gateway forwarding headers: request headers added by the gateway during forwarding, used to confirm the traffic path.
- status_code and latency_ms: response status code and latency, used to distinguish authentication failures, parameter validation errors, and timeouts.
Before launch, use the following checklist to confirm each item:
- baseUrl matches the provider documentation and includes the required version path.
- The key is read from environment variables or a secret service, with no plaintext in configuration files or logs.
- The model ID, context window, and maximum output length have been checked against the provider documentation.
- A minimal request can return a normal result.
- Logs contain the observability fields listed above.
- Message sequence numbers in session storage are continuous, with no duplicate or orphaned records.
3. Layered Troubleshooting for Message Order Errors
Message-order errors occur when the server rejects a request because the message sequence does not meet protocol requirements. Common causes include:
- An assistant message declares a tool call, but no corresponding tool result follows.
- The tool_call_id of a tool result does not match the preceding call ID.
- Empty-content messages are mixed into the history, or after truncation the first non-system message is not a valid role.
- Session storage replays messages out of order, for example by reading them by write time rather than by message sequence number.
- Some services require reasoning content to be returned in subsequent rounds after a tool call; if it is missing, validation fails.
The exact error text varies by provider. Troubleshoot layer by layer in the following order, confirming the expected result at each step before moving to the next.
Step 1: Minimal Request
Include only one user message, with no history and no tool definitions:
{"model": "<model-id>","messages": [{"role": "user", "content": "ping"}],"max_tokens": 16}
Expected result: the request succeeds.
