How to Connect a Third-Party LLM Coding Assistant: A Protocol Conversion Guide

    This article explains how to connect a coding assistant that expects Anthropic-style APIs to third-party LLMs that expose OpenAI-style endpoints. It covers runtime setup, the three credentials you need from your model provider, request and response mapping, authentication adaptation, streaming handling, and common debugging scenarios. By the end, you will understand how a lightweight local proxy lets you switch backend models through configuration alone.

    1. Why You Need a Protocol Conversion Layer

    Many developers run into a practical problem when using AI coding tools: the model bundled with the tool may not fit a specific use case in terms of capability, latency, context length, or usage quota. Meanwhile, most mainstream cloud LLM services and compatibility wrappers around open-source models expose the industry-standard OpenAI-style API. The two API styles differ in request structure, field naming, and streaming response format, so connecting them directly will fail.

    The solution is not to modify the tool itself, but to add a lightweight protocol conversion layer locally. It presents itself as a local service with an Anthropic-style interface, and internally translates each request into OpenAI style before forwarding it upstream. This design has three benefits: no client changes, the ability to switch backend models at any time, and a single place for logging and rate limiting.

    Note that this article does not recommend or review any specific commercial product. It discusses a general engineering practice: cross-protocol adaptation.

    2. Environment Setup

    2.1 Runtime Version

    The conversion script relies on features such as async I/O and structured type annotations, so Python 3.10 or later is recommended. With an older version, new syntax will fail immediately, and debugging that is far more costly than confirming the version up front.

    You can install Python from the official installer, a package manager on macOS, or a distribution such as Anaconda. For team environments, isolate dependencies with a virtual environment:

    1. python3 -m venv .venv
    2. source .venv/bin/activate
    3. pip install fastapi uvicorn httpx

    2.2 Client Installation

    AI coding tools are typically distributed as command-line programs. On macOS you can install via a package manager; on other platforms, follow the official documentation. After installation, confirm the executable is on your PATH before continuing.

    2.3 Dependencies

    • Service framework: exposes the local HTTP endpoint.
    • ASGI server: listens on the port and handles concurrent connections.
    • HTTP client: sends requests to the upstream model service; must support async and streaming reads.

    3. Three Things You Need from Your Model Provider

    Before writing any code, get three items from the provider’s console:

    1. API key: used for authentication. Make sure the target model is enabled for your account; many failures are actually permission issues.
    2. Endpoint URL: usually in the form /v1/chat/completions. Distinguish between public and internal addresses.
    3. Model name: must exactly match the identifier in the provider’s documentation, including case.

    Store these in environment variables or a local config file. Do not hardcode them in the script, and do not commit them to version control.

    4. Core Responsibilities of the Conversion Layer

    A working conversion service needs to do four things:

    • Listen on a local port and receive raw requests from the client.
    • Map the request body from Anthropic structure to OpenAI structure.
    • Forward it upstream with authentication, handling both streaming and non-streaming modes.
    • Map the upstream response back into a structure the client can parse.

    4.1 Request Mapping

    The main differences are in a few areas: where the system prompt goes, message role naming, the field structure for tool calls versus function calls, and parameter names such as max output length. The conversion function is essentially a two-way field mapping table; keep it in one place rather than scattering it across the code.

    1. def to_openai(payload: dict) -> dict:
    2. messages = []
    3. if payload.get("system"):
    4. messages.append({"role": "system", "content": payload["system"]})
    5. for m in payload.get("messages", []):
    6. messages.append({"role": m["role"], "content": m["content"]})
    7. return {
    8. "model": MODEL_NAME,
    9. "messages": messages,
    10. "max_tokens": payload.get("max_tokens", 4096),
    11. "stream": bool(payload.get("stream")),
    12. }

    4.2 Authentication Adaptation

    Two common authentication header styles exist: Authorization: Bearer {key}, and custom headers such as api-key or x-api-key. Make the authentication method configurable so that switching providers requires only a config change, not a code change.

    4.3 Response and Streaming Handling

    Non-streaming responses are relatively simple: just rename fields. Streaming responses require parsing incremental chunks and wrapping each delta into the event sequence the client expects, then sending the termination marker correctly. This is the most error-prone part of integration testing. Get the non-streaming path working first, then enable streaming.

    5. Integration Testing and Debugging

    After starting the service, verify connectivity with the simplest possible request, then gradually add multi-turn conversations, system prompts, and tool calls. Common failures and how to diagnose them:

    • 401/403: wrong key, or the model is not enabled for your account.
    • 404: the endpoint URL is missing the version path.
    • Format parsing failure: the upstream returned an error body but the conversion layer did not pass the error through; add the missing exception branch.
    • Streaming interruption: the client timeout is too short, or the conversion layer is not forwarding the termination event correctly.

    For production, add timeout control, retries with backoff, request logging, and usage tracking. If needed, deploy the service on a container platform and connect it to logging and monitoring with alerts so you can observe latency and failure rates.

    6. Summary

    The key to cross-protocol adaptation is not the amount of code, but thinking clearly about three things: field mapping, authentication, and streaming semantics. Once the conversion layer is in place, switching backend models becomes a configuration change, which has real value for cost control and model selection. If you use model services from Baidu AI Cloud, the same general adaptation approach applies; just replace the endpoint URL, key, and model identifier.