X No. 6959 words
How to build a simple Claude-powered AI CLI from scratch. No framework. One file.

I think we’re moving into a pretty neat time in history, where how people learn shifts from reading books and watching YT videos, to actually building the thing. This also means that anyone can learn anything at a much deeper level than before IMO, since I think people fundamentally learn better by doing, than by reading or watching.
Lately, I’ve been going pretty deep with CLIs, because I think as we move into a world where there are more agents than humans interacting with software and websites, CLIs are going to be increasingly more important, esp. when speed/performance matters.
Most AI tutorials throw a framework at you (LangChain, etc.) before you understand what’s happening underneath. I feel like starting with raw API calls means you actually understand what the framework is abstracting later.
And for me personally, I think a lot about agentic commerce, and how much every second of speed is going to matter when it comes to helping an agent make a decision on what to buy, or present to a shopper.
So, given how important CLIs are becoming, or more like already are, I thought it would be interesting to whip up a little, simple AI CLI, that anyone can build and use, to understand what’s going on under-the-hood with an AI-CLI, and in this example, powered by Claude.
You don’t need to know how to code to understand this and run it yourself. And I’m making the Github repo public, so you can even pop over at look at it now if you want before you read any more. Here’s the link:
https://github.com/Zen-Open-Source/SimpleCLI
Okay, now let’s walk through how you can learn aboutCLIs, by building a simple CLI yourself.
First - one thing you’ll notice, everything is in one file, index.js, it’s ~80 lines of code, that’s it. There’s no framework, no abstraction layer, just raw SDK and stuff that’s build-into Node.js.
Building this yourself or just cloning the repo, looking at the code, and running it, will show you how LLMs actually work at the API level, as well as how simple it is to build a CLI, which can help explain why they’re so fast too.
Okay, so now let’s get to the good stuff and walk through the code.
The 9 moving parts, explained
1. Imports. Three things:
-
dotenv (reads .env into process.env)
-
Node’s built-in readline (terminal I/O)
-
Anthropic SDK (HTTP client with streaming built in)
2. Client setup. new Anthropic() with no arguments. The SDK automatically looks for ANTHROPIC_API_KEY in your environment. If it’s missing, it throws an error.
3. Conversation history. This is where I think/hope some people will have their aha moment. The Claude API is completely stateless — it remembers nothing. You need to maintain an array of { role, content } objects and send the entire thing on every request. That array is the memory.
4. System prompt. Sent as a separate field, not inside messages[]. This shapes Claude’s behavior globally. Swap this out to change the personality entirely. And you’ll notice, I already put my preferences in there because I can’t stand Claude’s default overly friendly personality, so I updated this in the code to send this in as the system prompt: “You are a concise CLI assistant. Be direct and accurate. No fluff, tell it like it is.”
5. Readline. Wraps stdin and stdout so you can prompt and receive line-by-line terminal input. This is built-into Node so no dependencies needed.
6. ANSI colors. Kinda an extra but fun to have in there, this is just three tiny helpers using escape codes. \x1b[36m = cyan, \x1b[2m = dim, \x1b[90m = gray, \x1b[0m = reset. Works in every real terminal.
7. The chat() function. The actual AI call. Uses client.messages.stream() instead of client.messages.create(), the difference is that stream() fires a “text” event for every token chunk as it arrives, so you see output immediately instead of waiting for the full response. At the end, the all the text gets pushed to history so Claude can reference it next turn.
8. The input loop. Recursive async function. It prompts, waits for input, calls chat(), then calls itself again. Also handles two slash commands: /quit to exit cleanly, /history to dump the raw JSON context window.
9. Entry point. Prints a header and starts the loop. That’s it 🕺
You’ll see the index.js file is super duper commented, so you can look at the file in the repo and follow along there as well.
A few takeaways that I think are interesting, and that I don’t think people know right away until they dig into this stuff:
- Claude (and every major LLM API) is completely stateless — it has zero memory between calls
- The “memory” is just an array you maintain yourself and send with every request
I didn’t know this myself for quite a while there, and when I learned it, it kinda blew my mind. To think of memory as just an array, also made me realize how darn useful it was to learn about arrays in college because jeez, they certainly never went away, they’re more useful than ever now.
Okay, you now have a simple cli to play around with, and you know how an AI cli works under-the-hood. Go ahead and fork it, break it, extend it, go nuts, lots more to learn now that you have this foundation.
And that’s it, I have no newsletter to shill you, no podcast for you to listen to, all that I ask if you find this useful, like and and share it.
Oh and the most important part, I try to respond to every single comment I get, the conversations on X are what I like the most. So if you have a question, comment, suggestion, anything, comment below and let your voice be heard!