Invalid API Key 401 Error in VSCode, but it shows the correct API Key?

You do not need to put an API key environment variable “get” in the client creation. The OpenAI API automatically gets it.

It also gets from key files, so ensure you don’t have any extra file API keys around.

I suspect the API key is “invalid” because it is long and didn’t get set as an environment variable properly.

You can check what your code is using after making a call attempt: