Use the Claude command line interface (CLI) to interact with Anthropic's Claude models directly from your terminal, enabling quick prompts, script automation, and integration into workflows without opening a web browser.

What You'll Need

  • A terminal application (e.g., Terminal on macOS, Command Prompt or PowerShell on Windows, or any shell on Linux)
  • An active internet connection
  • An Anthropic API key with access to Claude models
  • Node.js (version 18 or later) installed on your system
  • The Claude CLI tool installed via npm

Step-by-Step

  1. Open your terminal application.
  2. Verify Node.js is installed by running node --version. If it returns a version number (e.g., v20.5.0), proceed. If not, download and install Node.js from the official website.
  3. Install the Claude CLI globally using npm by running: npm install -g @anthropic-ai/claude-cli. Wait for the installation to complete.
  4. Set your Anthropic API key as an environment variable. On macOS or Linux, run: export ANTHROPIC_API_KEY="your_api_key_here". On Windows in Command Prompt, use: set ANTHROPIC_API_KEY=your_api_key_here. In PowerShell, use: $env:ANTHROPIC_API_KEY="your_api_key_here". Replace "your_api_key_here" with your actual key from the Anthropic console.
  5. Test the CLI by running a simple prompt: claude -p "Hello, Claude!". You should see a response from the model in your terminal.
  6. To use a specific model version, add the --model flag. For example: claude -p "Explain quantum computing" --model claude-3-opus-20240229. Available models include claude-3-haiku-20240307, claude-3-sonnet-20240229, and claude-3-opus-20240229.
  7. Save a prompt to a file and run it via the CLI. Create a file named prompt.txt with your question, then run: claude -f prompt.txt.
  8. For continuous interaction, start an interactive session with: claude --chat. Type your prompts and press Enter to receive responses. Type exit or press Ctrl+D to end the session.
  9. To output results in JSON format for scripting, use the --format json flag: claude -p "What is 2+2?" --format json. This returns structured data including the response, model used, and token counts.

Common Mistakes

  • Forgetting to set the ANTHROPIC_API_KEY environment variable, which results in authentication errors.
  • Using an outdated version of Node.js that lacks support for modern JavaScript features required by the CLI.
  • Attempting to use model names that are incorrect or deprecated, such as "claude-2" instead of the current Claude 3 variants.
  • Running the CLI without internet access, which prevents communication with Anthropic's servers.
  • Misplacing quotes around the API key or prompt, especially on Windows where quoting rules differ between shells.

Tips

  • Add the export command for your API key to your shell profile (e.g., ~/.bashrc, ~/.zshrc, or PowerShell profile) to avoid setting it every time you open a new terminal.
  • Use the --help flag with the claude command to see all available options and flags.
  • Combine the CLI with shell scripts or aliases for repetitive tasks—for example, create an alias like alias claudesum='claude -p "Summarize this text: "' for quick summarization.
  • Monitor your API usage via the Anthropic console to avoid unexpected charges, especially when running automated scripts.
  • If you're behind a corporate proxy, ensure your terminal is configured to allow outbound HTTPS connections to api.anthropic.com.

Troubleshooting

  • If you see an "Authentication failed" error, double-check that your API key is correctly set and has not been revoked or expired in the Anthropic console.
  • If the command claude is not found after installation, verify that your system's PATH includes the directory where npm installs global packages (e.g., ~/.npm-global/bin on Linux/macOS or %APPDATA%\npm on Windows).
  • For "Network timeout" or "Connection refused" errors, confirm your internet connection and that no firewall is blocking access to api.anthropic.com on port 443.
  • If you receive a "Model not found" error, ensure you are using a valid, current model identifier from Anthropic's documentation.
  • When getting JSON parsing errors in scripts, validate that the --format json flag is used and that the output is being processed correctly by your script.
  • If the CLI crashes or shows unexpected behavior, try updating it with npm update -g @anthropic-ai/claude-cli or reinstalling it.

Frequently Asked Questions

Do I need to pay to use the Claude CLI?

The Claude CLI itself is free to install and use, but it requires a valid Anthropic API key, and usage is billed according to your API consumption based on the model and number of tokens processed.

Can I use the Claude CLI without Node.js?

No, the Claude CLI is distributed as an npm package and requires Node.js to run. There is no standalone binary or Python-based alternative officially provided by Anthropic.

Is the Claude CLI available on Windows?

Yes, the Claude CLI works on Windows via Command Prompt or PowerShell, provided Node.js is installed and the API key is set correctly in the environment.

How do I update the Claude CLI to the latest version?

Run npm update -g @anthropic-ai/claude-cli in your terminal to update to the latest version. You can check your current version with claude --version.

Can I use the Claude CLI in automated scripts or CI/CD pipelines?

Yes, the CLI is designed for automation. Use it in shell scripts, CI/CD workflows, or DevOps pipelines to generate text, summarize logs, or assist with code review, as long as the API key is securely managed.

Does the Claude CLI support streaming responses?

As of the current version, the Claude CLI does not support streaming output; it waits for the full response before displaying it in the terminal.