Getting old API key error after updating OpenAI credentials in LangSmith

I’m working with LangChain and LangSmith to test different prompts for my language model. Everything was working fine until I had to regenerate my OpenAI API key.

I updated the new key in my environment variables and confirmed it works by testing with OpenAI’s chat completion directly. I also went to LangSmith and updated the key in the playground settings under Secrets & API keys, plus verified it shows correctly in my organization settings.

But when I run arun_on_dataset in my Python script, I get this authentication error:

AuthenticationError("Error code: 401 - {'error': {'message': 'Incorrect API key provided: sk-M9XWA***************************************QpRt. You can find your API key at https://platform.openai.com/account/api-keys."})

The error shows my old API key prefix, not the new one I set everywhere. My code hasn’t changed at all, just the API key externally.

Where else might the old key be cached or stored? Any suggestions would be helpful!

it sounds like langsmith could be holding onto the old key. try logging out and in again, that often helps. also check your .env files in the project directory - sometimes the old key sticks around there.

Same thing happened to me! LangSmith stores API keys in multiple spots, so even if you updated the playground settings, the old key might still be lurking somewhere else. Check your project settings and workspace configs - they could still have the old key. LangSmith also caches credentials at the session level. If you’re using Jupyter, restart your Python kernel completely. Otherwise, kill any Python processes that might have the old key loaded in memory. Also worth checking - your LangChain client might be initializing with cached credentials from before. Clear your browser cache for LangSmith too since the web interface sometimes hangs onto old auth tokens.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.