Claude Code installation and usage guide: from npm install to your first prompt
Updated 2026-07-15
Installing Claude Code takes three steps: install Node.js 18 or newer, run npm install -g @anthropic-ai/claude-code, then set ANTHROPIC_AUTH_TOKEN and ANTHROPIC_BASE_URL before typing claude inside a project folder. The install itself takes under five minutes on macOS, Linux, or Windows, whether that is native PowerShell, Git Bash, or WSL. What actually takes practice is picking the right model for each task and reading the handful of error messages the CLI throws on a first run.
Quick answer: four steps from a blank terminal to a running session
Claude Code is a command-line tool, not a hosted app, so the whole install lives inside your terminal. First, confirm Node.js is at version 18 or newer; the CLI will not run reliably on anything older. Second, install the package globally with npm install -g @anthropic-ai/claude-code, which puts the claude binary on your PATH. Third, set two environment variables before launching it: ANTHROPIC_AUTH_TOKEN, the bearer credential for whichever backend you are calling, and ANTHROPIC_BASE_URL, the address of that backend. Fourth, cd into the project you want help with and type claude. The CLI then walks you through picking a color theme, accepting a safety notice, and confirming it can trust the current working directory, before dropping you into a chat prompt with read, edit, and terminal access scoped to that folder. Supported platforms are macOS, Linux, and Windows, with native support for PowerShell and Git Bash alongside the option of running inside WSL.
How Claude Code actually talks to a model backend
None of this changes what runs on your machine. Claude Code still reads files, edits them, and runs shell commands locally; only the model call itself leaves your machine, carrying whatever sits in the context window as input tokens. Point the base URL at a different backend and the local behavior, the commands, the file access, stays identical. What changes is where the bill gets calculated and at what rate.
- ANTHROPIC_BASE_URL is the only line standing between "talking to Anthropic directly" and "talking to anything else that speaks the same protocol." Claude Code does not hardcode a destination beyond this variable.
- ANTHROPIC_AUTH_TOKEN is sent as a bearer credential on every request to whatever ANTHROPIC_BASE_URL points at. A direct Anthropic key and a third-party gateway key go in the exact same field.
- ANTHROPIC_MODEL sets the default model for the main agent loop, an ID like claude-sonnet-4-6.
- ANTHROPIC_DEFAULT_OPUS_MODEL and ANTHROPIC_DEFAULT_SONNET_MODEL remap what "Opus" and "Sonnet" resolve to when Claude Code auto-picks a tier for a task, with no manual model switch mid-session.
- API_TIMEOUT_MS controls how long the CLI waits on a single response before giving up, worth raising on a slow connection or a heavily loaded backend.
Worked example: what your first real week costs, model by model
Installing Claude Code is free. Running it is not, and the size of the bill depends entirely on which model answers each call. A first work week of real use, not one-line questions but actual edits and debugging across a handful of files, lands around 3 million input tokens and 30,000 output tokens for most users. The table below prices that exact workload against three models at both direct list rates and a gateway's catalog rates, so the gap shows up as a dollar figure instead of a vague percentage.
| Model ID | Direct rate ($/1M in / out) | Gateway rate ($/1M in / out) | Cost for ~3M in / 30K out |
|---|---|---|---|
| claude-haiku-4-5 | $1.00 / $5.00 | $0.80 / $4.00 | $3.15 direct vs $2.52 gateway |
| claude-sonnet-4-6 | $3.00 / $15.00 | $2.40 / $12.00 | $9.45 direct vs $7.56 gateway |
| claude-opus-4-7 | $5.00 / $25.00 | $4.00 / $20.00 | $15.75 direct vs $12.60 gateway |
Why the setup trips people up
None of these are exotic bugs. They are the same five or six issues on repeat, and every one is fixable by checking one of three things: the Node version, whether the current shell session actually has both variables set, and whether the network in front of you allows outbound HTTPS to the backend you configured.
- An old Node.js binary fails quietly. Claude Code needs 18 or newer; on an older version the install can still complete and the claude command can still exist, but it crashes or misbehaves at launch.
- Environment variables set in one terminal tab do not follow you to a new tab, a new window, or a reboot. Exporting ANTHROPIC_AUTH_TOKEN once and closing the terminal is the single most common reason a working setup stops working the next day.
- Windows has three shells with three different syntaxes for the same variable: WSL bash uses export NAME=value, PowerShell uses $env:NAME = "value", and cmd uses set NAME=value. Mixing them up during copy-paste is a frequent mistake.
- "Invalid API Key, Please run /login" almost never means the key itself is wrong. It usually means Claude Code never saw ANTHROPIC_AUTH_TOKEN or ANTHROPIC_BASE_URL at all, because they were set in a shell session that already closed.
- An "offline" status inside the CLI is cosmetic more often than not. Claude Code separately runs a general connectivity check, and failing that check does not stop chat completions from working over ANTHROPIC_BASE_URL.
- Fetch and web-lookup tool calls fail before your actual model call even starts if the network path to that specific reachability check is blocked, which is common behind corporate proxies and some VPN configurations.
Two ways to point ANTHROPIC_BASE_URL
ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN are the only two settings that decide who gets paid and at what rate. Everything else about running Claude Code, the commands, the session behavior, the file access, stays the same no matter what you put in those two variables.
| Setup detail | Anthropic's own API | A third-party OpenAI-compatible gateway |
|---|---|---|
| Where the account lives | console.anthropic.com | Varies by provider; some skip account creation and email a key after a one-time payment |
| ANTHROPIC_BASE_URL value | https://api.anthropic.com (the default, can be left unset) | The provider's own domain, set explicitly |
| Model IDs | claude-sonnet-4-6, claude-opus-4-7, claude-haiku-4-5, unchanged | Same Claude model IDs, if the provider carries them, priced on its own rate card |
| Billing basis | Anthropic's published per-token list price | Whatever the provider lists, sometimes below official list, sometimes at parity |
| Subscription option | Pro and Max plans bundle a rate-limited Claude Code allowance | Pay-as-you-go only, no bundled plan tier to track |
Fixing the errors you'll actually hit
APIsRouter is one option built along those lines: an OpenAI-compatible gateway with pay-as-you-go billing and no subscription tier, where global models are priced 20% below official list, Chinese models sit below their own official rates, the first top-up adds a full balance match, and the /topup page takes payment before it asks for a signup, emailing the key right after. Nothing about the claude command or the workflow above changes; only the environment variable and the rate card do.
- Invalid API Key, Please run /login: close and reopen the terminal, re-export both variables, and confirm with echo $ANTHROPIC_AUTH_TOKEN (or $env:ANTHROPIC_AUTH_TOKEN on PowerShell) that the current shell actually has them set before typing claude again.
- "offline" status: ignore it if chat completions are otherwise responding normally. It reflects a general connectivity probe, not the health of your configured backend.
- Web fetch or browsing tool calls fail: this needs a stable outbound path to the reachability check Claude Code runs before browsing. Behind a restrictive network, allowing normal outbound HTTPS traffic, or adjusting a blocking VPN, usually clears it.
- "fetch failed" on otherwise ordinary requests: check DNS resolution for the domain in ANTHROPIC_BASE_URL and any firewall blocking outbound HTTPS first, then retry once the network path is confirmed open.
- Recurring API errors mid-session with no consistent pattern: exit with Ctrl+C and relaunch. A single flaky upstream response is common enough that a clean retry resolves most one-off errors without any config change.
- If the actual complaint is the size of the bill rather than an error message, the fix is a config change, not a retry: point ANTHROPIC_BASE_URL at a cheaper OpenAI-compatible endpoint and keep using the same model IDs and the same claude command.
Config example: environment variables and a smoke test
The three-shell dance above, export in bash, $env: in PowerShell, set in cmd, only lasts for the current terminal session. A more durable option is Claude Code's own settings file, read on every launch regardless of shell: put the two required variables there once, plus which model IDs answer each tier, and every new terminal picks them up without re-exporting anything. Verify a new key with a plain curl call before pointing a real session at it. A bad key fails the same way whether the mistake was a stray space or an expired token, and it is faster to find that out with curl than inside a half-started session.
{
"env": {
"ANTHROPIC_BASE_URL": "https://api.apisrouter.com",
"ANTHROPIC_AUTH_TOKEN": "sk-APIsRouter-...",
"ANTHROPIC_MODEL": "claude-sonnet-4-6",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "claude-opus-4-7",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-4-6",
"API_TIMEOUT_MS": "300000"
}
}FAQ
What are the system requirements for Claude Code?
Node.js version 18 or newer is the hard requirement; the installer and the CLI itself both run unreliably on older versions. Supported platforms are macOS, Linux, and Windows, with native support for PowerShell and Git Bash as well as the option to run inside WSL.
Why do I get 'Invalid API Key, Please run /login' after I already set my token?
This message means Claude Code never saw ANTHROPIC_AUTH_TOKEN or ANTHROPIC_BASE_URL in the current shell session, not that the key is wrong. The most common cause is exporting the variables in one terminal window and launching claude in a different one that never inherited them. Close and reopen the terminal, re-export both variables, or move them into a settings file so this stops recurring.
Why does Claude Code show an 'offline' status when it's clearly working?
That status comes from a general connectivity check the CLI runs on startup, separate from whichever backend ANTHROPIC_BASE_URL points at. If chat responses are coming back normally, the offline indicator is cosmetic and safe to ignore.
Why do web fetch and browsing tool calls fail inside Claude Code?
Claude Code runs a reachability check before letting a session use fetch or web-lookup tools, and that check needs its own stable outbound path. Behind a restrictive corporate network or certain VPN setups, that specific path gets blocked even though ordinary chat completions still work. Allowing normal outbound HTTPS traffic, or adjusting the VPN, usually resolves it.
What is the cheapest way to run Claude Code without an Anthropic subscription?
Skip the Pro or Max plan and pair Claude Code with a metered API key instead, since ANTHROPIC_BASE_URL accepts any backend that speaks the same protocol. APIsRouter is one option built that way: pay-as-you-go with no subscription, global models including the Claude family priced 20% below official list, and a /topup page that takes payment and emails the key without a signup form. A light user can cover weeks of sessions for less than one month of a bundled plan.
Can I switch Claude Code between Opus, Sonnet, and Haiku without redoing my whole config?
Yes. ANTHROPIC_MODEL sets the default the main agent loop uses, while ANTHROPIC_DEFAULT_OPUS_MODEL and ANTHROPIC_DEFAULT_SONNET_MODEL remap what those tiers resolve to. A common pattern keeps Sonnet as the everyday driver and reserves Opus for explicit hard architecture or debugging requests, with no changes needed mid-session.