I imported the notebook L3. When I run the cell chat_loop() I am getting following error "Error: “Could not resolve authentication method. Expected either api_key or auth_token to be set. Or for one of the X-Api-Key or Authorization headers to be explicitly omitted”
I’m guessing that are trying to run the code locally on your own machine. When you do so, you no longer have access to the course’s Anthropic API key to use their LLM. You need to create your own API key. Good practice is to store your key in an .env file. Then you can import it into python.
More details on Anthropic API key here:
docs.anthropic(dot)com/en/api/client-sdks
Here’s the code that Cursor generated to import the key in my .env file, which was stored in a higher directory (to keep it away from the Cursor chatbot). There are more elegant ways I’m sure.
# Load environment variables using absolute path
script_dir = os.path.dirname(os.path.abspath(__file__))
env_path = os.path.join(script_dir, '..', '..', '.env')
load_dotenv(env_path)
# Check if API key is available
if 'ANTHROPIC_API_KEY' not in os.environ:
print("❌ ANTHROPIC_API_KEY not found in environment variables")
print("Please make sure your .env file contains: ANTHROPIC_API_KEY=your_api_key_here")
exit(1)
print("✅ ANTHROPIC_API_KEY found in environment variables")
# Initialize Anthropic client
client = anthropic.Anthropic()