Qwen OpenAI-Compatible Proxy Server - Works with opencode, crush, claude code router, roo code, cline mostly everything
A proxy server that exposes Qwen models through an OpenAI-compatible API endpoint. Has tool calling and streaming support.
Users might face errors or 504 Gateway Timeout issues when using contexts with 130,000 to 150,000 tokens or more. This appears to be a practical limit for Qwen models. Qwen code itself tends to also break down and get stuck on this limit.
-
Install Dependencies:
npm install
-
Authenticate: You need to authenticate with Qwen to generate the required credentials file.
- Run
npm run auth:add <account>
to authenticate with your Qwen account - This will create the
~/.qwen/oauth_creds.json
file needed by the proxy server - Alternatively, you can use the official
qwen-code
CLI tool from QwenLM/qwen-code
- Run
-
Start the Server:
npm start
-
Use the Proxy: Point your OpenAI-compatible client to
http://localhost:8080/v1
. -
API key? Random doesn't matter.
The proxy supports multiple Qwen accounts to overcome the 2,000 requests per day limit per account. Accounts are automatically rotated when quota limits are reached.
-
List existing accounts:
npm run auth:list
-
Add a new account:
npm run auth:add <account-id>
Replace
<account-id>
with a unique identifier for your account (e.g.,account2
,team-account
, etc.) -
Remove an account:
npm run auth:remove <account-id>
- When you have multiple accounts configured, the proxy will automatically rotate between them
- Each account has a 2,000 request per day limit
- When an account reaches its limit, Qwen's API will return a quota exceeded error
- The proxy detects these quota errors and automatically switches to the next available account
- If a DEFAULT_ACCOUNT is configured, the proxy will use that account first before rotating to others
- Request counts are tracked locally and reset daily at UTC midnight
- You can check request counts for all accounts with:
npm run auth:counts
The proxy provides real-time feedback in the terminal:
- Shows which account is being used for each request
- Displays current request count for each account
- Notifies when an account is rotated due to quota limits
- Indicates which account will be tried next during rotation
- Shows which account is configured as the default account on server startup
- Marks the default account in the account list display
The proxy server can be configured using environment variables. Create a .env
file in the project root or set the variables directly in your environment.
LOG_FILE_LIMIT
: Maximum number of debug log files to keep (default: 20)DEBUG_LOG
: Set totrue
to enable debug logging (default: false)STREAM
: Set totrue
to enable streaming responses (default: false)- Important: Set this to
true
when using tools like opencode or crush that require streaming responses
- Important: Set this to
DEFAULT_ACCOUNT
: Specify which account the proxy should use by default (when using multi-account setup)- Should match the name used when adding an account with
npm run auth add <name>
- If not set or invalid, the proxy will use the first available account
- Should match the name used when adding an account with
Example .env
file:
# Keep only the 10 most recent log files
LOG_FILE_LIMIT=10
# Enable debug logging (log files will be created)
DEBUG_LOG=true
# Enable streaming responses (disabled by default)
# Required for tools like opencode and crush
STREAM=true
# Specify which account to use by default (when using multi-account setup)
# Should match the name used when adding an account with 'npm run auth add <name>'
DEFAULT_ACCOUNT=my-primary-account
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: 'fake-key', // Not used, but required by the OpenAI client
baseURL: 'http://localhost:8080/v1'
});
async function main() {
const response = await openai.chat.completions.create({
model: 'qwen-coder-plus',
messages: [
{ "role": "user", "content": "Hello!" }
]
});
console.log(response.choices[0].message.content);
}
main();
POST /v1/chat/completions
This proxy server supports tool calling functionality, allowing you to use it with tools like opencode and crush roo cline kilo and etc .
To use with opencode, add the following to ~/.config/opencode/opencode.json
:
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"myprovider": {
"npm": "@ai-sdk/openai-compatible",
"name": "proxy",
"options": {
"baseURL": "http://localhost:8080/v1"
},
"models": {
"qwen3-coder-plus": {
"name": "qwen3"
}
}
}
}
}
Note: For opencode to work properly with streaming responses, you need to enable streaming in the proxy server by setting STREAM=true
in your .env
file.
To use with crush, add the following to ~/.config/crush/crush.json
:
{
"$schema": "https://charm.land/crush.json",
"providers": {
"proxy": {
"type": "openai",
"base_url": "http://localhost:8080/v1",
"api_key": "",
"models": [
{
"id": "qwen3-coder-plus",
"name": "qwen3-coder-plus",
"cost_per_1m_in": 0.0,
"cost_per_1m_out": 0.0,
"cost_per_1m_in_cached": 0,
"cost_per_1m_out_cached": 0,
"context_window": 150000,
"default_max_tokens": 64000
}
]
}
}
}
Note: For crush to work properly with streaming responses, you need to enable streaming in the proxy server by setting STREAM=true
in your .env
file.
{
"LOG": false,
"Providers": [
{
"name": "qwen-code",
"api_base_url": "http://localhost:8080/v1/chat/completions/",
"api_key": "wdadwa-random-stuff",
"models": ["qwen3-coder-plus"],
"transformer": {
"use": [
[
"maxtoken",
{
"max_tokens": 65536
}
],
"enhancetool",
"cleancache"
]
}
}
],
"Router": {
"default": "qwen-code,qwen3-coder-plus"
}
}
To use with Roo Code or Kilo Code or Cline :
- Go to settings in the client
- Choose the OpenAI compatible option
- Set the URL to:
http://localhost:8080/v1
- Use a random API key (it doesn't matter)
- Type or choose the model name exactly as:
qwen3-coder-plus
- Disable streaming in the checkbox for Roo Code or Kilo Code
- Change the max output setting from -1 to 65000
- You can change the context window size to around 300k or so but after 150k it gets slower so keep that in mind .
The proxy now displays token counts in the terminal for each request, showing both input tokens and API-returned usage statistics (prompt, completion, and total tokens).
The proxy includes comprehensive token usage tracking that monitors daily input and output token consumption across all accounts. View detailed token usage reports with either:
npm run auth:tokens
or
npm run tokens
Both commands display a clean table showing daily token usage trends, lifetime totals, and request counts. For more information, see docs/token-usage-tracking.md
.
For more detailed documentation, see the docs/
directory.
For information about configuring a default account, see docs/default-account.md
.