openclaw - 💡(How to fix) Fix [Bug] Persistent HTTP 401 with Valid Aliyun ModelStudio Key (Direct API Calls Work, CLI Fails) [4 comments, 5 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#51452Fetched 2026-04-08 01:11:05
View on GitHub
Comments
4
Participants
5
Timeline
7
Reactions
0
Timeline (top)
commented ×4labeled ×2cross-referenced ×1

Error Message

I am encountering a persistent HTTP 401: Incorrect API key provided error when running OpenClaw with Aliyun ModelStudio (DashScope), despite the API key being fully valid and functional in direct API calls.

📜 Error Logs

[agent/embedded] embedded run agent end: runId=... isError=true model=qwen-plus-2025-07-28 provider=modelstudio error=HTTP 401: Incorrect API key provided. For details, see: https://help.aliyun.com/zh/model-studio/error-code#apikey-error

Code Example

[gateway] agent model: modelstudio/qwen-plus-2025-07-28
...
[agent/embedded] embedded run agent end: runId=... isError=true model=qwen-plus-2025-07-28 provider=modelstudio error=HTTP 401: Incorrect API key provided. For details, see: https://help.aliyun.com/zh/model-studio/error-code#apikey-error
[agent/embedded] auth profile failure state updated: ... reason=auth window=cooldown

### OpenClaw version

2026.3.13 (61d171a)

### Operating system

Windows 11 (Build 22631+)

### Install method

npm

### Model

Model ID: qwen-plus-2025-07-28

### Provider / routing chain

Provider: Aliyun ModelStudio (DashScope)

### Additional provider/model setup details

Base URL: https://dashscope.aliyuncs.com/compatible-mode/v1
Auth Method: API Key (Bearer Token)

### Logs, screenshots, and evidence
RAW_BUFFERClick to expand / collapse

Bug type

Behavior bug (incorrect output/state without crash)

Summary

🐛 Describe the Bug

I am encountering a persistent HTTP 401: Incorrect API key provided error when running OpenClaw with Aliyun ModelStudio (DashScope), despite the API key being fully valid and functional in direct API calls.

The issue occurs consistently across multiple troubleshooting steps, suggesting a potential bug in how OpenClaw constructs the authentication header or handles the specific Aliyun OpenAI-compatible endpoint.

✅ Verification: API Key is Valid

I have confirmed the API key works perfectly outside of OpenClaw:

  • Direct PowerShell Test: Successfully invoked qwen-plus-2025-07-28 using Invoke-RestMethod with the exact same Base URL and API Key.
  • Response: Received valid JSON completions without any auth errors.
  • Key Format: The key starts with sk- and contains no visible spaces or special characters.

Steps to reproduce

🔁 Steps Taken (All Failed)

I have exhausted the following standard troubleshooting steps with no success:

  1. Config Update: Verified openclaw.json has the correct baseUrl (https://dashscope.aliyuncs.com/compatible-mode/v1) and apiKey.
  2. Cache Clearing: Deleted the entire .openclaw/state directory to force a fresh auth profile creation.
  3. Environment Variables: Attempted to force the key via $env:DASHSCOPE_API_KEY and $env:OPENAI_API_KEY, bypassing the config file entirely.
  4. Model Variation: Tried switching between qwen-plus-2025-07-28 and qwen-turbo with identical results.
  5. Re-installation: Ensured running the latest version (2026.3.13).

openclaw-2026-03-21.log

Expected behavior

connection established from Alibaba model studio and reply my request

Actual behavior

📜 Error Logs

Gateway Log Snippet:

[gateway] agent model: modelstudio/qwen-plus-2025-07-28
...
[agent/embedded] embedded run agent end: runId=... isError=true model=qwen-plus-2025-07-28 provider=modelstudio error=HTTP 401: Incorrect API key provided. For details, see: https://help.aliyun.com/zh/model-studio/error-code#apikey-error
[agent/embedded] auth profile failure state updated: ... reason=auth window=cooldown

### OpenClaw version

2026.3.13 (61d171a)

### Operating system

Windows 11 (Build 22631+)

### Install method

npm

### Model

Model ID: qwen-plus-2025-07-28

### Provider / routing chain

Provider: Aliyun ModelStudio (DashScope)

### Additional provider/model setup details

Base URL: https://dashscope.aliyuncs.com/compatible-mode/v1
Auth Method: API Key (Bearer Token)

### Logs, screenshots, and evidence

```shell
attached in log

Impact and severity

annoying always failure

openclaw-2026-03-21.log

Additional information

Since direct API calls work but OpenClaw fails with 401, I suspect: Header Malformation: OpenClaw might be adding extra whitespace, newlines, or incorrect prefixes to the Authorization header when sending to the Aliyun endpoint. Endpoint Parsing: There might be a subtle incompatibility with how the Aliyun "OpenAI-compatible" mode parses requests from this specific client version. Encoding Issue: Potential character encoding mismatch for the API Key string internally within the TS/Node runtime. Could the maintainers please check the raw HTTP request headers generated by the modelstudio provider module? A comparison between a working curl/PowerShell request and the failing OpenClaw request would likely reveal the discrepancy. Thank you for your hard work on this amazing project!

extent analysis

Fix Plan

To resolve the HTTP 401: Incorrect API key provided error, we will focus on adjusting how OpenClaw constructs the authentication header for the Aliyun ModelStudio (DashScope) endpoint.

Step 1: Verify API Key Format

Ensure the API key starts with sk- and contains no visible spaces or special characters.

Step 2: Adjust Authentication Header Construction

Modify the OpenClaw code to correctly construct the Authorization header. The header should be in the format Authorization: Bearer YOUR_API_KEY.

Example code adjustment:

// Assuming this is how OpenClaw constructs its auth header
const authHeader = `Bearer ${apiKey}`;
// Ensure there are no leading/trailing spaces in apiKey
const trimmedApiKey = apiKey.trim();
const correctedAuthHeader = `Bearer ${trimmedApiKey}`;

// Use correctedAuthHeader in your request

Step 3: Check for Encoding Issues

Verify that the character encoding of the API key string matches what is expected by the Aliyun endpoint. If necessary, adjust the encoding to match.

Step 4: Compare Request Headers

Compare the raw HTTP request headers generated by OpenClaw with those from a working curl or PowerShell request to identify any discrepancies.

Verification

After applying these fixes, verify that OpenClaw can successfully authenticate with the Aliyun ModelStudio endpoint by checking for a successful response without the HTTP 401 error.

Extra Tips

  • Always ensure that API keys are handled securely and not exposed in plain text within code or logs.
  • Regularly review and update dependencies to prevent compatibility issues.
  • Utilize tools like curl or Postman to test API endpoints and compare requests for debugging purposes.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

connection established from Alibaba model studio and reply my request

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix [Bug] Persistent HTTP 401 with Valid Aliyun ModelStudio Key (Direct API Calls Work, CLI Fails) [4 comments, 5 participants]