Unified Agent Interaction API: Design and Implementation

    This article explains why enterprise AI agents need a unified interaction interface and how to build one. It covers the fragmentation problems caused by connecting general-purpose LLMs, built-in agent capabilities, and custom agents separately, then presents a layered reference architecture, a stateful session model with context management, a standardized tool-calling loop, multi-agent orchestration, and the reliability, security, and cost controls required for production. Readers will find a practical four-step rollout path and a layered testing strategy for validating the design.

    1. Why Agent Integration Needs a Unified Entry Point

    In enterprise agent deployments, the most common engineering challenge is not model capability but a fragmented integration layer. A typical business workflow often needs to connect three types of executors at the same time:

    1. General-purpose LLMs that handle understanding, reasoning, and generation.
    2. Built-in platform agent capabilities, such as retrieval-augmented Q&A, code assistance, and document parsing.
    3. Custom agents built by developers to implement private business rules, approval workflows, and domain-specific tools.

    Maintaining a separate calling protocol for each executor quickly leads to four problems:

    • Inconsistent protocol semantics, with different authentication schemes and error codes.
    • No place to store session state, forcing callers to stitch multi-turn context together themselves.
    • No unified tool description, making it hard to execute the structured intents produced by models reliably.
    • Fragmented observability, so tracing and cost accounting cannot be aligned on the same dimension.

    The core value of a unified interaction interface is to consolidate these differences behind a single RESTful-style entry point: consistent request semantics for text, multimodal, and structured input; server-managed state for multi-turn sessions; and standardized tool declarations for invoking external capabilities. This turns integration cost from something that grows linearly with the number of executors into a one-time onboarding investment.

    2. Four Consistency Problems the Unified Interface Solves

    Abstracted from the original idea, the unified interface addresses consistency at four levels.

    Protocol consistency. Whether the backend is a general-purpose model, a built-in agent, or a custom agent, callers see the same resource model and the same field semantics, so switching backends does not require rewriting business code.

    State consistency. Interaction evolves from stateless completion to stateful sessions. Sessions, turns, and intermediate artifacts are persisted on the server, and callers only need to hold a session identifier to resume context. This reduces bandwidth consumption and provides an idempotency basis for reconnect retries.

    Capability consistency. Tools and functions are declared in a unified manifest. The runtime parses, authorizes, executes, and writes back the call intents produced by the model, so business teams do not need to write glue code for every tool.

    Governance consistency. Quotas, rate limiting, auditing, cost accounting, and content safety policies are enforced at the entry layer instead of being reimplemented across business services.

    3. Reference Architecture: Layered Decoupling

    The system can be divided into five layers that communicate only through explicit contracts.

    1. Access layer: protocol parsing, authentication, tenant identification, rate limiting, and canary routing.
    2. Session layer: session and turn lifecycle management, context window handling, summary compression, and state snapshots.
    3. Orchestration layer: deciding which executor handles a given interaction, plus task decomposition, parallel scheduling, and result merging.
    4. Execution layer: a unified runtime for model inference, built-in capability calls, custom agents, and tool sandboxes.
    5. Governance layer: cross-cutting capabilities for logging, metrics, tracing, cost accounting, and security review.

    The direct benefit of this layering is controlled replacement cost: adding a new executor type only requires registering its capability description in the orchestration layer, while the access and session layers remain unchanged.

    4. Stateful Sessions: Lifecycle and Context Management

    Statefulness is what distinguishes the unified interface from a traditional text completion API. A robust session model includes at least the following elements.

    Identity and ownership. The server generates the session identifier and binds it to the tenant and creator for authentication and quota attribution.

    Turn structure. Each turn records the input, model output, tool call trace, and elapsed time, forming a replayable execution trace.

    Context strategy. When history exceeds the window, use “rolling summary plus retention of key fragments” to compress it rather than simple truncation, so early constraints such as output format and compliance requirements are not lost in long conversations.

    Persistence and recovery. State snapshots are written to a low-latency key-value store or a document database, while long-term archives can be pushed down to object storage. Callers can resume from any instance using the session identifier, which naturally supports horizontal scaling and failover.

    An illustrative request structure is shown below (the fields are generic examples, not a specific platform API):

    1. {
    2. "session_id": "s-7f3a...",
    3. "input": { "text": "Help me review the risk points in this contract clause" },
    4. "tools": ["document_parse", "rule_check"],
    5. "options": { "stream": true, "max_turns": 6 }
    6. }

    5. Tool Calling and Multi-Agent Orchestration

    Tool calling should be treated as a closed loop of declaration, decision, execution, and write-back. The tool manifest needs to include the name, purpose description, parameter schema, and permission scope. After the model outputs a structured call intent, the runtime validates parameters and permissions, then executes in a sandbox. Results are written back into the session context in a standard structure for the next reasoning turn. The sandbox should restrict network egress, file system access, and execution duration to prevent untrusted code from affecting the main workflow.

    For multi-agent collaboration, a “controller plus specialists” pattern is recommended: the controller agent handles task decomposition and result arbitration, while each specialist agent is bound to a clear toolset and knowledge domain. Scheduling should distinguish serial dependencies from parallel independent subtasks. Parallel results need defined merge rules and conflict resolution strategies to prevent contradictory conclusions from both being accepted.

    6. Reliability, Security, and Cost Control

    Idempotency and retries. Assign an idempotency key to every interaction turn so that timeout retries do not produce duplicate side effects; tool execution also needs idempotency design.

    Streaming and backpressure. Streaming improves time to first token, but heartbeat and stream-resume semantics must be defined, and consumers must implement backpressure so slow clients do not overwhelm the gateway.

    Security boundaries. Bidirectional content moderation, prompt injection protection, least-privilege tools, sensitive data masking, and transport encryption are all required. Audit logs should record who triggered which tool call, when, and with what permissions.

    Cost control. Context compression, cache reuse, selecting model sizes per scenario, and setting turn limits for long-running tasks move cost constraints into the orchestration strategy rather than assigning blame after the fact.

    7. Rollout Path and Validation

    A four-step rollout is recommended: first, connect a single executor to validate the access and session layers, including the protocol and authentication; then add the tool-calling loop with sandboxing and auditing; next, introduce multi-agent orchestration and parallel merging; finally, complete canary release, rate limiting, and the cost dashboard.

    Validation should use a layered test suite: protocol compatibility cases covering fields and error codes; session cases covering very long conversations, reconnect recovery, and concurrent resumption; tool cases covering invalid parameters, permission denials, and timeouts; and orchestration cases covering task decomposition correctness and conflict resolution. If deployed on Baidu AI Cloud, the architecture can be supported with general cloud products: a container platform for the stateless gateway, a message queue for peak shaving, logging and monitoring services for observability, and object storage for long-term archives, without binding to any proprietary interface.