Building Multi-IM Platform AI Agents: Architecture Design and Deployment Practices
This article explains how to connect one large language model to several instant messaging (IM) platforms. It covers the main integration problems, including inconsistent protocols, authentication and security requirements, and response time limits that clash with slow model inference. It also describes a four-layer architecture, reliability and security practices, and common deployment troubleshooting steps for Docker Compose.
1. Main Challenges of Connecting to Multiple IM Platforms
Connecting a large language model to several IM platforms usually raises three problems.
1. Inconsistent protocols and message formats. Some platforms push messages over WebSocket long connections. Others send messages to a specified address through Webhook callbacks. Message bodies may be JSON or XML. Multimodal messages such as images, voice recordings, and files are also retrieved differently. Some platforms require a separate API call to download the file, while others provide a temporary link directly.
2. Varying authentication and security requirements. Each platform has its own token acquisition process, refresh cycle, and signature algorithm. Enterprise scenarios also require multi-tenant isolation, data compliance, and privacy protection. If a Webhook callback does not verify its signature, anyone can forge message requests.
3. Conflict between response time limits and long-running inference. Generating a reply with a large model can take several seconds or tens of seconds. Most IM platforms expect a response within a short time. If a response times out, the platform may retry the push, so the same message gets processed twice. For this reason, synchronously waiting for the model to return a result rarely runs reliably.
2. General Architecture Design
A common approach divides the system into four layers: the adapter layer, the routing layer, session management, and the model layer. Each layer handles only its own responsibilities, so the underlying model and the upstream channels can be replaced independently.
| Component | Responsibility | Handling on Failure |
|---|---|---|
| Channel adapter | Verifies signatures, parses platform messages and converts them to a unified format, and converts outbound messages back to the platform format | Rejects requests that fail signature verification and logs the source IP address and time |
| Routing layer | Recognizes commands (for example, /help is handled locally) and places natural language messages in a queue | Returns a placeholder notice when the queue is backed up and does not discard messages |
| Session management | Stores context in Redis and links history messages by session_id | When the cache is invalid, restores the most recent conversation turns from the database |
| Model scheduling layer | Calls the model asynchronously and handles streaming output, timeouts, and rate limiting | After a timeout, retries once. If the retry also fails, returns an error message |
The adapter layer converts each platform’s messages into a unified structure, for example:
{"platform": "im_platform_a","session_id": "user_12345_group_67890","msg_type": "text","content": "Summarize today's work for me","message_id": "evt_20261124_0001","timestamp": 1698765432}
In this structure, message_id is the unique identifier of the platform message. The system uses it later for deduplication.
Streaming Output Involves Two Separate Links
Streaming output involves two links. The first link runs from the model to the server, and the model can return text in chunks. The second link runs from the server to the IM platform. Whether the reply can be displayed in chunks depends on the platform. Some platforms allow editing a message after it has been sent. In that case, you can send a placeholder message first and update its content gradually. Platforms that do not support editing require either accumulating content and sending it in segments, or waiting for the complete result and sending it at once. Before integrating, check the target platform’s documentation to confirm what it supports.
Reliability Design
- Timeouts: The IM callback should return an acknowledgment immediately. Model inference runs in a background task, and the result is pushed through the platform’s proactive send interface.
- Retries and idempotency: Use
message_idas the idempotency key. Ignore messages that have already been processed, which prevents duplicate replies when the platform retries. - Rate limiting: Set concurrency limits per tenant and per user, so a single user cannot exhaust the model quota.
Security and Compliance
- Webhook callbacks must verify signatures and timestamps according to the platform’s rules, and they must reject expired requests.
- Store AppSecret, tokens, and other credentials encrypted. Keep keys in environment variables or a key management service, not in the code repository.
- Isolate each tenant’s data, sessions, and knowledge bases by
tenant_id. Add the tenant prefix to Redis key names as well. - Do not log full mobile phone numbers, national ID numbers, or credential contents. If logging these values is necessary, mask them first.
- Configure services under the principle of least privilege. For example, grant only the API permissions needed to send and receive messages.
3. Deployment Prerequisites and Troubleshooting
Containerization makes deployment simpler, but running one command does not guarantee that the service is available. Before deploying, confirm the following:
- Docker and Docker Compose versions meet the project requirements. See the project documentation for the specific versions.
- The
.envfile contains the model API key, platform credentials, and database password. Its file permissions allow only the service user to read it. - The server exposes only the necessary ports. Redis and database ports are not open to the public internet.
- Redis and database data directories are mounted to the host or a cloud disk, so data survives container rebuilds.
- IM platform callback URLs usually must be publicly reachable HTTPS addresses. Configure a domain, a certificate, and a reverse proxy.
- The reverse proxy’s read timeout exceeds the model’s longest response time, and buffering is disabled for streaming responses.
After these conditions are met, run the following commands to start the service:
docker compose up -ddocker compose psdocker compose logs -f --tail=100
ps confirms that all containers are running. logs shows startup errors.
Common faults and where to look:
| Symptom | Likely Cause | Where to Look |
|---|---|---|
| Callback signature verification fails | Wrong key, server clock out of sync, or incorrect order in the signature string | Check the signature rules in the platform documentation, and sync the system clock with NTP |
| Credentials expire frequently | The token was not refreshed on schedule, or the refresh task failed | Check the refresh task logs and confirm the refresh interval setting |
| The same message gets two replies | The platform retried after a timeout, and idempotency handling is missing | Check the message_id deduplication logic |
| A streaming reply disconnects midway | The reverse proxy timeout is too short, or buffering is enabled | Increase proxy_read_timeout and disable proxy_buffering |
| Conversation records are lost after a restart | The data volume is not mounted | Check the volumes configuration in the Compose file |
| Many requests return rate limit errors | Concurrency exceeds the model quota | Adjust the per-tenant concurrency limit, or request a higher quota |
4. Operational Value of a Visual Dashboard
For non-technical staff, a dashboard removes much of the manual work of editing configuration files. Typical features fall into three categories.
- **Channel
