> For the complete documentation index, see [llms.txt](https://unsloth.ai/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://unsloth.ai/docs/zh/ji-chu-zhi-shi/claude-code.md).

# 如何使用 Claude Code 运行本地 LLM

在你的本地设备上使用 Claude Code 与开源模型的指南。

本分步指南将展示如何将开源 LLM 和 API 完全本地连接到 Claude Code，并附带截图。可使用任何开源模型运行，例如 Qwen3.6、DeepSeek 和 Gemma。

{% columns %}
{% column width="58.333333333333336%" %}
本教程将使用以下开源模型： [Gemma 4](/docs/zh/mo-xing/gemma-4.md) 和 [Qwen3.5](/docs/zh/mo-xing/qwen3.5.md) 它们是强大的智能体与编程模型（可在 24GB RAM/统一内存设备上运行）。

进行推理时，我们将使用 [Unsloth Desktop](https://github.com/unslothai/unsloth) 和 [`llama.cpp`](https://github.com/ggml-org/llama.cpp) 它让你能够在 macOS、Linux 和 Windows 上运行/部署 LLM。你也可以使用其他任何模型。对于模型量化文件，我们使用 Unsloth [动态 GGUF](/docs/zh/ji-chu-zhi-shi/dynamic-3.0-ggufs.md) 以在保留精度的同时运行任何量化 LLM。
{% endcolumn %}

{% column width="41.666666666666664%" %}

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FpXE6kCHjh8qOEaggf94M%2FScreenshot_20260718_122426.png?alt=media&amp;token=a59e4c8c-efdb-451b-b1f8-621955564f6d" alt=""><figcaption><p>Claude Code 在本地运行 Qwen3.5。</p></figcaption></figure>
{% endcolumn %}
{% endcolumns %}

<a href="/docs/zh/ji-chu-zhi-shi/claude-code.md#claude-code-setup" class="button primary" data-icon="claude">Claude Code 设置</a><a href="/docs/zh/ji-chu-zhi-shi/claude-code.md#quickstart-tutorials" class="button primary">📖 本地模型设置教程</a>

## <i class="fa-claude">:claude:</i> Claude Code 设置

在设置本地 LLM 之前，我们需要先安装 Claude Code。Claude Code 是一个基于终端的编程智能体，能够理解你的代码库，并使用自然语言处理复杂的 Git 工作流。

{% tabs %}
{% tab title="macOS、Linux、WSL" %}

#### **安装 Claude Code：**

将以下内容粘贴到终端中以安装 Claude Code：

```bash
curl -fsSL https://claude.ai/install.sh | bash
```

安装完成后，导航到你的项目文件夹。然后输入 `claude` 到 `shell` 开始。

```bash
cd ~/projects/my-project 
claude
```

{% endtab %}

{% tab title="Windows" %}

#### **安装 Claude Code：**

进入 `PowerShell` 以安装 Claude Code：

```powershell
irm https://claude.ai/install.ps1 | iex
```

安装完成后，导航到你的项目文件夹。然后输入 `claude` 到 `powershell` 开始。

<pre class="language-powershell"><code class="lang-powershell"><strong>cd /path/to/your/project
</strong>claude
</code></pre>

<div data-with-frame="true"><figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F49Pt77DNcx9d50uwiXWy%2Fimage.png?alt=media&amp;token=f1561255-861b-481d-9c75-824d5bf297a7" alt="" width="563"><figcaption></figcaption></figure></div>
{% endtab %}
{% endtabs %}

### :detective:修复 Claude Code 中慢 90% 的推理

{% hint style="warning" %}
Claude Code 最近会在前面添加一个 Claude Code Attribution 标头，这会 **使 KV Cache 失效，从而导致本地模型的推理速度慢 90%。**.
{% endhint %}

该归因信息是一行添加到 **系统提示词开头的内容** (`x-anthropic-billing-header: cc_version=...; cch=...;`）其值会在每次请求时变化，因此整个提示前缀在每一轮都会无法命中 KV cache。

最简单的修复方式是在启动 Claude Code 时直接将其禁用，这样就无需编辑文件：

{% code overflow="wrap" %}

```bash
claude --settings '{\"env\":{\"CLAUDE_CODE_ATTRIBUTION_HEADER\":\"0\",\"CLAUDE_CODE_ENABLE_TELEMETRY\":\"0\"}}' --model unsloth/gemma-4-26B-A4B-it-GGUF
```

{% endcode %}

{% hint style="info" %}
较新的 Claude Code 版本也支持 `export CLAUDE_CODE_ATTRIBUTION_HEADER=0`；旧版本会忽略这个 shell 变量，因此 `--settings` 上面的形式（或下面的设置文件）才是可靠的选择。
{% endhint %}

若要永久生效，请添加 `CLAUDE_CODE_ATTRIBUTION_HEADER` 设为 0 于 `env` 在 `~/.claude/settings.json`。例如执行 `cat > ~/.claude/settings.json` 然后添加下面的内容（粘贴后按 ENTER，再按 CTRL+D 保存）。如果你已经有一个之前的 `~/.claude/settings.json` 文件，只需添加 `CLAUDE_CODE_ATTRIBUTION_HEADER : 0` 到 env 部分，并保持设置文件的其余部分不变。

<pre class="language-json"><code class="lang-json">{
  "promptSuggestionEnabled": false,
  "env": {
    "CLAUDE_CODE_ENABLE_TELEMETRY": "0",
    "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
    <a data-footnote-ref href="#user-content-fn-1">CLAUDE_CODE_ATTRIBUTION_HEADER : 0</a>
  },
  "attribution": {
    "commit": "",
    "pr": ""
  },
  "plansDirectory" : "./plans",
  "prefersReducedMotion" : true,
  "terminalProgressBarEnabled" : false,
  "effortLevel" : "high"
}
</code></pre>

## 📖 快速开始教程

{% columns %}
{% column %}
在开始之前，我们首先需要完成你将使用的特定模型的设置。我们使用 [Unsloth](/docs/zh/xin/studio.md) （一个 web UI）以及 llama.cpp，它们是用于在你的 Mac、Linux、Windows 设备上运行和部署 LLM 的开源框架。

Unsloth 还具有独特的自我修复 [工具调用](/docs/zh/xin/studio/chat.md#auto-healing-tool-calling) 和 [网页搜索](/docs/zh/xin/studio/chat.md#code-execution) 能力。右侧可查看 Claude Code 连接到 Unsloth 的示例：
{% endcolumn %}

{% column %}

<div data-with-frame="true"><figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F1s98Id9xclzwMfxjXw2O%2Funsloth%20api%20cropped.png?alt=media&amp;token=64fac263-ca5b-4447-a740-41f58ec94904" alt=""><figcaption></figcaption></figure></div>
{% endcolumn %}
{% endcolumns %}

<a href="/docs/zh/ji-chu-zhi-shi/claude-code.md#connect-claude-code" class="button primary" data-icon="claude">连接 Claude Code</a><a href="/docs/zh/ji-chu-zhi-shi/claude-code.md#unsloth-tutorial" class="button primary">🦥 Unsloth 教程</a><a href="https://unsloth.ai/docs/basics/claude-code#llama.cpp-tutorial" class="button primary"> llama.cpp 教程</a>

## 🦥 Unsloth 教程

在本教程中，我们将通过 UI 使用……将本地模型提供/连接到 Claude Code，方法是使用 [Unsloth](https://github.com/unslothai/unsloth)。Unsloth 可在 Windows、WSL、Linux 和 macOS 上运行。

{% columns %}
{% column %}

* 搜索、下载， [运行 GGUF](/docs/zh/xin/studio.md#run-models-locally) 以及 safetensor 模型
* [**自我修复** 工具调用](/docs/zh/xin/studio.md#execute-code--heal-tool-calling) + **网页搜索**
* [**代码执行**](/docs/zh/xin/studio.md#run-models-locally) （Python、Bash）
* [自动推理](https://unsloth.ai/docs/desktop#feature-deep-dive) 参数选择（temp、top-p 等）
* 通过 llama.cpp 实现快速 CPU + GPU 推理
* [训练 LLM](/docs/zh/xin/studio.md#no-code-training) 速度提升 2 倍，VRAM 减少 70%

安装说明见下方：
{% endcolumn %}

{% column %}

<div data-with-frame="true"><figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FY3nfB43TEk7k11zcE4wm%2Fthe%20big%20one.gif?alt=media&amp;token=335be087-7375-4f89-9039-71195ee44ab8" alt=""><figcaption><p>Qwen3.6 2-bit 在 Unsloth 中运行的示例。</p></figcaption></figure></div>
{% endcolumn %}
{% endcolumns %}

{% stepper %}
{% step %}

#### 下载 Unsloth

最简单的开始方式是安装 [Unsloth Desktop](/docs/zh/desktop.md) 应用。它支持 [MacOS](/docs/zh/kuai-su-kai-shi/install/mac.md)、Linux、 [Windows](/docs/zh/kuai-su-kai-shi/install/windows-installation.md), [NVIDIA](/docs/zh/kuai-su-kai-shi/install/pip-install.md), [AMD](/docs/zh/kuai-su-kai-shi/install/amd.md)、Intel 以及 CPU 配置。

<a href="https://unsloth.ai/download" class="button primary" data-icon="down-to-bracket">下载 Unsloth</a>

* <i class="fa-apple">:apple:</i> [下载 macOS 版本](https://unsloth.ai/download/mac)
* <i class="fa-windows">:windows:</i> [下载 Windows 版本](https://unsloth.ai/download/windows)
* <i class="fa-linux">:linux:</i> [下载 Linux 版本](https://unsloth.ai/download/linux)

或者，如果你更喜欢手动安装：

**MacOS、Linux、WSL：**

```bash
curl -fsSL https://unsloth.ai/install.sh | sh
```

**Windows PowerShell：**

```bash
irm https://unsloth.ai/install.ps1 | iex
```

{% endstep %}

{% step %}

#### 安装

1. 打开 Unsloth 安装程序（`.dmg`, `.exe` 文件）
2. 在 Mac 上将 Unsloth 拖到 Applications，或在 Windows 上完成安装。
3. 启动应用并等待安装完成
   {% endstep %}

{% step %}

#### 选择模型

打开顶部的“选择模型”下拉菜单或“模型中心”选项卡，选择适合你设备的模型和量化方式，然后下载。完成后就可以开始聊天——无需额外设置。

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FNCEVgKLJI0goPqjgcg9B%2Fmodel%20hub%20models.png?alt=media&amp;token=533b5e3c-a901-4b33-963e-4a703cc9d5a6" alt="" width="563"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### Unsloth 现已就绪

要开始聊天，请输入消息并按 Enter。

* **连接工具：** [Claude Code](/docs/zh/ji-chu-zhi-shi/claude-code.md), [Codex](/docs/zh/ji-chu-zhi-shi/codex.md)、网络搜索、 [MCP](/docs/zh/ji-chu-zhi-shi/mcp.md) 以及更多
* **训练模型：** 微调文本、扩散、嵌入等更多内容
* **生成媒体：** 在本地创建并训练图像、视频、TTS

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FpAGvwjGD0iVMZKBoyu7m%2Fgreeennn.png?alt=media&amp;token=d17a5528-8375-444c-9aff-f9e9f7903bcd" alt="" width="563"><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

### 模型加载 + API 指南

{% stepper %}
{% step %}

#### 选择模型

在使用 API 之前，请先从 Chat 页面左上角的 **选择模型** 下拉菜单中加载一个模型。

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FuZqd6tcZ5LgMSh4ZND5x%2Fexport-1778505117710-24fps.gif?alt=media&amp;token=9defec95-5404-4654-9c33-67be967c9820" alt=""><figcaption></figcaption></figure>

在本指南中，我们将使用： `unsloth/gemma-4-26B-A4B-it-GGUF` 以及推荐的 `UD-Q4_K_XL` 量化。
{% endstep %}

{% step %}

#### 测试模型

在使用客户端之前，先发送一条简短消息：

<div data-with-frame="true"><figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F2Ivilke0aJX8AHWDwSmr%2Fimage.png?alt=media&amp;token=9f9380b9-f963-4861-a17b-fd0fe16684d4" alt="" width="563"><figcaption></figcaption></figure></div>

{% hint style="info" %}
这可以确认模型已正确加载并可响应。
{% endhint %}
{% endstep %}

{% step %}

#### **Unsloth API 密钥**

在 Unsloth 中，打开 **设置 → API** 以查看或创建你的 API 密钥。

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FAZwaRmBVPpXA2SFhMGW9%2Fexport-1778506924396-30fps.gif?alt=media&amp;token=96f3f1a7-fce4-4508-b1b0-e8b6294dc423" alt=""><figcaption></figcaption></figure>

请将你的 API 密钥视为密码，避免在截图或代码仓库中暴露它。
{% endstep %}
{% endstepper %}

## ⚙️ 连接 Claude Code

既然我们已经为 Claude Code 设置好了本地 LLM，接下来配置 Claude Code 以配合你的工具使用。你可以轻松通过 `unsloth start` 下面的方式连接，或者手动进行 [手动](#connect-manually).

### ⚡ 使用……运行 Claude Code `unsloth start`

要直接用模型启动 Claude，请运行：

```bash
unsloth start claude \\
    --model unsloth/Qwen3.8-27B-GGUF-GGUF:UD-Q4_K_XL
    --temp 1.0 \\
    --top-p 0.95 \\
    --top-k 20 \\
    --min-p 0.0 \\
    --reasoning-effort medium
```

{% hint style="success" %}
如果没有设置任何配置/采样标志，Unsloth 会自动为该模型选择最佳/推荐设置，包括上下文长度、temperature 等。
{% endhint %}

在 Unsloth Studio 中加载模型后，打开你的项目文件夹并运行：

```bash
unsloth start claude
```

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FB1CAedMQxexmFjLLLG5A%2FScreenshot_20260714_151401.png?alt=media&amp;token=c7badb92-b565-467d-bd90-35f8fba44f29" alt="Claude Code connected to a local model through Unsloth Studio"><figcaption><p>Claude Code 正在针对 Unsloth Studio 中加载的模型运行。</p></figcaption></figure>

Unsloth 会为本次启动设置本地端点、API 密钥、模型和上下文长度。你常规的 Claude Code 配置不会被修改。

Claude Code 已经会将对话保存在其常规会话存储中，因此 `--persist` 不需要。继续你最近的会话：

```bash
unsloth start claude --continue
```

查看完整的 `unsloth start` 用于从命令行加载模型、远程 Unsloth 服务器以及高级选项的参考。

本指南其余部分将介绍手动 `llama.cpp` 设置。

#### 🔌 手动连接

如果你更愿意手动设置，可以先配置以下环境变量。默认情况下，这些变量不会在会话之间持续保留。

{% tabs %}
{% tab title="macOS、Linux、WSL" %}
**配置：** 设置本地 API URL：

```bash
export ANTHROPIC_BASE_URL="http://localhost:8888"
```

从 Unsloth Studio → Settings → API 复制你的密钥（或者从控制台中复制，当你使用 `unsloth run`启动它时，它会显示为 `sk-unsloth-...`），然后进行设置。

{% code overflow="wrap" %}

```bash
export ANTHROPIC_AUTH_TOKEN="sk-unsloth-xxxxxxxxxxxx"
```

{% endcode %}

同时设置一个空的 `ANTHROPIC_API_KEY` 这样 Claude Code 就不会提示输入云端密钥：

```bash
export ANTHROPIC_API_KEY=""
```

可选：将当前在 Unsloth 中加载的模型名称设为默认值。

```bash
export ANTHROPIC_MODEL="unsloth/gemma-4-26B-A4B-it-GGUF"
```

请使用完整的模型 ID，完全按照其在 `GET http://localhost:8888/v1/models` （与传给 `claude --model`).
{% endtab %}

{% tab title="Windows" %}
**配置：** 在 PowerShell 中设置本地 API URL：

```powershell
$env:ANTHROPIC_BASE_URL = "http://localhost:8888"
```

从 **Unsloth Studio → Settings → API**复制你的密钥，然后设置：

```powershell
$env:ANTHROPIC_AUTH_TOKEN = "sk-unsloth-xxxxxxxxxxxx"
```

**可选：** 将当前在 Unsloth 中加载的模型名称设为默认值。

```powershell
$env:ANTHROPIC_MODEL = "gemma-4-26B-A4B-it-GGUF"
```

{% hint style="info" %}
模型名称应为当前在 Unsloth Studio 中加载的模型。
{% endhint %}
{% endtab %}
{% endtabs %}

### 启动 Claude Code

使用当前在 Unsloth 中加载的模型启动 Claude Code。

我们将使用 `gemma-4-26B-A4B-it-GGUF`，但你可以使用任何与 Unsloth 兼容的模型。

```shellscript
claude --model unsloth/gemma-4-26B-A4B-it-GGUF
```

{% hint style="info" %}
若要进一步提升本地模型速度，还可以使用以下参数启动： `--bare --exclude-dynamic-system-prompt-sections`。参见下方的可选项：缩减系统提示词。
{% endhint %}

Claude Code 应该会打开并显示所选模型。

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FilIbwzXF5WyEzHSTzNgj%2Fimage.png?alt=media&amp;token=0dcd2039-3bf5-4e73-b761-a73b28543d3b" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
请先查看 [#fixing-90-slower-inference-in-claude-code](#fixing-90-slower-inference-in-claude-code "mention") ，先修复开源模型因 KV Cache 失效而慢 90% 的问题。
{% endhint %}

试试这个提示词来研究并排名高质量的 SFT 数据集：

{% code overflow="wrap" %}

```
你只能在 project/ 中工作。不要搜索 CLAUDE.md——这里就是它。使用网络搜索在 Hugging Face 上找出 10 个真实的 instruction/chat/SFT 数据集，在研究过程中简要总结你的发现，并解释每个数据集为何与 SFT 相关，然后创建 sft_report.md 作为一份润色过的 Markdown 报告，包含排名、数据集名称、创建者、3–5 个相关标签、简短的通俗摘要，以及它为何对 SFT 有用。保持一切简洁易读，不要堆砌大量元数据、粘贴原始描述、超长标签列表或无关数据集。任务在 sft_report.md 包含 10 条干净、写得好的数据集条目时完成，并以以下内容结尾：“Successfully finetuned a model with Unsloth!”
```

{% endcode %}

在你提交提示词后，智能体会搜索网页、评估结果并撰写最终报告。这可能需要几分钟。

某些工作流可能需要你批准操作或回答后续提示。

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F1YVUcdNbQoPtje5RbbBp%2Fimage.png?alt=media&amp;token=c12e8f31-2698-41be-ab71-17f093f75b0c" alt="" width="563"><figcaption></figcaption></figure>

{% hint style="info" %}
某些工作流可能需要你批准操作或回答后续提示。
{% endhint %}

完成后，生成的 `sft_report.md` 会类似于这样。

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FWRvFItbYmBXgxebQKL8e%2Fimage.png?alt=media&amp;token=c0196814-5dc7-4c43-a2cc-29721704dff4" alt="" width="375"><figcaption></figcaption></figure>

{% hint style="warning" %}
如果你看到 `无法连接到 API（ConnectionRefused）` ，记得取消设置 `ANTHROPIC_BASE_URL` 的形式通过 `unset ANTHROPIC_BASE_URL`

如果你发现开源模型慢 90%， [先看这里](#fixing-90-slower-inference-in-claude-code) 以修复 KV cache 失效的问题。
{% endhint %}

### 可选：缩减系统提示词

Claude Code 是为 Anthropic 的托管模型构建的，因此其默认系统提示词很长。在本地模型上，你可以在启动时添加两个标志来裁剪它，以获得更快的响应和更好的 KV-cache 复用：

{% code overflow="wrap" %}

```shellscript
claude --model unsloth/gemma-4-26B-A4B-it-GGUF --bare --exclude-dynamic-system-prompt-sections
```

{% endcode %}

{% hint style="info" %}
`--bare` 会跳过 hooks、skills、plugins、MCP servers 和 CLAUDE.md 的自动发现（Claude 仍保留 Bash 以及文件读/写编辑），并且 `--exclude-dynamic-system-prompt-sections` 会把按机器区分的部分移出提示前缀。两者都会缩短提示并提升 KV-cache 复用，从而让本地模型明显更快。它们是可选的，不会改变上面的连接设置。
{% endhint %}

### 可选：调整 Unsloth 服务器

Claude Code 使用在 Unsloth 中运行的模型。你可以在启动服务器时自定义其行为。

```bash
# 为编程智能体提供服务：--disable-tools 会透传智能体自身的工具
unsloth run \
  --model unsloth/gemma-4-26B-A4B-it-GGUF:UD-Q4_K_XL \
  --disable-tools \
  --reasoning off \
  -p 8888
```

{% hint style="warning" %}
使用 `--disable-tools` 在驱动 Claude Code（或任何外部编程智能体）时使用。默认情况下，Unsloth Studio 会运行自己的服务器端工具，这会吞掉智能体的工具调用，因此 Claude Code 只会回答，却不会编辑文件。 `--disable-tools` 会切换为透传，因此将使用 Claude Code 自己的 Write/Edit/Bash 工具。
{% endhint %}

使用 `--reasoning off` 用于关闭思考，或使用 `--reasoning on` 为支持推理的模型开启它。

```bash
# 将 API 暴露到你的本地网络
unsloth run \
  --model unsloth/gemma-4-26B-A4B-it-GGUF:UD-Q4_K_XL \
  -H 0.0.0.0 \
  -p 8888
```

这将在 `0.0.0.0:8888`上启动服务器，允许本地网络上的其他设备连接。

使用 `-p` 用于更改服务器运行的端口。使用 `-H 0.0.0.0` 如果你希望网络中的手机、笔记本或其他设备能够连接。

如需更高级的运行时配置，请参阅主 [API 调优](https://unsloth.ai/docs/basics/api#unsloth-run-command) 部分。

## 🦙 Llama.cpp 教程

在开始之前，我们首先需要完成你将使用的特定模型的设置。我们使用 `llama.cpp` 它是一个开源框架，用于在你的 Mac、Linux、Windows 等设备上运行 LLM。Llama.cpp 包含 `llama-server` 它可让你高效地提供和部署 LLM。模型将运行在 8001 端口，所有智能体工具都会通过一个统一的 OpenAI 兼容端点路由。

#### Qwen3.5 教程

我们将使用 [Qwen3.5](/docs/zh/mo-xing/qwen3.5.md)-35B-A3B，以及用于快速准确编码任务的特定设置。如果你的 VRAM 不足，并且想要一个 **更智能的** 模型， **Qwen3.5-27B** 是一个很好的选择，但速度会慢约 2 倍；或者你也可以使用其他 Qwen3.5 变体，如 9B、4B 或 2B。

{% hint style="info" %}
如果你想要一个 **更智能的** 模型，或者你的 VRAM 不足，就使用 Qwen3.5-27B。不过它会比 35B-A3B 慢约 2 倍。或者你也可以使用 [**Qwen3-Coder-Next**](/docs/zh/mo-xing/qwen3-coder-next.md) 如果你的 VRAM 充足，它会非常出色。
{% endhint %}

{% stepper %}
{% step %}

#### 安装 llama.cpp

我们需要安装 `llama.cpp` 以便部署/提供本地 LLM，用于 Claude Code 等。我们遵循官方构建说明，以获得正确的 GPU 绑定和最高性能。修改 `-DGGML_CUDA=ON` 更改为 `-DGGML_CUDA=OFF` ，如果你没有 GPU，或者只想进行 CPU 推理。 **对于 Apple Mac / Metal 设备**，设置 `-DGGML_CUDA=OFF` ，然后像往常一样继续——Metal 支持默认开启。

```bash
apt-get update
apt-get install pciutils build-essential cmake curl libcurl4-openssl-dev git-all -y
git clone https://github.com/ggml-org/llama.cpp
cmake llama.cpp -B llama.cpp/build \\
    -DBUILD_SHARED_LIBS=OFF -DGGML_CUDA=ON
cmake --build llama.cpp/build --config Release -j --clean-first --target llama-cli llama-mtmd-cli llama-server llama-gguf-split
cp llama.cpp/build/bin/llama-* llama.cpp
```

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F4DmycqgjxOz6TOQd9PLJ%2Fimage.png?alt=media&amp;token=c94db0b5-8e4a-4043-b2a3-c68bad93213e" alt="" width="563"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 在本地下载并使用模型

通过以下方式下载模型 `huggingface_hub` 在 Python 中（通过 `pip install huggingface_hub hf_transfer`安装后）。我们使用 **UD-Q4\_K\_XL** 量化文件，以获得最佳的尺寸/精度平衡。你可以在我们的 [合集这里](/docs/zh/kuai-su-kai-shi/unsloth-model-catalog.md)。如果下载卡住，请查看 [Hugging Face Hub、XET 调试](/docs/zh/ji-chu-zhi-shi/troubleshooting-and-faqs/hugging-face-hub-xet-debugging.md)

```bash
hf download unsloth/Qwen3.5-35B-A3B-GGUF \\
    --local-dir unsloth/Qwen3.5-35B-A3B-GGUF \\
    --include "*UD-Q4_K_XL*" # 对于动态 2bit，请使用 "*UD-Q2_K_XL*"
```

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FRfXofrNzl1ypjfMTz15o%2Fimage.png?alt=media&amp;token=8009de90-cd11-46ed-85b5-fca5c07b66fc" alt=""><figcaption></figcaption></figure>

{% hint style="success" %}
我们使用了 `unsloth/Qwen3.5-35B-A3B-GGUF` ，但你也可以使用其他变体，如 27B，或任何其他模型，比如 `unsloth/`[`Qwen3-Coder-Next`](/docs/zh/mo-xing/qwen3-coder-next.md)`-GGUF`.
{% endhint %}

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FxlIrQGQ0cevb1ckkSFy5%2Fimage.png?alt=media&amp;token=b1a42562-927a-4ad2-85f8-29c2993c46aa" alt="" width="563"><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 启动 Llama-server

要为智能体工作负载部署 Qwen3.5，我们使用 `llama-server`。我们应用 [Qwen 推荐的采样参数](/docs/zh/mo-xing/qwen3.5.md#recommended-settings) 用于思考模式： `temp 0.6`, `top_p 0.95` , `top-k 20`。请记住，如果你使用非思考模式或其他任务，这些数值会变化。

在新的终端中运行此命令（可使用 `tmux` 或打开一个新的终端）。下面的配置应该 **完美适配 24GB GPU（RTX 4090）（占用 23GB）** `在……上可容纳` 也会自动卸载，但如果你看到性能不佳，请降低 `--ctx-size` .

{% hint style="info" %}
我们使用了 `--cache-type-k q8_0 --cache-type-v q8_0` 用于 KV cache 量化，以减少 VRAM 占用。若要使用全精度，请使用 `--cache-type-k bf16 --cache-type-v bf16` 。注意：bf16 KV Cache 在某些机器上可能会稍慢。
{% endhint %}

```bash
./llama.cpp/llama-server \\
    --model unsloth/Qwen3.5-35B-A3B-GGUF/Qwen3.5-35B-A3B-UD-Q4_K_XL.gguf \\
    --alias "unsloth/Qwen3.5-35B-A3B" \\
    --temp 0.6 \
    --top-p 0.95 \\
    --top-k 20 \\
    --min-p 0.00 \\
    --port 8001 \\
    --kv-unified \\
    --cache-type-k q8_0 --cache-type-v q8_0
```

{% hint style="success" %}
你也可以为 Qwen3.5 关闭思考，这会提升智能体编程任务的性能。要在 llama.cpp 中关闭思考，请把以下内容添加到 llama-server 命令中：

`--chat-template-kwargs "{\"enable_thinking\": false}"`

<img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F373wtRRbMcobtjV5e6xf%2Fkerkekke.png?alt=media&amp;token=2cd3b8c7-93b6-41cb-8bce-41f1aee819eb" alt="" data-size="original">
{% endhint %}
{% endstep %}
{% endstepper %}

### 使用 llama-server 启动 Claude Code

{% hint style="success" %}
我们使用了 `unsloth/GLM-4.7-Flash-GGUF` ，但你可以使用任何类似的 `unsloth/Qwen3.6-27B-GGUF`.
{% endhint %}

{% hint style="warning" %}
请先查看 [#fixing-90-slower-inference-in-claude-code](#fixing-90-slower-inference-in-claude-code "mention") ，先修复开源模型因 KV Cache 失效而慢 90% 的问题。
{% endhint %}

导航到你的项目文件夹（`mkdir project ; cd project`）并运行：

```bash
claude --model unsloth/GLM-4.7-Flash
```

要使用 Qwen3.6-35B-A3B，只需将其改为：

```bash
claude --model unsloth/Qwen3.6-35B-A3B
```

<div data-with-frame="true"><figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fnyc5BnXQiXPRZnyuYZt3%2Fimage.png?alt=media&amp;token=72011cb6-abed-4a41-99b0-104ef5d0111f" alt="" width="563"><figcaption></figcaption></figure></div>

若要将 Claude Code 设置为无需任何批准即可执行命令，请执行 **（注意：这会让 Claude Code 在无需任何批准的情况下随意执行代码！）**

{% code overflow="wrap" %}

```bash
claude --model unsloth/GLM-4.7-Flash --dangerously-skip-permissions
```

{% endcode %}

试试这个提示词来安装并运行一个简单的 Unsloth 微调：

{% code overflow="wrap" %}

```
你只能在 cwd project/ 中工作。不要搜索 CLAUDE.md——这里就是它。通过 uv 在虚拟环境中安装 Unsloth。尽可能使用 `python -m venv unsloth_env` 然后 `source unsloth_env/bin/activate`。关于方法请查看 https://unsloth.ai/docs/get-started/install/pip-install（获取并阅读）。然后按照 https://github.com/unslothai/unsloth 中的描述执行一个简单的 Unsloth 微调运行。你有 1 块 GPU 可用。
```

{% endcode %}

<div data-with-frame="true"><figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FBkpEsVssYZG9wHvvWMRH%2Fimage.png?alt=media&amp;token=e1a8283f-49ed-4b78-8052-d8970f069d5b" alt="" width="563"><figcaption></figcaption></figure></div>

稍等片刻后，Unsloth 会通过 uv 安装到虚拟环境中，并被加载：

<div data-with-frame="true"><figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FHATFwDrR1gP44XFbzWcv%2Fimage.png?alt=media&amp;token=6ff63733-686d-4b08-bdd5-66a6fa4aa34c" alt="" width="563"><figcaption></figcaption></figure></div>

最后你会看到一个使用 Unsloth 成功微调的模型！

<div data-with-frame="true"><figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FZjQ6askaixcYOMrr2qMi%2Fimage.png?alt=media&amp;token=e0e0047d-b6a2-421f-a86b-68e093a3a17a" alt="" width="563"><figcaption></figcaption></figure></div>

{% hint style="warning" %}
如果你看到 `无法连接到 API（ConnectionRefused）` ，记得取消设置 `ANTHROPIC_BASE_URL` 的形式通过 `unset ANTHROPIC_BASE_URL`

如果你发现开源模型慢 90%， [先看这里](#fixing-90-slower-inference-in-claude-code) 以修复 KV cache 失效的问题。
{% endhint %}

[^1]: 必须使用这个！


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://unsloth.ai/docs/zh/ji-chu-zhi-shi/claude-code.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
