qwen-code with OpenRouter: force a provider and fix OpenAI connection error?
I run qwen-code against OpenRouter with
OPENAI_BASE_URLandOPENAI_API_KEY, and OpenRouter sometimes routesqwen/qwen3-coderto unexpected providers (e.g. XAI vs XAI-FAST, or cheaper fp8/fp4 variants), which changes cost and output quality. One person also gets[API Error: OpenAI API error: Connection error.]from qwen-code even though curl to the same OpenRouter endpoint works. Is there a supported way to pin or whitelist a provider, and what env-var configuration actually works?Originally asked on GitHub: https://github.com/QwenLM/qwen-code/issues/70
5 answers
Status: explained. The issue thread is closed, but no maintainer comment appears in it and the requested per-model provider pinning is not shown as implemented. The usable outcome is an env-var setup that worked for one user, plus a temporary routing workaround.
A commenter got qwen-code talking to OpenRouter with:
bashexport OPENAI_API_KEY="sk-or-v1-..." export OPENAI_BASE_URL="https://openrouter.ai/api/v1" export OPENAI_MODEL="qwen/qwen3-coder"then running
qwen. For provider choice, they pointed to OpenRouter's "default provider sort" setting as a temporary workaround, not a pin. Another user said that only gives category-level choices and cannot force a named provider like Alibaba, which was a showstopper for them. A separate commenter linked a wrapper script at https://github.com/richtong/bin/blob/main/qwen.sh (opens external site) (it depends on local./libhelpers), but it wasn't confirmed to fix anything.On the
[API Error: OpenAI API error: Connection error.]report: with a valid key, curl to https://openrouter.ai/api/v1/chat/completions (opens external site) forqwen/qwen3-coder:freeworked while qwen-code failed. The thread never diagnoses this. General advice: if curl works, re-checkOPENAI_BASE_URL(must end in/api/v1), the exact model slug, and that no other env vars override these.Security note: the
sk-or-v1-...key is a credential; keep it out of shared config files, screenshots, and git history.Did this work for you?What a GitHub user tried on GitHub (Outcome unclear)
Requested an OPENROUTER_PROVIDER_ORDER env var (e.g. "DeepInfra,Fireworks") in .zshrc so qwen-code would call a specific OpenRouter provider per model for cost control. No maintainer response confirmed this feature.
Source: https://github.com/QwenLM/qwen-code/issues/70 (opens external site)
Did this work for you?What @tne-ops tried on GitHub (Didn't work)
Ran qwen-code with a valid OpenRouter key and got
[API Error: OpenAI API error: Connection error.], while the same key worked with curl to https://openrouter.ai/api/v1/chat/completions (opens external site) usingqwen/qwen3-coder:free. No fix for this discrepancy is confirmed in the thread.Source: https://github.com/QwenLM/qwen-code/issues/70#issuecomment-3120848637 (opens external site)
Did this work for you?What @mrgoonie tried on GitHub (Worked)
Set
OPENAI_API_KEY="sk-or-v1-...",OPENAI_BASE_URL="https://openrouter.ai/api/v1", andOPENAI_MODEL="qwen/qwen3-coder". This worked for that user, and they suggested OpenRouter's default provider sort setting as a temporary routing control.Source: https://github.com/QwenLM/qwen-code/issues/70#issuecomment-3121582812 (opens external site)
Did this work for you?What @tne-ops tried on GitHub (Outcome unclear)
Shared a shell script wrapper (https://github.com/richtong/bin/blob/main/qwen.sh (opens external site)) that depends on local
./libhelper functions. No confirmation that it resolves the connection error or provider routing.Source: https://github.com/QwenLM/qwen-code/issues/70#issuecomment-3123366283 (opens external site)
Did this work for you?
Related questions
- Why did qwen-code burn millions of tokens and push my Alibaba Cloud account into debt?
- Qwen Code shows far fewer tokens than Bailian bills — how do I stop charges and get a refund?
- How do I enable Agentic Coding Protocol (ACP) support for qwen-code in Zed?
- Ollama qwen3-coder fails with "does not support tools" in qwen-code agent mode
- Does qwen-code support Anthropic-style sub-agents?