I’m working on an LLM prompt evaluation setup using Langchain and LangSmith. Everything worked 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 direct OpenAI chat completions. I also updated it in LangSmith through the playground settings under Secrets & API keys, and verified it appears correctly in my organization settings.
But when I run arun_on_dataset, I get this authentication error:
AuthenticationError: Error code: 401 - {'error': {'message': 'Incorrect API key provided: sk-X7QWC***************************************MKdP. 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 can’t figure out where this old key is still being referenced. My code hasn’t changed and worked before the key rotation.
Where else might the old API key be cached or stored that I need to update?
check if you’ve got multiple virtual envs running - sometimes the old key gets stuck in a different env than the one you’re updating. also, langsmith might be pulling from your system-wide openai config file (~/.openai or similar). try hardcoding the key directly in your code temporarily to see if that fixes it.
Been there, done that. LangSmith caches configs everywhere and it’s a nightmare tracking them all down manually.
Had this exact problem last month rotating our team’s API keys. Updated everything I could find, but the old key kept appearing in traces.
These tools have session states, cached configs, and sometimes store keys in temp files you don’t even know exist. Instead of hunting down where each service caches stuff, I automated the whole rotation process.
Built a workflow that updates keys everywhere at once - environment variables, LangSmith secrets, config files, plus restarts services. Takes 30 seconds now instead of hours troubleshooting.
Quick fix: clear your browser cache for LangSmith and restart your Python process completely. But honestly, automating this saves tons of time long-term.
This exact headache made me switch how I handle API keys in production.
LangSmith pulls keys from multiple sources at once. Your old key showing up means you’re missing part of the priority chain.
I’ve seen this when LangSmith uses the key from your LangChain hub settings instead of local environment. Check your LangChain hub account settings - there’s a separate API key section that overrides local configs.
If you’re using LangSmith client initialization in your code, it might read from a different config than you think. The client can pull from project-level configs, user-level configs, or cached auth tokens.
Set the key explicitly when you initialize your LangSmith client:
from langsmith import Client
client = Client(api_key="your_new_openai_key")
This bypasses all the automatic key discovery and forces it to use what you specify. Saved me hours debugging similar issues.
I experienced a similar problem recently, and it turned out that the old API key was being retained in a session. To resolve it, I logged out and back into LangSmith, which refreshed the cached credentials. Additionally, make sure to check any .env files in your project hierarchy or system environment variables that might conflict with your local settings. In my case, the OPENAI_API_KEY was set in my shell profile and took precedence over my project-specific .env file. Restarting your Python kernel or terminal is also essential to ensure the new environment variables are loaded properly.