Keeps getting 401 issue when using Azure instance even everything is being passed correctly

Resolved 💬 4 comments Opened Jun 1, 2025 by ajasingh Closed Aug 7, 2025

What version of Codex is running?

0.1.2505172129

Which model were you using?

gpt-4.1

What platform is your computer?

macos

What steps can reproduce the bug?

codex --provider azure --model gpt-4.1 "explain the code to me"

● OpenAI Codex (research preview) v0.1.2505172129 │
╰──────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────────────────────────╮
│ localhost session: ac4797c9e1fa44398e7035723217d606 │
│ ↳ workdir: ~/Work/loa-parser │
│ ↳ model: gpt-4.1 │
│ ↳ provider: azure │
│ ↳ approval: suggest │
╰──────────────────────────────────────────────────────────────╯
user
explain the code to me

system
⚠️ OpenAI rejected the request. Error details: Status: 401, Code: 401, Type: unknown, Message: 401 Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid
key for an active subscription and use a correct regional API endpoint for your resource.. Please verify your settings and try again.
export AZURE_OPENAI_API_KEY="***"
export AZURE_OPENAI_API_VERSION=2025-01-01-preview
export AZURE_BASE_URL=https://xxxx.openai.azure.com/openai

What is the expected behavior?

_No response_

What do you see instead?

_No response_

Additional information

_No response_

View original on GitHub ↗

4 Comments

camballe · 1 year ago

I'm also experiencing this. Somebody help please!

yapus · 1 year ago

JavaScript version fails with: OpenAI rejected the request. Error details: Status: 401, Code: 401, Type: unknown, Message: 401 Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource.. Please verify your settings and try again. and seams ignoring and required defining non-empty OPENAI_API_KEY even if alternate provider selected.

I'm using the following config.json for JavaScript CLI:

{
  "model": "gpt-4.5-preview",
  "provider": "azure",
  "providers": {
    "azure": {
      "name": "AzureOpenAI",
      "baseURL": "https://my-project-name-redacted.openai.azure.com/openai",
      "envKey": "AZURE_OPENAI_API_KEY"
    }
}

and surely define AZURE_OPENAI_API_KEY in the env.

Rust version fails with: { message: "stream error: unexpected status 404 Not Found: {\"error\":{\"code\":\"404\",\"message\": \"Resource not found\"}}.
For Rust-based CLI i use the following config:

model_provider = "azure"
model = "gpt-4.5-preview"
[model_providers.azure]
name = "AzureOpenAI"
base_url = "https://my-project-name-redacted.openai.azure.com/openai"
env_key = "AZURE_OPENAI_API_KEY"
wire_api = "chat"
jonmagic · 1 year ago

Yeah, I'm running into the second case explained above and consistently getting 404 errors.

I've got this ~/.codex/config.json (replacing <resource-name> with the correct value):

{
  "model": "gpt-4o",
  "provider": "azure",
  "providers": {
    "azure": {
      "name": "AzureOpenAI",
      "baseURL": "https://<resource-name>.openai.azure.com",
      "envKey": "AZURE_OPENAI_API_KEY"
    }
  }
}

I have these environment variables configured and ensured they are set in my env:

AZURE_OPENAI_API_VERSION
AZURE_OPENAI_API_KEY

And I'm using those with https://github.com/simonw/llm to ensure they work as expected. With codex I consistently get this:

➜ codex
(node:62182) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
╭──────────────────────────────────────────────────────────────╮
│ ● OpenAI Codex (research preview) v0.1.2505172129            │
╰──────────────────────────────────────────────────────────────╯
╭──────────────────────────────────────────────────────────────╮
│ localhost session: 04e871248f814052ba0d6cf2b5b42644          │
│ ↳ workdir: ~/code/project-foo                                │
│ ↳ model: gpt-4o                                              │
│ ↳ provider: azure                                            │
│ ↳ approval: suggest                                          │
╰──────────────────────────────────────────────────────────────╯
user
explain this codebase to me

    system
    ⚠️  OpenAI rejected the request. Error details: Status: 404, Code: 404, Type: unknown, Message: 404 Resource not found. Please verify your settings and
    try again.
codex-maintainers · 1 year ago

Thank you for the feedback! Please ensure you're using the latest version of Codex CLI. If the issue persists, try the native/Rust implementation of Codex CLI. If it still occurs, feel free to report back.