Connecting Claude Code to Local Models: A Four-Layer Architecture Breakdown and GPU Cloud Deployment Guide

Breaking down the four-layer architecture and GPU server deployment path for connecting Claude Code to local models.
Based on a Bilibili technical sharing session, this article explains how to redirect Claude Code from vendor clouds to local models. Three core motivations: zero-cost experimentation, keeping data off the public internet, and free model selection. The four layers are: using Anthropic Base URL and Auth Token environment variables to take over the API endpoint; wrapping models as persistent services with inference engines (Ollama for individual validation, stronger engines for enterprise); using a protocol conversion layer to smooth over format differences and ensure tool calling; and using MCP Servers to fill in the local tool ecosystem. A four-step GPU cloud deployment process is also provided.
AI coding agents like Claude Code and Codex are becoming standard tools in developers' daily workflows, but by default they send requests to the vendor's cloud. If your code involves sensitive business logic, or you want to try every open-source model at zero cost, connecting Claude Code to local models becomes a necessity. Based on a technical sharing session from Bilibili, this article breaks down the underlying principles into four layers and provides a deployment path on GPU cloud servers.
Why Connect Claude Code to Local Models
Redirecting Claude Code to local models offers three direct benefits.
The first is zero-cost experimentation. Teams often need to compare different models' performance in the early stages of a project. The traditional approach is to register accounts on various platforms, switch back and forth, and burn through tokens — costly and cumbersome. With local deployment, the same agent can switch models at any time, run side-by-side comparisons with the same prompt, and tokens are no longer a constraint.
The second is data security. Code and requests stay on your own server and never go to third-party vendors' clouds, domestic or foreign. For teams with data sovereignty requirements, this is practically a hard requirement.
The third is freedom in model selection. According to the presenter, local deployment can involve "all kinds of models," including the DeepSeekAd series. Claude Code itself isn't tied to any specific model — even though it defaults to its own model, it can be switched via API configuration.

Core Architecture: Three Key Layers
The presenter breaks the overall architecture into three key layers, which form the foundation for understanding the subsequent operations.
The request router takes over the API endpoint via environment variables, redirecting requests originally pointed at the vendor's cloud to a local service. The protocol conversion layer acts as middleware, handling API format translation and toolchain compatibility — because the interface protocols exposed by different inference engines may not be fully compatible with Anthropic's official API. The capability extension layer corresponds to the MCP server ecosystem, supporting localized tool invocation.
Among these three layers, request routing is the entry point, protocol conversion is the key to making requests "understandable," and MCP determines whether the agent can actually get work done in a local environment.
Layer 1: How Environment Variables Take Over the API Endpoint
Claude Code is natively designed to steer users toward the official Anthropic API, which is directly tied to the vendor's token billing model. To achieve redirection, the key is setting two environment variables: Anthropic Base URL and Anthropic Auth Token.
The former should contain the local service's IP plus port, and is the most important setting in the entire configuration; the latter can be a dummy token placeholder, with the specific value depending on how your local service validates.
With these two variables, requests originally pointed at Anthropic's official cloud are seamlessly switched to a locally deployed open-source model. The presenter emphasizes that this step is "understandable even to non-technical people" — essentially it's just changing two config items to swap the destination from the cloud to your own server.

Layer 2: Inference Engines and Service Wrapping
For a local model to be callable by Claude Code, it must first become an API service. This requires an inference engine to handle model loading and inference computation.
Ollama is the lowest-barrier option — easy to get started with and well-documented by the community, making it suitable for individual developers doing quick validation. But the presenter explicitly warns: Ollama has poor parallelism, is mostly used by individuals, and is rarely adopted in真正的 enterprise scenarios. If you need to handle concurrent users in production, you'll need an inference engine with stronger parallelism.
Regardless of which engine you choose, the approach is the same: wrap the large model as a Service, expose the corresponding IP and port, and let Claude Code connect via the API protocol. If you're using a compute card rather than a standard GPU, it's equally feasible — provided you find an inference engine adapted to that compute card, which hardware vendors sometimes provide directly.

An inference engine is a software component specifically optimized for the inference phase of large models, responsible for loading model weights, managing VRAM, scheduling requests, and exposing API interfaces. Common open-source inference engines include vLLM, SGLang, TensorRT-LLM, and Ollama, which differ mainly in concurrent throughput, batching strategies (such as PagedAttention and Continuous Batching), support for quantization formats, and hardware compatibility. Ollama is highly encapsulated and works out of the box, but its underlying scheduling is relatively simple and requests tend to queue under concurrent load; engines like vLLM improve throughput through more aggressive batching and VRAM reuse techniques, making them better suited for multi-user production environments. Selection requires weighing model format compatibility, GPU architecture support, deployment complexity, and concurrency requirements.
Layer 3: Protocol Conversion and Toolchain Compatibility
This is the most easily underestimated layer. Claude Code speaks Anthropic's API protocol, while the interfaces exposed by local inference engines may not match, requiring a conversion layer in between to translate formats.
The significance of this layer isn't just "making it work" — it's about toolchain compatibility. The capabilities of AI coding agents depend heavily on tool interfaces such as function calling, file reading/writing, and command execution. The protocol conversion layer must ensure these tool calls work as usual on local models; otherwise the agent degrades into an ordinary chat window.
The Anthropic Messages API is the interface specification that Claude Code relies on. Its request body includes fields such as system, messages, and tools, and responses return text or tool call instructions in the form of content blocks. OpenAI-compatible interfaces use different field names and structures — for example, tool calls are placed in tool_calls, and the message role system also differs. The protocol conversion layer's job is to perform bidirectional mapping between these two schemas, including request parameter renaming, streaming response format conversion, and tool call result injection. Proxy projects like LiteLLM and One-API have these conversion capabilities built in, and can serve directly as an Anthropic-compatible endpoint to connect to local inference services, saving you the effort of writing adaptation code yourself.
Layer 4: MCP Server and the Local Tool Ecosystem
The capability extension layer corresponds to the MCP (Model Context Protocol) server ecosystem. MCP allows the agent to mount localized tools — such as reading local files, querying databases, and calling internal services.
Once this layer is connected, localized Claude Code achieves full productivity: the model infers locally, tools execute locally, data never leaves the internal network, and agent-style automation capabilities are preserved. The presenter mentions that several colleagues working on automation and intelligent products used this local model-switching capability to solve the high costs during the model selection phase.

MCP (Model Context Protocol) is an open protocol proposed by Anthropic to standardize how large model applications connect to external tools and data sources. It uses a client-server model: the agent acts as an MCP client and discovers and invokes tools, resources, and prompt templates exposed by MCP Servers through a unified JSON-RPC interface. Because the protocol is decoupled from specific models, the same MCP Server can be called by cloud models and reused in local model scenarios. This means that after switching Claude Code to local models, tool capabilities such as filesystem access, database queries, and internal API calls don't need to be rewritten — as long as the model itself supports function calling, they continue to work.
Deployment Path on GPU Cloud Servers
Deploying on a Linux server is the demo scenario in the sharing, but Windows and macOS are equally viable. The overall process can be summarized in four steps:
- Select and deploy an inference engine on a GPU cloud server, confirming it's compatible with your GPU;
- Load the target model and start the inference engine as a persistent Service, exposing the IP and port;
- Configure
Anthropic Base URLon the Claude Code side to point to that address, and set the token; - Verify protocol compatibility, add a protocol conversion layer if necessary, then mount MCP tools.
Regarding whether models can be trained locally on 8GB of VRAM, the presenter's answer is yes, as mentioned in a previous sharing session. However, training and inference have quite different VRAM requirements, so actual selection still needs to be evaluated based on model size.
Common Questions
Are Claude Code and Codex the same software? No. They are agent products from different vendors, equivalent in logic to Cursor, Trae, and OpenCode. The presenter chose Codex for the demo simply because it's more well-known.
Isn't Codex limited to GPT? In the context of connecting to local models, this is no longer a limitation. As long as adaptation is done through the API protocol, you can freely switch to any model — GPT or otherwise, there's no difference.
Can I use domestic API providers? Yes, the approach is the same — switching the API endpoint configuration.
Can it only be installed on Linux? No, Linux, Windows, and macOS are all supported. Linux was chosen for the demo only because the server environment is more typical.
Summary
Connecting Claude Code to local models isn't mysterious. It's essentially about using environment variables to redirect requests, using an inference engine to turn the model into a service, using a protocol conversion layer to smooth over interface differences, and using MCP to fill in tool capabilities. Each of the four layers has its role, and missing any one degrades the experience. For teams that need data sovereignty, cost control, or model selection, this path is worth trying out.
One-line Summary
Breaking down the four-layer architecture and GPU server deployment path for connecting Claude Code to local models.
Paragraph Summary
Based on a Bilibili technical sharing session, this article systematically explains how to redirect AI coding agents like Claude Code from vendor clouds to local models. There are three core motivations: zero-cost experimentation, keeping data off the public internet, and freely switching model choices. The architecture is divided into three layers — request routing, protocol conversion, and capability extension — which are further refined into four layers during implementation: taking over the API endpoint with the two environment variables Anthropic Base URL and Auth Token; wrapping the model as a persistent service with an inference engine (Ollama suits individual validation, while enterprises need engines with stronger parallelism); using a protocol conversion layer to smooth over format differences between Anthropic and local interfaces and ensure tool calling; and using MCP Servers to fill in the local tool ecosystem. The article also provides a four-step deployment process on GPU cloud servers and clarifies common questions such as the relationship between Codex and Claude Code and cross-platform support.
Related articles

How to Learn AI Agents? A Roadmap That Cuts Your Learning Load in Half
Confused by the flood of Agent concepts? A Bilibili tutorial offers a three-step roadmap: build a knowledge map to cut learning load in half, drive learning with real projects, and calculate ROI—plus three questions to ask about any new concept, helping application developers escape tech anxiety.

Codex Zero-Basics Hands-On: Local Sandbox and MCP Ecosystem Explained
Bilibili tutorial on zero-basics Codex AI programming: Codex vs Claude Code, ChatGPT registration and mobile setup, local sandbox, Spring MVC debugging, Skills for PPTs, and MCP with Amap—ideal for non-programmers.

Tencent WorkBuddy Step-by-Step Tutorial: From Installation to AI-Automated Office Work in Practice
Tencent WorkBuddy step-by-step tutorial: explains the AI agent's four-step working mechanism, three-layer architecture, and multi-model advantages, covering document writing, data cleaning, PPT generation, and other office scenarios, and introduces installation and usage of four entry points: PC, mobile, web, and mini program.