Designing a Unified API for Multimodal Intelligent Applications: Engineering Best Practices

    This article outlines the key engineering considerations for building a unified API layer for multimodal intelligent applications. It covers pre-integration requirements such as authentication, rate limiting, and idempotency, defines a standardized request/response contract, and details the orchestration sequence for tool execution, error handling, and graceful degradation.

    Prerequisites Before Integration

    A unified API can only run stably if several prerequisites are in place. Confirm the following before designing the system:

    • Authentication: Use API keys or short-lived tokens on the server side. Store secrets only in environment variables or a dedicated secret management service—never in code or logs.
    • Rate Limiting: Set per-user and per-application request limits (requests per minute). When a limit is exceeded, return HTTP 429 with a Retry-After response header.
    • Idempotency: Clients must generate a unique idempotency_key for each request. On retries, the server returns the same result to avoid duplicate searches or double billing. It is recommended to retain these records for 24 hours.
    • Versioning: Include an api_version field in the request body. Introduce a new version when tool definitions contain breaking changes, and support the previous version for at least one release cycle.
    • Tool Flags: All tool capabilities are disabled by default and must be explicitly enabled via the application configuration.

    Request and Response Contract for the Unified API

    A single conversational request can include text, file attachments, and tool configurations. Below is an example request:

    1. {
    2. "api_version": "2026-11-01",
    3. "session_id": "sess_8f3a",
    4. "idempotency_key": "req_20261124_0001",
    5. "messages": [
    6. {"role": "user", "content": "Review the risk clauses in the attached contract and search for the latest judicial interpretation of the relevant provisions."}
    7. ],
    8. "attachments": [
    9. {"file_id": "file_c91e", "mime_type": "application/pdf"}
    10. ],
    11. "tools": ["file_parse", "web_search"],
    12. "tool_choice": "auto",
    13. "stream": true,
    14. "options": {
    15. "max_tool_calls": 4,
    16. "max_context_tokens": 32000
    17. }
    18. }

    Field descriptions:

    • tool_choice: Accepts auto, none, or a specific tool name.
    • stream: When true, the server returns incremental text via Server-Sent Events (SSE).
    • options.max_tool_calls: The maximum number of tool calls allowed for a single request. When this limit is reached, the model generates a response based on the results already obtained.

    The response returns the answer, cited sources, and an execution record for each tool call:

    1. {
    2. "trace_id": "tr_4b77d2",
    3. "session_id": "sess_8f3a",
    4. "status": "partial",
    5. "answer": "Clause 7 of the contract carries a high risk regarding liquidated damages. As for the latest judicial interpretation, the web search did not succeed. The following is based on existing knowledge only; we recommend verifying this later.",
    6. "citations": [
    7. {"source_title": "Full text of a judicial interpretation", "url": "https://example.com/interpretation", "published_at": "2026-06-10"}
    8. ],
    9. "tool_calls": [
    10. {"tool_call_id": "tc_01", "name": "file_parse", "status": "succeeded", "duration_ms": 1840},
    11. {"tool_call_id": "tc_02", "name": "web_search", "status": "failed", "error_code": "TOOL_TIMEOUT", "duration_ms": 8000}
    12. ],
    13. "usage": {"input_tokens": 12850, "output_tokens": 920}
    14. }

    The status field can take the following values:

    • completed: All tools succeeded; the answer is complete.
    • partial: Some tools failed; the answer is degraded and must be labeled as such in the UI.
    • failed: No usable answer is available; only error information is returned.

    Error responses follow a unified structure:

    1. {"error": {"code": "FILE_TOO_LARGE", "message": "File exceeds 50 MB. Please compress or split it and upload again.", "trace_id": "tr_4b77d2"}}

    Common error codes:

    Error Code HTTP Status Description Client Handling
    INVALID_REQUEST 400 Missing parameters or type mismatch Fix and resubmit
    UNSUPPORTED_MEDIA_TYPE 415 File type is not in the allowlist Notify the user of supported formats
    FILE_TOO_LARGE 413 File exceeds size or page limits Notify the user to compress or split the file
    FILE_PARSE_FAILED 422 File cannot be parsed (e.g., encrypted or corrupted) Notify the user to check and re-upload
    IDEMPOTENCY_CONFLICT 409 Parameters differ for the same idempotency_key Generate a new key
    RATE_LIMITED 429 Request frequency limit exceeded Wait per the Retry-After header
    QUOTA_EXCEEDED 403 Usage quota exhausted Notify the user to upgrade the plan or retry the next day
    CONTENT_BLOCKED 422 Blocked by safety moderation Display a standard refusal message
    WEB_BLOCKED 403 Target page requires login, has anti-scraping measures, or restricts access Degrade gracefully; ask the user to upload content
    TOOL_TIMEOUT 504 Tool call timed out Handle according to the degradation matrix
    TOOL_UNAVAILABLE 503 Tool service is temporarily unavailable Disable the tool and retry once

    Tool Orchestration Sequence

    The server-side orchestrator processes a request in the following order:

    1. The gateway validates authentication, rate limits, parameter formats, and the file allowlist.
    2. Safety moderation is performed on the user input. If it fails, return CONTENT_BLOCKED directly.
    3. The model determines whether a tool call is needed and generates the tool_call.
    4. The orchestrator executes independent tools in parallel. Tools with dependencies are executed sequentially.
    5. Each tool result is recorded in the tool_call log, truncated as needed, and placed into the context.
    6. The model generates the final answer, which is checked by output moderation before being returned.
    7. The request details are written to the audit log.