Build a Multi-Platform AI Agent Gateway from Scratch on Ubuntu

    This article explains how to build a multi-platform AI agent gateway from scratch on Ubuntu. It covers a four-layer architecture (access, orchestration, model, and execution), baseline environment setup, a unified internal event model for connecting multiple messaging platforms, adapter-based multi-model integration, isolated execution backends, terminal usability and observability, scheduled automation, and a pre-launch checklist. The goal is to decouple model inference from message delivery and execution so the gateway can evolve into organization-level collaboration infrastructure.

    Build a Multi-Platform AI Agent Gateway from Scratch on Ubuntu

    Why Self-Host an Agent Gateway on Ubuntu

    When enterprises roll out AI assistants internally, the common bottleneck is not a lack of models but the disconnect between models and business channels. Business users communicate through instant messaging tools, email, and collaboration platforms, while model capabilities are often locked inside standalone web pages or clients. As a result, context cannot accumulate and actions cannot form a closed loop. The value of an agent gateway is to decouple model inference from message delivery and execution, then orchestrate them through a single unified instance.

    Ubuntu is chosen as the host environment for three reasons. First, its package management and dependency ecosystem are mature, with clear installation and upgrade paths for components such as Python, Node.js, and container runtimes. Second, it is compatible with mainstream container platforms and virtualization solutions, making later isolation and elastic scaling easier. Third, its command-line toolchain is complete, which suits operations and development scenarios where the terminal is the primary interface. Note that this article is not tied to any specific vendor product; all capabilities described map to general industry technology categories.

    Layered Architecture

    A maintainable agent gateway should be split into four layers:

    • Access layer: Handles multi-platform message sending and receiving plus identity mapping, normalizing messages from heterogeneous platforms into internal standard events.
    • Orchestration layer: Manages session state, context assembly, tool-call decisions, and streaming output control.
    • Model layer: Connects to different inference services through an adapter pattern, enabling “switch by configuration.”
    • Execution layer: Actually runs code and commands, supporting local, container, remote host, and Serverless backends.

    The benefit of this layering is that swapping models does not affect access logic, replacing the execution backend does not affect session orchestration, fault domains stay clear, and canary releases and rollbacks are straightforward.

    Environment Setup and Baseline Constraints

    Starting from scratch on Ubuntu, complete the baseline first:

    1. Create a dedicated non-root user and run all services with least privilege.
    2. Install runtime dependencies (such as a Python virtual environment, the Node.js runtime, and Git).
    3. Install a container runtime for later isolated execution.
    4. Configure the firewall to open only necessary ports, and allow management-plane access only from the internal network.
    5. Use a process supervisor or container orchestration to ensure services auto-start and restart after crashes.

    A sample directory structure is shown below:

    1. agent-gateway/
    2. ├── config/ # Layered config: access, model, execution, scheduling
    3. ├── adapters/ # Platform adapters
    4. ├── orchestrator/ # Session and tool orchestration
    5. ├── executors/ # Local/container/remote execution backends
    6. └── logs/ # Structured logs and audit trail

    Multi-Platform Access: A Unified Event Model Is Key

    Connecting one instance to a dozen or more messaging platforms is not complex engineering-wise; semantic unification is the hard part. Define an internal standard event that includes at least: platform identifier, session identifier, sender identifier, message type, body, attachment references, timestamp, and reply routing. All platform adapters should only perform “protocol translation” and stay out of business logic.

    Identity mapping needs separate design: the same person has different IDs on different platforms, so a unified identity table should establish the mapping to prevent context from crossing between users. For group chats, distinguish “command messages” from “normal discussion,” typically triggered by a prefix or slash command to reduce accidental triggers.

    Multi-Model Access: Hide Differences with Adapters

    The model layer’s core requirement is “switch anytime without code changes.” A practical approach is to abstract a unified interface that takes a message list and tool definitions as input and returns streaming increments plus a termination reason as output. Inference services differ in authentication, context length, and tool-call formats; all these differences are contained inside the adapters.

    For configuration, use a layered structure: a default model, per-session overrides, and per-task-type overrides. For example, long-text summarization uses a long-context model, code generation uses a model with stronger coding ability, and everyday Q&A uses a low-latency model. Switching only requires changing configuration or issuing a command, with no need to restart the whole service.

    Execution Backends and Isolation Strategy

    The execution layer carries the highest risk and must be isolated by default. The recommended priority is: container isolation > remote host > local direct run. The container backend should set resource limits, a read-only root filesystem, no privileged mode, restricted network egress, and persistent mounts for the working directory to balance security and state retention.

    For Serverless-style persistent execution, separate “session state” from “compute instances”: write state to external storage, spin up compute on demand, and reclaim it when idle. This controls cost and avoids wasting resources on long-lived connections.

    Terminal Experience and Observability

    Details of the end-user terminal experience determine whether adoption succeeds: multi-line editing to avoid truncated long commands; slash-command autocomplete to lower the learning curve; real-time token usage monitoring to make cost visible; and streaming tool output to make waiting perceptible. For observability, use unified structured logs that record session ID, model, latency, token consumption, and the tool-call chain for auditing and troubleshooting.

    Scheduled Automation and Protocol Extensions

    Built-in scheduling is the dividing line that moves a gateway from “passive responses” to “proactive service.” In practice, describe task intent in natural language, let the orchestration layer translate it into scheduling configuration, and push results proactively through the message gateway. Every scheduled task must have a timeout, a retry limit, and failure alerts to avoid silent failures.

    Capability extension follows an open-protocol approach: mount external capabilities through a standardized tool-access protocol, such as querying databases, operating dashboards, or calling internal APIs. When integrating tools, apply a tool allowlist and permission tiers to prevent unauthorized operations.

    Pre-Launch Checklist

    • Does it run with least privilege, and is the management plane reachable only from the internal network?
    • Is the execution backend isolated by default, with resource and network restrictions?
    • Can model configuration be hot-swapped, and are keys centrally managed?
    • Are usage monitoring, log auditing, and failure alerts in place?
    • Are scheduled tasks idempotent, retryable, and cancellable?

    Following this path, you can build a logically rigorous, extensible agent gateway on Ubuntu and gradually evolve it into organization-level intelligent collaboration infrastructure.