Claude Code

Powering Claude Code with XAI

Claude Code is a powerful coding assistant from Anthropic. It runs directly in your terminal, helping you write, explain, and refactor code.

By default, using Claude Code requires logging into an official Claude account and purchasing a subscription. Fortunately, it supports configuration via an API endpoint, which allows us to power it through a third-party API gateway, enabling you to use your own API keys.

Installation and Configuration

  1. Install the Claude Code CLI: You'll need to have Node.js and npm installed. Then, run the following command in your terminal to install the Claude Code tool globally.

    npm install -g @anthropic-ai/claude-code
    
  2. Configure API Key and Endpoint: Claude Code uses a JSON file for API configuration. Find or create a .claude directory in your user's home directory, and create a file named setting.json within it (full path: ~/.claude/setting.json).

    Paste the following content into the setting.json file. Be sure to replace sk-... in both ANTHROPIC_API_KEY and apiKeyHelper with your own valid API key.

    {
      "env": {
        "ANTHROPIC_API_KEY": "sk-***",
        "ANTHROPIC_BASE_URL": "https://api.xaixapi.com"
      },
      "permissions": {
        "allow": [],
        "deny": []
      },
      "apiKeyHelper": "echo 'sk-***'"
    }
    
  3. Initialize the Configuration: After saving the setting.json file, execute the following command in your terminal to load and initialize the configuration:

    claude init
    

    Once the command completes, your Claude Code is configured and ready to use!

Usage Guide

Once configured, you can start interacting with the AI directly from your terminal using the claude command.

Example 1: Writing Code

You can directly ask Claude Code for a coding request, and it will stream the response back to you.

claude "write a python flask app with a single endpoint that returns hello world"

Claude Code will immediately start generating the code and related explanations, which you can see directly in your terminal.

Example 2: Explaining Code

If you have a piece of code you don't understand, you can pipe it to Claude Code for an explanation.

cat my_script.js | claude "explain this javascript code"

Example 3: Interactive Session

Running the claude command by itself starts an interactive session where you can have a multi-turn conversation.

claude
> How can I help you today?
> write a git command to show the commit history of a specific file

For more information or help, please refer to the following online resources: