Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:2aronS Duel Agents Protocol Multiplexing

From Leeroopedia
Knowledge Sources
Domains Networking, API_Design
Last Updated 2026-10-27 14:00 GMT

Overview

Pattern for routing requests to a single backend through two different wire protocols — OpenAI chat completions and Anthropic messages — by multiplexing on a protocol flag that selects the request path, header set, and body schema, while sharing a common transport layer.

Description

When a proxy is wire-compatible with two different API styles, a client library can serve both communities without duplicating the transport code. The key insight is that the two protocols differ only in three respects:

  • Endpoint path — OpenAI uses `/chat/completions`; Anthropic uses `/messages`
  • Headers — both use `Authorization: Bearer <key>` and `Content-Type: application/json`, but Anthropic additionally requires `x-api-key` and `anthropic-version: 2023-06-01`
  • Request/response schema — OpenAI uses `messages` with roles `system`/`user`/`assistant`; Anthropic uses a top-level `system` field and messages with roles `user`/`assistant`

Protocol multiplexing factors out the shared logic (fetch, timeout, response parsing, error mapping) into a single `request` method, and exposes two typed entry points — `chat.completions.create` and `messages.create` — that differ only in the path, the protocol flag, and the parameter schema. The protocol flag drives the header injection inside the shared method.

This gives users a single client instance that can speak either protocol, and gives maintainers one transport code path to test and secure.

Usage

Use this principle when:

  • Building a client for a proxy that accepts multiple API wire formats
  • You want to offer both OpenAI- and Anthropic-compatible interfaces from one SDK
  • The transport layer (fetch, timeout, auth, error handling) is identical across protocols
  • You need to enforce protocol-specific constraints (e.g., streaming not yet supported on one path)

Theoretical Basis

The shared transport is parameterized by a protocol tag:

# Abstract algorithm (NOT real implementation)
Protocol = Literal["openai", "anthropic"]

def request(path: str, body: dict, protocol: Protocol) -> dict:
    headers = {"Authorization": f"Bearer {key}", "Content-Type": "application/json"}
    if protocol == "anthropic":
        headers["anthropic-version"] = "2023-06-01"
        headers["x-api-key"] = key
    res = fetch(base_url + path, method="POST", headers=headers,
                body=json.dumps(body), timeout=timeout_ms)
    return parse_and_map_errors(res)

The two public entry points are thin wrappers:

def chat_completions_create(params: ChatCompletionCreateParams) -> ChatCompletion:
    if params.stream:
        raise ApiError("Streaming not supported yet", 400)
    return request("/chat/completions", params, "openai")

def messages_create(params: MessagesCreateParams) -> MessagesResponse:
    return request("/messages", params, "anthropic")

The multiplexing point is the protocol enum, which selects the path and header set. The typed parameter schemas enforce the wire contract at the call site before any network activity.

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment