页面简介
Guide to use open models with Claude Code on your local device.
正文摘录
This step-by-step guide shows you how to connect open LLMs and APIs to Claude Code entirely locally, complete with screenshots. Run using any open model like Qwen3.6, DeepSeek and Gemma.For this tutorial, we’ll use the open models: Gemma 4 and Qwen3.5 which are strong agentic & coding models (works on 24GB RAM/unified mem device).For inference, we'll use Unsloth Desktop and llama.cpp which enables you to run/serve LLMs on macOS, Linux, and Windows. You can use any other model. For model quants, we utilize Unsloth Dynamic GGUFs to run any quantized LLM, while retaining accuracy.Claude Code running with Qwen3.5 locally.Claude Code Setup📖 Setup Local Model Tutorial Claude Code SetupBefore setting up our local LLM, we need to install Claude Code. Claude Code is a terminal-based coding agent that understands your codebase and handles complex Git workflows using natural language.macOS, Linux, WSLWindowsInstall Claude Code:Paste into your terminal to install Claude Code:AskCopycurl -fsSL https://claude.ai/install.sh | bashAfter install, navigate to your project folder. Then type claude into the shell to begin.AskCopycd ~/projects/my-project claudeInstall Claude Code:Enter into PowerShell to install Claude Code:AskCopyirm https://claude.ai/install.ps1 | iexAfter install, navigate to your project folder. Then type claude into the powershell to begin.AskCopycd /path/to/your/project claude🕵️Fixing 90% slower inference in Claude CodeClaude Code recently prepends and adds a Claude Code Attribution header, which invalidates the KV Cache, making inference 90% slower with local models.The attribution is a line prepended to the start of the system prompt (x-anthropic-billing-header: cc_version=...; cch=...;) whose value changes on every request, so the whole prompt prefix misses the KV cache each turn.The simplest fix is to disable it inline when you launch Claude Code, so there is no file to edit:AskCopyclaude --settings '{"env":{"CLAUDE_CODE_ATTRIBUTION_HEADER":"0","CLAUDE_CODE_ENABLE_TELEMETRY":"0"}}' --model unsloth/gemma-4-26B-A4B-it-GGUFRecent Claude Code releases also honor export CLAUDE_CODE_ATTRIBUTION_HEADER=0; older builds ignored the shell variable, so the --settings form above (or the settings file below) is the reliable choice.To make it permanent, add CLAUDE_CODE_ATTRIBUTION_HEADER set to 0 inside "env" in ~/.claude/settings.json. For example do cat > ~/.claude/settings.json then add the below (when pasted, do ENTER then CTRL+D to save it). If you have a previous ~/.claude/settings.json file, just add "CLAUDE_CODE_ATTRIBUTION_HEADER" : "0" to the "env" section, and leave the rest of the settings file unchanged.📖 Quickstart TutorialsBefore we begin, we firstly need to complete setup for the specific model you're going to use. We use Unsloth (a web UI) and llama.cpp which are open-source frameworks for running and serving LLMs on your Mac, Linux, Windows devices.Unsloth also has unique self-healing tool-calling and web search capabilities. See right for Claude Code connected to Unsloth:Connect Claude Code🦥 Unsloth Tutorial llama.cpp Tutorial🦥 Unsloth TutorialFor this tutorial, we will serve/connect local models to Claude Code via a UI by using Unsloth. Unsloth works on Windows, WSL, Linux and MacOS.Search, download, run GGUFs and safetensor modelsSelf-healing tool calling + web searchCode execution (Python, Bash)Automatic inference parameter selection (temp, top-p, etc.)Fast CPU + GPU inference via llama.cppTrain LLMs 2x faster with 70% less VRAMSee below for install instructions:Example of Qwen3.6 2-bit running in Unsloth.1Download UnslothThe easiest way to get started is by installing the Unsloth Desktop app. It supports MacOS, Linux, Windows, NVIDIA, AMD, Intel and CPU setups.Download Unsloth Download for macOS Download for Windows Download for LinuxOr, if you prefer manual installation:MacOS, Linux, WSL:Windows PowerShell:2InstallOpen the Unsloth installer (.dmg, .exe files)Drag Unsloth to Applications for Mac or complete setup for Windows.Launch the app and wait for installation to complete3Choose a modelOpen 'Select model' dropdown ontop or 'Model hub' tab, choose a model and a quantization that fits your device, then download it. Once it finishes, start chatting - no setup required.4Unsloth is now readyTo start chatting, type a message and press Enter.Connect tools: Claude Code, Codex, web search, MCP and moreTrain models: Fine-tune text, diffusion, embedding, and moreGenerate media: Create and train images, video, TTS locallyModel Loading + API Guide1Select ModelBefore using the API, load a model from the Select model dropdown in the top-left corner of the Chat page.In this guide, we’ll use: unsloth/gemma-4-26B-A4B-it-GGUF with the recommended UD-Q4_K_XL quantization.2Test the ModelBefore using the Client, send a quick message:This confirms that the model loaded correctly and is ready to respond.3Unsloth API keyIn Unsloth, open Settings → API to view or create your API key.Treat your API key like a password and avoid exposing it in screenshots or repositories.⚙️ Connect Claude CodeNow that we have setup the local LLM for Claude Code, we now configure Claude Code to work with your tool. You can either connect easily with unsloth start below or do it manually.⚡ Run Claude Code with unsloth startTo launch Claude directly with a model, run:If there are no settings/sampling flags set, Unsloth automatically selects the best/recommended settings for the model including context length, temperature etc.With a model loaded in Unsloth Studio, open your project folder and run:Claude Code running against the model loaded in Unsloth Studio.Unsloth sets the local endpoint, API key, model and context length for this launch. Your normal Claude Code configuration is left alone.Claude Code already keeps conversations in its normal session store, so --persist is not needed. Continue your latest session with:See the complete unsloth start reference for loading a model from the command line, remote Unsloth servers and advanced options.The rest of this guide covers the manual llama.cpp setup.🔌 Connect manuallyIf you prefer to set it up manually, you can start by setting the following environment variables. These variables will not persist between sessions by default.MacOS, Linux, WSLWindowsConfig: Set the local API URL:Copy your key from Unsloth Studio → Settings → API (or from the console when you start it with unsloth run, where it is printed as sk-unsloth-...), then set it.Also set an empty ANTHROPIC_API_KEY so Claude Code does not prompt for a cloud key:Optional: Use the name of the model currently loaded in Unsloth as a default.Use the full model id exactly as it appears in GET http://localhost:8888/v1/models (the same string you pass to claude --model).Config: Set the local API URL in Powershell:Copy your key from Unsloth Studio → Settings → API, then set it:Optional: Use the name of the model currently loaded in Unsloth to set as a default.Model name should be the model that is currently loaded in Unsloth Studio.Start Claude CodeStart Claude Code with the model that is currently loaded in Unsloth.We will use gemma-4-26B-A4B-it-GGUF, but you can use any Unsloth compatible model.For an extra speed boost on local models, you can also launch with --bare --exclude-dynamic-system-prompt-sections. See Optional: shrink the system prompt below.Claude Code should open and display the selected model.See Fixing 90% slower inference in Claude Code first to fix open models being 90% slower due to KV Cache invalidation.Try this prompt to research and rank high-quality SFT datasets:After you submit the prompt, the agent will search the web, evaluate findings, and write the final report. This may take a few minutes.Some workflows may require you to approve actions or answer follow up prompts.Some workflows may require you to approve actions or answer follow-up prompts.Once complete, the generated sft_report.md will look similar to this.If you see Unable to connect to API (ConnectionRefused) , remember to unset ANTHROPIC_BASE_URL via unset ANTHROPIC_BASE_URLIf you find open models to be 90% slower, see here first to fix KV cache being invalidated.Optional: shrink the system promptClaude Code was built for Anthropic's hosted models, so its default system prompt is large. On local models you can trim it for faster responses and better KV-cache reuse by adding two flags when you launch:--bare skips auto-discovery of hooks, skills, plugins, MCP servers and CLAUDE.md (Claude keeps Bash and file read/edit), and --exclude-dynamic-system-prompt-sections moves per-machine sections out of the prompt prefix. Both shrink the prompt and improve KV-cache reuse, which makes local models noticeably faster. They are optional and do not change the connection setup above.Optional: tune the Unsloth serverClaude Code uses the model running in Unsloth. You can customize how the server behaves when starting it.Use --disable-tools when driving Claude Code (or any external coding agent). By default Unsloth Studio runs its own server-side tools, which swallows the agent's tool calls, so Claude Code answers but never edits files. --disable-tools switches to passthrough, so Claude Code's own Write/Edit/Bash tools are used.Use --reasoning off to turn thinking off, or --reasoning on to turn it on for models that support reasoning.This starts the server on 0.0.0.0:8888, allowing other devices on your local network to connect.Use -p to change which port the server runs on. Use -H 0.0.0.0 if you want phones, laptops, or other devices on your network to connect.For more advanced runtime configuration, see the main API tuning section.🦙 Llama.cpp TutorialBefore we begin, we firstly need to complete setup for the specific model you're going to use. We use llama.cpp which is an open-source framework for running LLMs on your Mac, Linux, Windows etc. devices. Llama.cpp contains llama-server which allows you to serve and deploy LLMs efficiently. The model will be served on port