Build a Minimal AI CLI with Claude in 80 Lines of Node.js

This tutorial walks you through creating a lightweight, single‑file AI command‑line interface powered by Anthropic's Claude API, covering prerequisites, repository setup, core components, streaming responses, built‑in commands, and the underlying stateless architecture.

High Availability Architecture
High Availability Architecture
High Availability Architecture
Build a Minimal AI CLI with Claude in 80 Lines of Node.js

Overview

This tutorial demonstrates how to build a minimal AI command‑line interface (CLI) that talks to Anthropic's Claude model using the official SDK. The entire application lives in a single index.js file (≈80 lines) and requires only Node.js built‑in modules, the dotenv package for environment variables, and the Anthropic SDK with streaming support.

Key Components

Imports : dotenv (load .env), Node's readline (terminal I/O), and the Anthropic SDK ( new Anthropic()) which reads ANTHROPIC_API_KEY from the environment.

Client Setup : Instantiating new Anthropic() creates a client that throws an error if the API key is missing.

Conversation History : An array of { role, content } objects is maintained in memory and sent with every request, providing the AI’s “memory”.

System Prompt : A dedicated message in the messages array defines Claude’s global behavior (e.g., a concise CLI assistant).

Readline Wrapper : Uses Node’s readline to read user input line‑by‑line without extra dependencies.

ANSI Color Helpers : Simple functions that prepend escape codes such as \x1b[36m (cyan) and \x1b[0m (reset) to style terminal output.

chat() Function : Calls client.messages.stream() so tokens are streamed and displayed in real time; the full response is appended to the history array.

Input Loop : An asynchronous recursive function that prompts the user, invokes chat(), and processes two slash commands: /quit / /exit to terminate and /history to dump the raw JSON context.

Entry Point : Prints a banner and starts the input loop.

Prerequisites

Node.js v18 or newer

An Anthropic API key (set as ANTHROPIC_API_KEY in a .env file)

Project Setup

# 1. Clone the repository and enter the directory
git clone https://github.com/Zen-Open-Source/SimpleCLI.git
cd SimpleCLI

# 2. Install dependencies
npm install

# 3. Copy the example environment file
cp .env.example .env

# 4. Paste your API key into .env
ANTHROPIC_API_KEY=sk-ant-your-key-here

Running the CLI

npm start

When started, the CLI prints a prompt such as:

AI CLI  |  输入 /quit 退出  |  输入 /history 查看上下文
You: _

Any line entered is sent to Claude; the response streams back token‑by‑token.

Built‑in Commands

/quit

or /exit – exit the CLI. /history – print the full conversation history as formatted JSON.

How It Works

Stateless API, Stateful Array : Claude’s API does not retain any memory between calls. The application supplies a history array with every request, which the model treats as context.

Streaming : Using client.messages.stream() emits each generated token immediately, allowing the user to see the answer as it is produced instead of waiting for the complete response.

Single‑File Architecture : All logic resides in index.js, making the codebase easy to read, modify, and extend.

References

SimpleCLI repository: https://github.com/Zen-Open-Source/SimpleCLI

Anthropic API console: https://console.anthropic.com

Node.js official site: https://nodejs.org/

AI CLI illustration
AI CLI illustration
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CLIAILLMStreamingNode.jsClaude
High Availability Architecture
Written by

High Availability Architecture

Official account for High Availability Architecture.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.