Building an LLM AI Assistant Gateway from Scratch: Cloud Deployment and IM Integration
This article explains how to build an LLM-based AI assistant gateway that connects heterogeneous entry points such as enterprise IM tools, internal systems, and HTTP clients to model services. It covers a four-layer architecture, deployment options, security baseline, model abstraction, long-term memory, multi-agent orchestration, skill execution, IM callback handling, observability, and operational controls. Readers will learn how to make model access controllable, auditable, replaceable, and cost-aware for long-term team use.
1. The Starting Point: Why Build Your Own AI Assistant Gateway?
Large language models have clear capability boundaries, but they remain inconvenient in real workflows: capabilities are locked inside separate chat windows, teams cannot accumulate shared context, the same information must be manually copied across tools, and repetitive questions consume human effort without an auditable processing chain.
An AI assistant gateway is essentially a controllable middle layer between “model services” and “the entry points people use every day.” It is responsible for four things:
- Access adaptation: Unify heterogeneous entry points such as enterprise collaboration platforms, instant messaging tools, internal systems, and HTTP clients into one internal session protocol.
- Capability orchestration: Handle session management, model routing, tool/skill scheduling, memory read/write, and multi-agent collaboration.
- Governance and observability: Provide unified authentication, rate limiting, content auditing, logging, and cost accounting.
- Replaceability: Model vendors, IM platforms, and vector stores can be replaced without locking business logic to a single vendor.
It is worth noting that the gateway itself consumes almost no compute. The real resource consumers are model inference on the model-service side and retrieval, especially if a vector store is self-hosted. This judgment directly affects the configuration choices later.
2. Overall Architecture: Four Layers plus One Cross-Cutting Capability
[Access Layer] Enterprise IM apps / IM bots / internal systems / HTTP & SDKCallback verification · event deduplication · fast ACK[Gateway Layer] Session management · intent and task routing · memory read/write · agent orchestration · skill scheduling · rate limiting and quotas|[Model Layer] General LLM APIs (chat/inference/lightweight models) · embedding service · optional self-hosted small models|[Storage Layer] Session store (relational) · vector store (long-term memory) · object storage (files and artifacts) · cache/queue------------------------------------------------------------[Cross-cutting] Logging · metrics · tracing · alerting · secrets management · audit records
The key design principle is protocol isolation: the message formats of external platforms (event types, encryption/decryption methods, field naming) are translated into a unified internal session object at the access layer. After that, all business logic only recognizes the internal protocol. When replacing or adding an IM platform, the change is limited to one adapter rather than spread across the entire codebase.
3. Deployment Options and Configuration Choices
| Deployment form | Suitable scenarios | Advantages | Costs |
|---|---|---|---|
| Lightweight cloud server | Personal/small-team validation and long-term online service | Public IP, simple callback access, low cost | Requires self-managed operations and backups |
| Container platform | Elastic scaling and multiple replicas | Standardized release and rollback, easy horizontal expansion | Higher learning cost and fixed cost |
| On-premises device / intranet server | Strong compliance scenarios where data must stay inside the intranet | Full data control | Requires intranet penetration or dedicated line to receive callbacks |
Starter configuration recommendation: 2 vCPU, 4 GB RAM, and a system disk of at least 40 GB are enough to run the gateway service, process supervisor, reverse proxy, and one lightweight vector store together. If the same machine also needs to self-host an embedding model or perform local document parsing, 8 GB of RAM or more is recommended. If conversation volume grows enough to require separately deployed queues and databases, the storage layer should be split out. Prices vary significantly by region, duration, and promotions, so always use real-time pricing from the official cloud provider website rather than numbers from old articles.
Region selection depends on two things: network reachability to the target model service and IM platform, and data residency/compliance requirements (some industries require data to remain within the country/region). If the architecture is deployed on general cloud products from providers such as Baidu AI Cloud — for example, cloud servers, containers, and object storage — the considerations are still the common ones: compute, network, storage, logging, and monitoring. The gateway code itself remains cloud-vendor neutral. This is also the practical benefit of the protocol isolation mentioned above.
4. Environment Preparation and Security Baseline (Do Not Skip)
This is the highest-risk part of the whole practice: once the gateway exposes public callback endpoints, it becomes an entry point that can be reached by arbitrarily constructed requests.
- Run with a dedicated account: Create a service account without login privileges to run the process. Never run business processes directly as root.
- Minimize exposure surface: Security groups/firewalls should allow only 22 (limited to specific source IPs), 80, and 443. Databases, vector stores, and caches should listen only on 127.0.0.1 or private network addresses and should never be exposed to the public internet.
- Enforce HTTPS: Callback URLs must be publicly reachable HTTPS endpoints. Use automatic certificate issuance and renewal.
- Verify callbacks: All inbound requests must first be verified with a signature (timestamp + nonce + secret digest). Failed verification requests should be dropped immediately and counted/alerted. Verification requests sent by the platform when a callback is first configured (usually containing challenge-like parameters) must be returned as-is.
- Externalize secrets: Model keys, IM app credentials, and database passwords should be stored in environment variables or a secrets management service. They must never be committed to code repositories or baked into images.
- Process supervision: Use the operating system’s native service unit mechanism to ensure processes restart after crashes.
# Example service unit (adjust paths and commands to the actual deployment)[Unit]Description=AI Assistant GatewayAfter=network-online.target[Service]User=gatewayWorkingDirectory=/opt/ai-gatewayEnvironmentFile=/opt/ai-gateway/.envExecStart=/opt/ai-gateway/run.shRestart=alwaysRestartSec=5NoNewPrivileges=truePrivateTmp=true[Install]WantedBy=multi-user.target
Reverse proxy rules should be explicitly declared rather than left to defaults:
route: /callback/im -> http://127.0.0.1:8080tls: enforce HTTPS; HTTP automatically redirects with 301rate_limit: 20 req/s per IP, burst 40timeout: read 30s / write 10sbody_limit: 2 MB (file-type endpoints can be relaxed with a type whitelist)
5. Model Access Layer: Abstract Away Vendor Differences
Do not call any vendor SDK directly in business code. Define a thin abstraction layer first, then implement adapters:
# Illustrative pseudocode: unified chat interfaceinterface ChatModel {chat(messages, tools = [], stream = false) -> Replyembed(texts) -> Vector[]}class VendorAdapter implements ChatModel {// Handle only: auth headers, field mapping, stream chunking, error-code normalization// Upper layers only see a unified Reply{ text, tool_calls, usage, finish_reason }}
Use configuration-driven model selection instead of hard-coding model names:
model_router:default: { provider: vendor_a, model: "<chat model id>", max_output_tokens: 2048 }routes:- { match: { task: summarize }, target: { provider: vendor_a, model: "<lightweight model id>" } }- { match: { task: code_review }, target: { provider: vendor_b, model: "<reasoning model id>" } }- { match: { task: embed }, target: { provider: vendor_a, model: "<embedding model id>" } }limits:timeout_ms: 60000max_retries: 2 # Retry only on 429/5xx/network errors; exponential backoff + jitterrpm_per_user: 20daily_token_budget: 2000000
Four engineering rules must be followed:
- Timeouts and retries must appear together, and retries should only be used for idempotent read calls. Retries must include backoff and jitter; otherwise vendor-side rate limiting can cause a cascading failure.
- Design fallback paths in advance: When the primary model is unavailable, switch to a backup vendor or a lightweight model and mark degraded capability in the response.
- Prefer streaming: Time to first token has a much greater impact on chat experience than total duration. The gateway should support forwarding chunks as they arrive.
- Make cost observable: Write structured logs for each call, including input/output token counts, latency, and model identifier. Aggregate by user, session, and skill dimension. This is the only basis for later governance.
6. Long-Term Memory: Three-Tier Structure and Write Strategy
“Remembering users” does not mean stuffing all history into the context. It should be layered:
- L1 short-term context: The latest N turns of the current conversation, sent directly into the prompt.
- L2 session summary: Once the threshold is exceeded, history is compressed into a structured summary (user goals, confirmed facts, to-dos, preferences) and updated on a rolling basis.
- L3 long-term memory store: Preferences, project background, and conclusive facts are embedded and retrieved on demand with top-k injection.
# Illustrative rolling summary updateif token_count(history) > threshold:summary = llm.summarize(previous_summary, recent_turns,schema = { goals, confirmed_facts, todos, preferences, open_questions })history = last_k_turns # Keep only the latest N turns of raw textstore.update(session_id, summary) # Overwrite write to prevent unbounded summary growth
Key engineering points:
- Perform PII detection and masking before writing to long-term memory.
- Keep the source session ID and timestamp on each memory entry to support deletion by user (compliance requirement: “the right to be forgotten”).
- Apply a relevance threshold during retrieval. It is better not to inject memory than to inject noise. Low-quality memory is more harmful than no memory.
7. Multi-Agent Collaboration and Custom Skills
The value of multi-agent systems lies in separation of responsibilities, not the number of agents. The recommended pattern is “orchestrator + workers”:
- Orchestrator: Decomposes goals, decides which worker or skill to call, and determines when to finish.
- Workers: One retrieval worker, one computation worker, and one review worker, with isolated contexts.
- Termination conditions: A maximum turn limit (for example, 8 rounds), a maximum token budget, and an explicit completion signal. Any one of these stops the loop and prevents endless self-cycling and cost burn.
- Task passing: Pass only structured results (conclusion + evidence + artifact location), not the entire conversation history.
A skill is a tool. It should be declared with JSON Schema and driven by the model’s function-calling capability:
{"name": "query_order_status","description": "Query the current status of an order and its latest logistics checkpoints by order ID.","parameters": {"type": "object","properties": {"order_id": {"type": "string","pattern": "^[A-Z0-9]{8,20}$"}},"required": ["order_id"]},"annotations": {"read_only": true,"timeout_ms": 3000,"auth_scope": "order:read"}}
Skill execution must follow these rules:
- Parameter whitelist validation: Do not trust any input generated by the model.
- Least privilege: Each skill should be bound to an independent credential and scope.
- Timeout and concurrency limits: Prevent runaway execution.
- Human confirmation for write operations: Irreversible actions such as transfers, sending external emails, or deleting data must first return a confirmation card for a human to click.
Scheduled tasks (daily report summaries, data synchronization) should run in a separate task queue and should not consume the concurrency quota of interactive request paths.
8. Integrating with IM Platforms: Four Critical Details in the Callback Chain
Details vary by platform, but the flow is the same: create a custom app/bot in the open platform → obtain credentials → configure permissions and visibility → configure event subscription and callback URL → publish. The four points that most often cause failures are:
- Fast ACK: Most platforms require a response within seconds and will retry after a timeout. The correct approach is to enqueue the inbound event immediately, return ACK, and process the business asynchronously. Use a “processing” message or streaming update as a placeholder.
- Event deduplication: Use the platform event ID as the idempotency key and write it to cache (TTL: 10 minutes). Drop duplicate deliveries. Without deduplication, the bot will inevitably answer twice.
- Signature verification and encryption/decryption: Implement signature validation and message body decryption according to the platform documentation. Keys should come from a secrets management service. Requests with timestamps outside the tolerance window should be rejected to prevent replay attacks.
- Trigger rules and scope: Use @ mentions in group chats and full triggers in one-to-one chats. Also configure skill whitelists and user-group permissions so that not everyone can invoke high-privilege skills.
# Callback processing main flow (pseudocode)def on_callback(req):if not verify_signature(req): return drop("bad_signature")if not fresh(req.timestamp, 300): return drop("replayed")event = decrypt(req.body)if seen(event.id, ttl=600): return ack() # Idempotencyenqueue(event) # Enqueue immediatelyreturn ack() # Return within seconds to avoid platform retries
9. Integration Testing, Acceptance, and Operations
Acceptance checklist (check each item before going live):
- Callback signature verification failure is rejected.
- Duplicate events are answered only once.
- Timeout fallback works.
- 50 concurrent connections remain stable.
- Per-user rate limiting works.
- Logs contain trace_id and can link one complete Q&A flow.
- Key rotation does not require business restart.
- Deleting memory by user works.
Core metrics: first-token latency P95, request success rate, per-session token cost and per-user daily cost, skill failure rate and timeout rate, rate-limit trigger count, and memory hit rate.
Four cost governance tactics:
- Keep prompts and system instructions concise. Long system prompts are billed repeatedly on every call.
- Compress history and reuse summaries.
- Route tasks to lightweight models based on task level.
- Use semantic caching for high-frequency repeated questions.
In practice, prompts and history are usually the two largest cost items. Optimizing them first gives the most obvious return.
Change management and disaster recovery: Make the gateway stateless and externalize sessions and memory to the storage layer so that multi-replica deployment and rolling releases are possible. Model keys and IM credentials should have rotation plans. Databases and object storage should enable automatic backups and undergo regular recovery drills.
10. Common Pitfalls
- Running the gateway service directly as root and exposing databases to the public internet. This is the most frequent and most dangerous mistake.
- Processing long-running tasks synchronously in the callback chain, causing the platform to time out and push duplicate requests.
- Letting conversation history grow without limits; cost and latency lose control after a few weeks.
- Multi-agent systems without termination conditions fall into “plan → execute → re-plan” loops.
- Skills without input validation allow malformed model-generated parameters to reach internal APIs.
- Using configuration and pricing from old articles for capacity planning, then patching resources after real load testing.
Conclusion
The technical difficulty of this solution is not merely “connecting to a model.” It lies in packaging unstable, uncontrollable, and pay-as-you-go third-party capabilities into a stable, auditable, and cost-predictable internal control plane. Get the architecture layering and security baseline right first, then progressively enhance in this order: single model + single entry point → add memory → add skills → add multi-agent. Every step should be rollbackable. The final result is not a demo, but an AI assistant infrastructure that can support daily team work for a long time.
