Claude Code 如何透過 Ollama 使用本機及雲端大語言模型
Claude Code with Ollama: Run an AI Coding Agent on Local and Cloud LLMs
在 Windows 上透過 Ollama 連接 Claude Code,可讓它使用本機模型或用量有限的 Ollama 雲端模型,無需付費 Claude 訂閲。使用本機模型時,提示詞和程式碼留在自己的裝置上,速度與質素取決於硬件及模型大小;雲端模型能力較強但用量受限,提示詞會傳到 Ollama 伺服器。
The need
Claude Code is a powerful coding agent in the terminal, but by default it relies on a paid Claude account or API billing. I wanted to use it without depending on a single paid provider: running a model locally to keep my code on my machine, and falling back to free cloud models when a small local model is not enough.
What we will build
- Claude Code installed in the terminal.
- Ollama installed on Windows.
- Claude Code linked to a local LLM through Ollama.
- An alternative with Ollama cloud models (limited usage).
Prerequisites
- Windows with PowerShell.
- Node.js and npm.
- Enough disk space for the local model you choose.
1. Install Claude Code
Claude Code needs Node.js. Install it globally with npm and verify the version. (The PowerShell script from the docs failed on my machine, npm worked.)
On first launch, Claude Code asks how you want to log in: a Claude subscription, an Anthropic Console account (API billing), or a third-party platform. In the next steps we skip all of these and use Ollama instead.
2. Install Ollama
Download the Ollama installer for Windows, run it and click Install. Setup extracts the files and installs Ollama under your user folder. When the app opens, a welcome screen explains the idea: run open models with your coding agents. On the sign-up screen, choose “No thanks, I’ll use Ollama locally” so your prompts and code stay on your machine.
3. Link Claude Code with Ollama (local)
Ollama can launch Claude Code already wired to a model, with no manual configuration. Run the command below, pick a model from the list, then give Claude a task.
![]()
The list shows cloud models and local models, with the download size for each local one (for example qwen3.5:4b ~6 GB or gemma4:12b ~11 GB). Here I picked the tiny qwen3.5:0.8b. On the first launch, Claude Code asks you to choose a text style for your terminal (you can change it later with /theme).
Claude Code then starts on the local model. It may warn that the model is not recognized and assume a 200k-token context, and the header shows API Usage Billing because Claude Code does not know it is talking to Ollama. Your prompts and code never leave your machine, but speed and quality depend on your hardware and the size of the model.
4. Alternative: use Ollama cloud models (limited usage)
If your machine is too small for a good local model, sign in to Ollama from the terminal when asked; a browser page confirms Device Connected Successfully. The cloud models of the selection list (gemma4:31b-cloud, glm-5.3-flash:cloud, kimi-k3:cloud, deepseek-v4.1-flash:cloud) can then be used from Claude Code. They are far more capable than a tiny local model, but usage is capped.
⚠️ Cloud models run on Ollama’s servers, so your prompts leave your machine. Keep the local option for sensitive code.
Demo
A Java class generated through Claude Code on the Ollama cloud model gemma4:31b-cloud:
Local vs cloud: quick comparison
Local (Ollama): private, no usage cap, but limited by your hardware.
Cloud (Ollama): much more capable models, but usage is limited and prompts leave your machine.
Conclusion
With a single command, Claude Code runs on a local model or on an Ollama cloud model, without a paid Claude subscription. Start local to protect your code, and switch to cloud models when you need more power.
Which do you prefer for your coding agent: local or cloud models? Share your experience in the comments.
來源:DEV Community · dev.to












