Claude Code

Powering Claude Code with XAI

Claude Code is a powerful coding assistant from Anthropic. It runs directly in your terminal to help 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 of a custom API endpoint, which allows us to use a third-party API router and your own API key to make calls.

Installation and Configuration

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

    npm install -g @anthropic-ai/claude-code
    
  2. Configure the API Key and Endpoint:

    Method: Using Environment Variables

    export ANTHROPIC_BASE_URL=https://api.xaicontrol.com
    export ANTHROPIC_API_KEY=sk-Xvs..
    

Usage Guide

Once configured, you can directly use the claude command in your terminal to interact with the AI.

Example 1: Writing Code

You can directly make a coding request to Claude Code, and it will return the result in a streaming fashion.

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's output.

Example 2: Explaining Code

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

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

Example 3: Interactive Session

Running the claude command by itself will start 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: