Create your keys for OpenAI and Anthropic
For the second-half of the course we will use large language models (LLMs) such as OpenAI’s GPT-4 and Anthropic’s Claude. To access these models programmatically, you will need to create API keys for both services and store them securely on your local machine.
tl:dr;
For each platform,
- Accept the email invitation to join the course organization on the platform.
- Create an API key in the appropriate workspace through the online platform.
- Store your API key securely on Posit Workbench (or your local computer) by adding it to your
.Renvironfile (R) or.env(Python).
OpenAI
We will use OpenAI’s API to programmatically interact with LLMs. To do this, you will need to generate an API key and store it in a secure location.
Create an OpenAI account. I have invited all students to join our OpenAI organization. It is just for students in this class and your accounts will be deactivated at the end of the semester. Look for an email from OpenAI to your Cornell email account inviting you join. Follow the instructions to create and activate your account through OpenAI’s website.
Generate an API key. Once you have an account, log in to OpenAI and navigate to the API keys. Make sure you are viewing the dashboard under our “INFO 4940” organization and the “INFO 4940 class usage” project. If that is not the default setting, use the dropdown menus to adjust this. You will see a button on the right side of the screen to generate an API key. Click the button.
Give your key a name and make sure you have selected the “INFO 4940 class usage” project. This is the only way to ensure your API usage is charged to the course and not you individually. Click “Create secret key”.
Save your key. You will see a screen with your API key. It is a long string of characters. Copy this key to your clipboard.
From your R console, run
usethis::edit_r_environ()This will open your global
.Renvironfile in Positron. Add a line to this file that looks like this:OPENAI_API_KEY="YOUR-OPENAI-TOKEN"Replace
YOUR-OPENAI-TOKENwith the copied token from OpenAI. Save the file and restart your R session. You should now be able to access your API key in R by runningSys.getenv("OPENAI_API_KEY")Once the key is stored here, every workspace in Positron should automatically utilize the key.
You will need to store your key in each project directory you createUnlike R, Python does not have a built-in way to manage environment variables. Instead, we will use a
.envfile in your project directory to store the key. I recommend create this file in a single location (perhaps your user directory/home/<netid>/) which you can then copy/paste into each new project directory you create.If you feel comfortable and know how to do it, you can also store the API key as an environmental variable using your operating system’s appropriate procedure.
Create a new plaintext file using Positron.
Name the file
.env(note the leading period).Add a line to this file that looks like this:
OPENAI_API_KEY="YOUR-OPENAI-TOKEN"replacing
YOUR-OPENAI-TOKENwith the copied token from OpenAI. Save the file.For future assignments in the class that use the OpenAI API, copy/paste the contents of this
.envfile into the project directory you are working in another file called.env. At the beginning of any Python script which needs to access the API key, add the following lines of code:import dotenv dotenv.load_dotenv()This will automatically load the API key from the
.envfile into your Python environment.
Anthropic
Create an Anthropic account. I have invited all students to join our Anthropic organization. It is just for students in this class and your accounts will be deactivated at the end of the semester. Look for an email from Anthropic to your Cornell email account inviting you join. Follow the instructions to create and activate your account through Anthropic’s website.
Generate an API key. Once you have an account, log in to Claude Console and navigate to the API keys. Generate an API key within the INFO 4940/5940 workspace.1
Give your key a name and click Add.
Save your key. You will see a screen with your API key. It is a long string of characters. Copy this key to your clipboard and store it the same way as for OpenAI, except it should be stored under the name
ANTHROPIC_API_KEY.
Footnotes
Unlike OpenAI, you should not have the option to create it in the default workspace.↩︎



