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-Afterresponse header. - Idempotency: Clients must generate a unique
idempotency_keyfor 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_versionfield 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:
{"api_version": "2026-11-01","session_id": "sess_8f3a","idempotency_key": "req_20261124_0001","messages": [{"role": "user", "content": "Review the risk clauses in the attached contract and search for the latest judicial interpretation of the relevant provisions."}],"attachments": [{"file_id": "file_c91e", "mime_type": "application/pdf"}],"tools": ["file_parse", "web_search"],"tool_choice": "auto","stream": true,"options": {"max_tool_calls": 4,"max_context_tokens": 32000}}
Field descriptions:
tool_choice: Acceptsauto,none, or a specific tool name.stream: Whentrue, 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:
{"trace_id": "tr_4b77d2","session_id": "sess_8f3a","status": "partial","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.","citations": [{"source_title": "Full text of a judicial interpretation", "url": "https://example.com/interpretation", "published_at": "2026-06-10"}],"tool_calls": [{"tool_call_id": "tc_01", "name": "file_parse", "status": "succeeded", "duration_ms": 1840},{"tool_call_id": "tc_02", "name": "web_search", "status": "failed", "error_code": "TOOL_TIMEOUT", "duration_ms": 8000}],"usage": {"input_tokens": 12850, "output_tokens": 920}}
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:
{"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:
- The gateway validates authentication, rate limits, parameter formats, and the file allowlist.
- Safety moderation is performed on the user input. If it fails, return
CONTENT_BLOCKEDdirectly. - The model determines whether a tool call is needed and generates the
tool_call. - The orchestrator executes independent tools in parallel. Tools with dependencies are executed sequentially.
- Each tool result is recorded in the
tool_calllog, truncated as needed, and placed into the context. - The model generates the final answer, which is checked by output moderation before being returned.
- The request details are written to the audit log.
