Visual Studio Code has become the go-to environment for anyone who writes code — and increasingly, for people who are just starting out. It's free, it's well-designed, and once you've spent an hour in it, you start to understand why developers swear by it. If you're trying to use the Anthropic API and you've chosen VS Code as your editor, you've made a good choice.
This guide will walk you through the entire setup — from a completely blank computer to a working script that talks to Claude — using VS Code as your home base for every step. No prior programming experience assumed. Every term that needs explaining will be explained. Every screenshot-worthy moment will be described in plain language.
Before we go anywhere: this guide is for people who want to build something with Claude — automate a task, create a tool, connect it to something else. If you just want to chat with Claude, go to claude.ai and save yourself the setup entirely. The API is for programming, not for conversations.
What You'll Have by the End of This Guide
By the time you finish, you'll have:
- VS Code set up with Python support
- An Anthropic account with an API key
- A project folder with the Anthropic library installed
- A
.envfile storing your key safely - A working Python script that sends a message to Claude and prints the response
- A spending limit in place so nothing can surprise you on your bill
That's the foundation. Everything you'll ever build on top of the Anthropic API starts from exactly this setup.
Step 1: Install VS Code (If You Haven't Already)
Go to code.visualstudio.com and download the installer for your operating system — Windows, Mac, or Linux. Run it. The defaults are fine; you don't need to change anything during installation.
When VS Code opens for the first time, it looks a bit empty and slightly intimidating. That's normal. You're looking at a file explorer on the left (currently empty), a big blank editor area in the middle, and some icons on the far left sidebar. We'll use all of these, but right now there's only one thing you need to do: install the Python extension.
Install the Python Extension
On the far left sidebar, click the icon that looks like four squares — that's the Extensions panel. In the search box at the top, type Python. The first result should be "Python" published by Microsoft, with several million downloads. Click Install.
This extension is what gives VS Code the ability to understand Python code — it adds syntax highlighting (colors that make code easier to read), autocomplete suggestions as you type, error detection, and the ability to run your scripts with a single click.
Wait for it to install. It takes about 30 seconds. You'll see a small progress indicator. When it's done, you're ready for the next step.
Step 2: Install Python on Your Computer
VS Code is an editor — it's where you write code. But to actually run Python code, you need Python itself installed on your machine. These are two different things.
Go to python.org/downloads. The website detects your operating system automatically and offers you the right installer. Download it and run it.
One critical thing on Windows: at the very first screen of the installer, there's a small checkbox at the bottom that says "Add Python to PATH." Check this box before clicking Install Now. If you miss it and install without it checked, Python won't be accessible from the terminal — you'll need to uninstall and reinstall with the box checked. It's a frustrating mistake and a very common one.
On Mac, the installer is straightforward with no special options to watch out for.
Once installation finishes, verify it worked. In VS Code, open the integrated terminal by pressing Ctrl + ` (that's Ctrl plus the backtick key, the one to the left of the 1 key). A terminal panel will appear at the bottom of VS Code. Type this and press Enter:
python --version
You should see something like Python 3.12.3. Any version starting with 3 is fine. If you see an error instead, the PATH step was missed during installation — reinstall Python with that checkbox ticked.
The integrated terminal in VS Code is one of its most useful features. It's a full command-line terminal built right into the editor, so you never need to switch windows. You'll use it for installing packages, running scripts, and checking output. Ctrl + ` opens and closes it.
Step 3: Create Your Project Folder
Good habits start early. Rather than saving scripts to your Desktop or Downloads folder, create a dedicated folder for this project. It'll be much easier to manage as you build more things.
Create a new folder anywhere on your computer — Documents, Desktop, wherever you prefer. Name it something like claude-project or anthropic-test. The name doesn't matter, but avoid spaces in the folder name as they can cause occasional issues in terminals.
Now open this folder in VS Code. Go to File → Open Folder and select the folder you just created. VS Code will reload with your folder shown in the left-side file explorer. It'll be empty for now — that's expected.
Everything from this point forward — your scripts, your keys file, your installed packages — will live inside this folder. That's the project.
Step 4: Create a Virtual Environment
This step sounds more technical than it is. A virtual environment is a small, self-contained Python installation that lives inside your project folder. It means that any packages you install (like the Anthropic library) get installed only for this project, not globally on your entire computer. This keeps things tidy and avoids version conflicts if you work on multiple projects later.
In VS Code, open the integrated terminal (Ctrl + `). Make sure the terminal is open inside your project folder — the path shown in the terminal should end with your folder name. Then type:
python -m venv venv
Press Enter. You'll see nothing happen for a few seconds, then your cursor will return. That's success. Look in the left file explorer of VS Code — you'll see a new folder called venv has appeared inside your project. That's your virtual environment.
Now you need to activate it — tell your terminal to use this environment. The command differs by operating system:
On Windows:
venv\Scripts\activate
On Mac or Linux:
source venv/bin/activate
After running this, your terminal prompt will change slightly — you'll see (venv) at the beginning of the line. That's the signal that the virtual environment is active. Any packages you install from now on will go into this environment, not the global Python installation.
VS Code will probably notice the virtual environment automatically and ask you in a small pop-up whether you want to use it as your Python interpreter. Click Yes if it does — that lets VS Code's code editor know which Python to use when giving you suggestions and error highlights.
Step 5: Install the Anthropic Library
With the virtual environment active and the terminal open in VS Code, run this command:
pip install anthropic
You'll see text scrolling through the terminal as pip (Python's package installer) downloads and installs the Anthropic library and its dependencies. Wait for it to finish — it usually takes 20–30 seconds. When your prompt returns with (venv) still visible, it's done.
Also install a second small package called python-dotenv. This one handles loading your API key from a file, which we'll set up in the next step:
pip install python-dotenv
Two commands, two packages, the whole thing takes under a minute. That's your project dependencies sorted.
Step 6: Get Your Anthropic API Key
Go to console.anthropic.com in your browser. This is the Anthropic developer console — the dashboard where you manage your API access, monitor usage, and control billing.
If you don't have an account, sign up. It takes two minutes — you can use Google to sign in or create an account with an email address. You'll need to verify your email and probably your phone number too. That's normal.
Once you're in, click API Keys in the left navigation. Then click "Create Key." Give it a name like "VS Code project" — the name is just for your reference. Click Create.
When the key appears on screen, copy it immediately. It looks something like:
sk-ant-api03-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
This is the only time you'll ever see the full key. Once you close this page, it's gone. If you lose it, you have to create a new one. Keep it somewhere safe — a password manager is ideal.
Treat this key exactly like a password. If someone else gets it, they can use the API and the charges will appear on your account. Never share it, never post it publicly, and never write it directly into your code. Which brings us to the next step.
Step 7: Create a .env File to Store Your Key
The safest and most VS Code-friendly way to store your API key is in a .env file inside your project folder. A .env file is just a text file that stores secret values — it lives in your project but never gets shared or published.
In VS Code, right-click in the file explorer on the left and choose New File. Name it exactly: .env (yes, with the dot at the beginning and no extension after it). Press Enter.
The file will open in the editor. Type this exactly, replacing the placeholder with your actual API key:
ANTHROPIC_API_KEY=sk-ant-api03-your-actual-key-here
Save the file. That's it — your key is now stored locally in your project.
Now do one more important thing: create a file called .gitignore (again with the dot, again no extension). Inside it, write:
.env venv/
This file tells Git — the version control system — to never include your .env file or your venv folder if you ever share your project online (for example, on GitHub). Even if you're not using Git right now, it's a good habit to set this up from the start. API keys get scraped from public repositories within minutes.
Step 8: Set a Spending Limit Before You Write Any Code
Back in the Anthropic console at console.anthropic.com, go to Settings → Limits. Here you can set a hard monthly spending cap — a ceiling that the API simply won't go past, no matter what happens.
Set it to whatever feels comfortable. If you're just experimenting, $5 is more than enough — at current prices, $5 would let you send tens of thousands of short messages before running out. You can raise or lower this limit at any time.
You can also set a notification threshold — get an email when you've spent, say, 80% of your monthly limit. This gives you a heads-up before the cap kicks in.
Setting a spending limit is a 30-second step that removes all anxiety about "what if I mess something up." Do it now, before writing any code. You can always adjust it later.
Step 9: Write Your First Script
In VS Code, create a new file in your project folder. Right-click in the file explorer and choose New File. Name it hello_claude.py.
The file will open in the editor. Notice that VS Code already knows it's Python — it'll start offering autocomplete suggestions and syntax coloring as you type. Paste this into the file:
import anthropic
from dotenv import load_dotenv
load_dotenv()
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-haiku-4-5-20251001",
max_tokens=300,
messages=[
{
"role": "user",
"content": "Explain what an API is in two sentences. Keep it simple."
}
]
)
print(message.content[0].text)
Save the file with Ctrl + S (or Cmd + S on Mac). Now let's run it.
Step 10: Run the Script
There are two easy ways to run a Python script in VS Code:
Option A: The Play Button
Look at the top-right corner of the editor — there's a small triangle icon (a play button). Click it. VS Code will open a terminal at the bottom, activate the right Python environment, and run your script automatically. You'll see Claude's response appear in the terminal output.
Option B: The Integrated Terminal
Open the terminal with Ctrl + ` and make sure the (venv) prefix is visible in your prompt, meaning the virtual environment is active. Then type:
python hello_claude.py
Press Enter. After a second or two, Claude's response will appear in the terminal.
If everything worked, you'll see something like: "An API, or Application Programming Interface, is a set of rules that allows different software programs to communicate with each other. Think of it as a waiter in a restaurant — it takes your request, delivers it to the kitchen (the software), and brings back what you asked for."
That's it. You've just sent your first message to Claude from inside VS Code.
Understanding the Code, Line by Line
Let's break down exactly what each part of that script does. You don't need to memorize this — you can always come back here — but understanding it helps you modify the script for whatever you want to build.
import anthropic from dotenv import load_dotenv
These two lines load the tools you installed. anthropic is the library that knows how to talk to Claude. dotenv is the library that reads your .env file.
load_dotenv()
This reads your .env file and loads the ANTHROPIC_API_KEY value into memory. The next line (anthropic.Anthropic()) automatically finds this key — you never have to write the key itself in the code.
client = anthropic.Anthropic()
Creates a connection to the Anthropic API using your key. Think of this as opening the line of communication.
model="claude-haiku-4-5-20251001"
Specifies which Claude model to use. Haiku is the fastest and cheapest — perfect for testing and simple tasks.
max_tokens=300
Limits how long Claude's response can be. This is your most important cost-control lever — a response can't exceed this length, so costs stay predictable. Roughly 300 tokens means roughly 225 words of output.
messages=[{"role": "user", "content": "..."}]
The actual message you're sending. The role is always either "user" (you) or "assistant" (Claude's previous response, if you're building a multi-turn conversation). The content is the text of your message.
print(message.content[0].text)
Prints Claude's response to the terminal. message.content[0].text is how you extract the plain text from the API response object.
Every API call you'll ever make is a variation of this same structure. Change the model, the max_tokens, and the message content — the rest stays the same.
Understanding Tokens and Pricing
The API charges you based on tokens. A token is roughly three-quarters of a word in English. "Hello" is one token. "I need your help with something important" is eight tokens. A standard email is roughly 100–200 tokens. A full page of text is around 500–700 tokens.
You're charged for two directions: the tokens you send to Claude (your message, any instructions, any context you include) and the tokens Claude sends back (the response). Both directions cost money, but input is usually cheaper than output.
The Three Models
Anthropic offers three main models, each at a different price and capability level. Think economy, business, and first class — same airline, very different price tags.
Claude Haiku — Fast and Affordable
The smallest model. Quick responses, very cheap, genuinely capable for most everyday tasks. Classify a piece of text, summarize a short document, answer a factual question, extract data from a paragraph — Haiku handles all of this well. When you're testing or building something that will run frequently, start here and only upgrade if the output quality isn't good enough.
Claude Sonnet — The Balanced Choice
The middle model, and the one most production applications end up using. Noticeably smarter than Haiku for anything requiring nuanced reasoning, careful writing, or multi-step thinking. More expensive, but still very reasonable unless you're processing enormous volumes. This is the model powering most of Claude's consumer products.
Claude Opus — Maximum Capability
The most powerful model Anthropic makes. Also the most expensive by a significant margin. Use it when quality is the priority and you genuinely need the best possible output — complex analysis, high-stakes writing, difficult code. Don't reach for it by default; that's an expensive habit. For most tasks, Sonnet gets you 90% of the way there at a fraction of the cost.
What does it actually cost in practice?
To make this concrete: at Haiku prices, a typical API call — sending a paragraph and receiving a paragraph back — costs roughly one-tenth of a cent. A script that processes 500 documents in a batch might cost $1–2 total. A chatbot handling a few dozen conversations per day might cost $3–5 per month. Individual exploration and testing will likely cost you less than a cup of coffee per month.
The scenarios where costs scale up significantly are high-volume production applications, using Opus for tasks that don't require it, and forgetting to set max_tokens on calls that generate very long responses. All of these are avoidable.
Still weighing whether the API makes more financial sense than a monthly Claude subscription for your usage pattern? We've done that comparison in plain English — including the exact usage thresholds where each option wins.
Check the current prices at anthropic.com/pricing before building anything at scale. Prices shift as models improve and compute costs change — the concepts in this article stay true, but the specific numbers may not.
How to Keep Your Costs Under Control
Beyond the monthly spending limit you set in Step 8, here are the in-code habits that actually make a difference.
Always set max_tokens
Every single API call should have a max_tokens value. Without it, Claude will generate as long a response as it judges appropriate — which can be thousands of tokens if the prompt seems to call for it. That's fine for one call; multiplied across hundreds or thousands of calls, it adds up fast.
A rough guide for setting it:
- Classifications and yes/no answers: 20–50 tokens
- One-sentence summaries: 50–100 tokens
- Paragraphs, short responses: 200–400 tokens
- Emails, descriptions, explanations: 400–800 tokens
- Long documents, detailed analysis: 1,000–4,000 tokens
Use Haiku by default, upgrade only when needed
When you start building something new, use claude-haiku-4-5-20251001 for everything. Test your script, check the output quality. Only switch to Sonnet or Opus if Haiku genuinely isn't good enough for the task. You'll be surprised how often it is.
Test on small batches before running at scale
If you're about to process 1,000 files or rows of data, run the script on 5 first. Check that it works correctly, check the token usage reported in the API response, estimate the total cost. The API response object includes a usage field that shows you exactly how many input and output tokens each call consumed — use this before scaling up.
Watch the Usage dashboard in the console
Once a week while you're actively building, check console.anthropic.com → Usage. You'll see token consumption and cost graphs over time. A sudden spike you can't explain usually means a loop that ran more iterations than expected or a max_tokens value you forgot to set.
VS Code Features That Make API Development Easier
One reason VS Code is such a good environment for this kind of work is the set of features it adds on top of just editing text. A few that are genuinely useful when working with the Anthropic API:
Autocomplete and IntelliSense
With the Python extension installed, VS Code understands the code you're writing and offers suggestions as you type. When you type client.messages., it'll show you the available methods. When you're filling in the parameters of an API call, it'll often show you what the expected type and format is. This is enormously helpful when you're learning the API — you don't have to memorize everything or constantly check the documentation.
Inline error detection
If you make a mistake — a typo in a variable name, a missing colon, a wrong data type — VS Code will underline it in red before you even run the code. Click on the underlined text and it'll usually tell you exactly what's wrong. This saves a lot of "why isn't it running" time.
The integrated terminal
Keeping the terminal right inside VS Code — accessible with Ctrl + ` — means you can write code, save, switch to terminal, run it, and read the output without changing windows. It keeps your workflow tight and prevents the confusion of working across multiple applications.
Multiple terminals
You can open more than one terminal tab in VS Code. This is useful when you want to keep the virtual environment active in one terminal while using another for something else. Click the + icon in the terminal panel to add a new tab.
Split editor
You can split the VS Code editor to show two files side by side. Useful when you want to look at the Anthropic documentation (or this article) in one panel while editing your script in the other. Right-click on a file tab and choose "Split Right."
Your Project Folder Should Now Look Like This
After completing all the steps, your project folder structure should be:
claude-project/ ├── venv/ ← the virtual environment (don't touch this) ├── .env ← your API key (kept private) ├── .gitignore ← tells Git to ignore .env and venv └── hello_claude.py ← your first script
This is a clean, professional structure. Everything that should be private is protected. Everything that should be runnable is accessible. As you build more things, you'll add more .py files alongside hello_claude.py — the structure scales naturally.
Common Problems and How to Fix Them
A few things go wrong for almost everyone the first time. Here's how to deal with them without panicking.
"ModuleNotFoundError: No module named 'anthropic'"
This means the Anthropic library isn't installed in the Python environment you're currently using. The most common cause: you installed it in one terminal but VS Code is using a different Python interpreter. Check that (venv) is visible in your terminal prompt, and that VS Code's Python interpreter (bottom-right status bar) is pointing to the venv. If not, click the interpreter name in the status bar and select the one inside your venv folder.
"AuthenticationError" or "Invalid API key"
Your API key isn't being read correctly. Check three things: that your .env file is in the same folder as your script, that load_dotenv() appears before anthropic.Anthropic() in your code, and that there are no extra spaces around the = sign in the .env file. The format should be exactly ANTHROPIC_API_KEY=sk-ant-... with nothing else on that line.
"The terminal says (venv) but the script still fails"
If you're using the Play Button to run the script rather than the terminal, VS Code might be using a different Python than the one in your virtual environment. Click the Python interpreter shown in the bottom-right status bar of VS Code, then select "Enter interpreter path" and navigate to venv/Scripts/python.exe (Windows) or venv/bin/python (Mac/Linux).
"The script runs but prints nothing"
Check that you have print(message.content[0].text) at the end. If you have print(message) instead, you'll get a confusing object representation rather than the actual text. The path to the text is always message.content[0].text.
What to Build Next
Once you have this foundation working, the logical next step is to make the script do something actually useful. A few ideas that are achievable with a small extension of what you've just built:
- A document summarizer: Read a text file from your computer, send its contents to Claude, print the summary. Add
open("document.txt").read()to load local files. - A batch classifier: Give Claude a list of sentences and ask it to classify each one (positive/negative, spam/not spam, category A/B/C). Loop through a list in Python and call the API once per item.
- An interactive chatbot: Use a
while Trueloop and Python'sinput()function to take user input from the terminal, send it to Claude, and print the response. Keep the conversation history in themessageslist to preserve context. - A CSV processor: Read a spreadsheet with Python's
csvmodule, send each row to Claude with a prompt, and write the results to a new column. Useful for generating descriptions, extracting data, or scoring content.
Each of these is essentially the same script you already have, with a small addition. That's the nature of API development — once the scaffolding is in place, building new things on top of it is much faster than setting it all up in the first place.
If you'd also like to try DeepSeek — a cheaper alternative with similar capabilities for most everyday tasks — the setup process in VS Code is nearly identical.
The Setup I Actually Use
For what it's worth: this is exactly the stack I use for personal API projects. VS Code with the Python extension, a virtual environment per project, API keys in .env files, Haiku as the default model. My monthly spending limit in the Anthropic console is set to $20 — more than I've ever needed for personal experimentation, but low enough that any accidental loop would stop before causing a real problem.
The whole setup described in this guide took me about 25 minutes the first time, including troubleshooting the PATH checkbox I missed on Windows. The second project took 10 minutes because the steps were already familiar. By the third, it was muscle memory.
The goal was never to become a developer. It was to get to a place where "I want to build a script that does X with Claude" was a 20-minute job rather than a week-long project. That's what this setup gives you. The rest is just building things.