Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 219 additions & 0 deletions README-LMSTUDIO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
# LM Studio Integration with Claude Code Router

This guide explains how to integrate Claude Code Router (CCR) with LM Studio to use local models with Claude Code.

## Prerequisites

1. [LM Studio](https://lmstudio.ai/) installed and running
2. [Claude Code](https://docs.anthropic.com/en/docs/claude-code/quickstart) installed
3. Claude Code Router installed: `npm install -g @musistudio/claude-code-router`

## Quick Setup

1. **Start LM Studio**:
- Open LM Studio
- Load a model (e.g., `google/gemma-3n-e4b`)
- Start the local server (typically on `http://localhost:1234`)

2. **Configure CCR for LM Studio**:
```bash
./setup-lmstudio.sh
```

3. **Start CCR**:
```bash
ccr start
```

4. **Use Claude Code with LM Studio**:
```bash
ccr code "What is the capital of France?"
```

## Manual Configuration

If you prefer to configure manually, create `~/.claude-code-router/config.json`:

```json
{
"Providers": [
{
"name": "lmstudio",
"api_base_url": "http://localhost:1234/v1/chat/completions",
"api_key": "lm-studio",
"models": ["your-model-name"]
}
],
"Router": {
"default": "lmstudio,your-model-name",
"background": "lmstudio,your-model-name",
"think": "lmstudio,your-model-name",
"longContext": "lmstudio,your-model-name",
"longContextThreshold": 60000,
"webSearch": "lmstudio,your-model-name"
},
"API_TIMEOUT_MS": 600000,
"LOG": true
}
```

## Configuration Options

### Model Configuration
Update the `models` array with your loaded LM Studio model:
```json
"models": ["microsoft/DialoGPT-medium", "meta-llama/Llama-2-7b-chat-hf"]
```

### Custom API Endpoint
If LM Studio runs on a different port:
```json
"api_base_url": "http://localhost:8080/v1/chat/completions"
```

### Multiple Models Setup
Configure different models for different tasks:
```json
{
"Providers": [
{
"name": "lmstudio",
"api_base_url": "http://localhost:1234/v1/chat/completions",
"api_key": "lm-studio",
"models": ["google/gemma-3n-e4b", "microsoft/DialoGPT-medium"]
}
],
"Router": {
"default": "lmstudio,google/gemma-3n-e4b",
"background": "lmstudio,microsoft/DialoGPT-medium",
"think": "lmstudio,google/gemma-3n-e4b"
}
}
```

## Advanced Configuration

### Adding Transformers
While LM Studio doesn't typically need transformers (it's OpenAI-compatible), you can add them for custom behavior:

```json
{
"name": "lmstudio",
"api_base_url": "http://localhost:1234/v1/chat/completions",
"api_key": "lm-studio",
"models": ["google/gemma-3n-e4b"],
"transformer": {
"use": [
[
"maxtoken",
{
"max_tokens": 4096
}
]
]
}
}
```

### Custom Router
Create a custom router for LM Studio specific logic:

```javascript
// ~/.claude-code-router/custom-lmstudio-router.js
module.exports = async function router(req, config) {
const userMessage = req.body.messages.find((m) => m.role === "user")?.content;

// Use a specific model for code-related queries
if (userMessage && userMessage.toLowerCase().includes("code")) {
return "lmstudio,google/gemma-3n-e4b";
}

// Default to configured router
return null;
};
```

Then in your config:
```json
{
"CUSTOM_ROUTER_PATH": "$HOME/.claude-code-router/custom-lmstudio-router.js"
}
```

## Testing Your Setup

1. **Test basic functionality**:
```bash
ccr code "Hello, how are you?"
```

2. **Test with longer context**:
```bash
ccr code "Explain the concept of machine learning in detail"
```

3. **Check routing**:
```bash
ccr status
```

## Troubleshooting

### Common Issues

1. **Connection refused**: Make sure LM Studio server is running
2. **Model not found**: Ensure the model name in config matches the loaded model in LM Studio
3. **Timeout errors**: Increase `API_TIMEOUT_MS` in config for slower local models

### Debug Mode
Enable logging for troubleshooting:
```json
{
"LOG": true
}
```

View logs:
```bash
tail -f ~/.claude-code-router.log
```

### Testing LM Studio API Directly
Test the LM Studio API directly with curl (same as your original command):

```bash
curl http://localhost:1234/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "google/gemma-3n-e4b",
"messages": [
{ "role": "system", "content": "Always answer in rhymes. Today is Thursday" },
{ "role": "user", "content": "What day is it today?" }
],
"temperature": 0.7,
"max_tokens": -1,
"stream": false
}'
```

## Benefits of Using CCR with LM Studio

1. **Unified Interface**: Use Claude Code with local models
2. **Routing Logic**: Automatically route different types of requests to appropriate models
3. **Easy Model Switching**: Switch between local and cloud models seamlessly
4. **Cost Effective**: Use local models for development and testing
5. **Privacy**: Keep sensitive code on your local machine

## Example Workflows

### Development Workflow
```bash
# Use local model for quick iterations
ccr code "Review this Python function for bugs"

# Switch to cloud model for complex tasks
/model openrouter,anthropic/claude-3.5-sonnet
```

### Mixed Model Setup
Configure different models for different scenarios in your config.json and let CCR automatically route requests based on context, token count, and task type.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
## ✨ Features

- **Model Routing**: Route requests to different models based on your needs (e.g., background tasks, thinking, long context).
- **Multi-Provider Support**: Supports various model providers like OpenRouter, DeepSeek, Ollama, Gemini, Volcengine, and SiliconFlow.
- **Multi-Provider Support**: Supports various model providers like OpenRouter, DeepSeek, Ollama, Gemini, Volcengine, SiliconFlow, and LM Studio.
- **Request/Response Transformation**: Customize requests and responses for different providers using transformers.
- **Dynamic Model Switching**: Switch models on-the-fly within Claude Code using the `/model` command.
- **GitHub Actions Integration**: Trigger Claude Code tasks in your GitHub workflows.
Expand Down Expand Up @@ -151,6 +151,12 @@ Here is a comprehensive example:
"claude-opus-4-20250514",
"gemini-2.5-pro"
]
},
{
"name": "lmstudio",
"api_base_url": "http://localhost:1234/v1/chat/completions",
"api_key": "lm-studio",
"models": ["google/gemma-3n-e4b"]
}
],
"Router": {
Expand Down
10 changes: 8 additions & 2 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,17 @@
"enhancetool"
]
}
},
{
"name": "lmstudio",
"api_base_url": "http://localhost:1234/v1/chat/completions",
"api_key": "lm-studio",
"models": ["google/gemma-3n-e4b"]
}
],
"Router": {
"default": "deepseek,deepseek-chat",
"background": "ollama,qwen2.5-coder:latest",
"default": "lmstudio,google/gemma-3n-e4b",
"background": "lmstudio,google/gemma-3n-e4b",
"think": "deepseek,deepseek-reasoner",
"longContext": "openrouter,google/gemini-2.5-pro-preview",
"longContextThreshold": 60000,
Expand Down
20 changes: 20 additions & 0 deletions config.lmstudio.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"Providers": [
{
"name": "lmstudio",
"api_base_url": "http://localhost:1234/v1/chat/completions",
"api_key": "lm-studio",
"models": ["google/gemma-3n-e4b"]
}
],
"Router": {
"default": "lmstudio,google/gemma-3n-e4b",
"background": "lmstudio,google/gemma-3n-e4b",
"think": "lmstudio,google/gemma-3n-e4b",
"longContext": "lmstudio,google/gemma-3n-e4b",
"longContextThreshold": 60000,
"webSearch": "lmstudio,google/gemma-3n-e4b"
},
"API_TIMEOUT_MS": 600000,
"LOG": true
}
25 changes: 25 additions & 0 deletions setup-lmstudio.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# Setup script for integrating Claude Code Router with LM Studio
echo "Setting up Claude Code Router for LM Studio..."

# Create .claude-code-router directory if it doesn't exist
mkdir -p "$HOME/.claude-code-router"

# Copy the LM Studio configuration
echo "Copying LM Studio configuration..."
cp config.lmstudio.json "$HOME/.claude-code-router/config.json"

echo "Configuration copied to $HOME/.claude-code-router/config.json"
echo ""
echo "To use CCR with LM Studio:"
echo "1. Make sure LM Studio is running on http://localhost:1234"
echo "2. Load the model 'google/gemma-3n-e4b' in LM Studio"
echo "3. Run: ccr start"
echo "4. Run: ccr code 'your prompt here'"
echo ""
echo "You can also edit the configuration at:"
echo "$HOME/.claude-code-router/config.json"
echo ""
echo "To change the model, update the 'models' array in the lmstudio provider"
echo "and update the Router section accordingly."
Loading