Build a CLI AI Agent in Golang
Watch the full tutorial: If you prefer video content, check out my YouTube tutorial where I walk through this entire process step by step! Click here to watch
Building an AI agent is the number one skill in today's world - even if it's not, it is very required and fun to develop, so I wanted to do that but felt like sharing what I have learned with all of you.
Before I got my hands into developing an AI agent I thought that it is somewhat hard to do so, or that I have to use OpenAI models. Both are wrong - it is very simple and you can use any model, so let's get started.
Before we start, if you are here just for the GitHub link, here you go: https://github.com/morethancoder/buildcliagentdemo/tree/main
What is an AI Agent?
Now let's talk about what is an AI agent. It is necessary to know the difference between a large language model (LLM) and an AI agent before we start building one.
In a nutshell, an AI agent is a combination of tools - it's multiple components working together driven by the brain of the agent which is the LLM. These components can be mainly three:
The Model (LLM) - The Brain of the Agent
This is the core intelligence that processes information and makes decisions.
The Context - The Memory of the Agent
Whether it is short term or long term, this also includes the knowledge of the AI agent whether it is the data the model is trained on or outside knowledge you provide.
Tools - The Hands of the Agent
The third one is tools, and tools are basically functions you can call for the agent when the brain (LLM) decides to. Then you give the result of this function call to the LLM to decide what is the next step. So tools are basically functions the LLM can call (we call from the app for the LLM but the LLM decides what and when to do so). Those can be a process, an API call, or anything you can have as a function call in the code.
Let's Start Building
Now that we understand what is an AI agent, we can start building one. We can start with the basic setup and then gradually add more features to make it more interactive and powerful.
Step 1: Basic Setup
First, let's get our environment ready. We'll need to initialize our Go module and set up our dependencies.
Initialize Go Module
go mod init your_project_name
Create Environment File
vim .env
Add your OpenRouter API key:
OPENROUTER_API_KEY=your_api_key_here
Install Dependencies
go get github.com/openai/openai-go/v2
Step 2: Create the Main Application
Now let's create our main.go file:
vim main.go
In this file, we need to:
- Define OpenRouter API URL
- Load API key from
.envand check if defined - Create LLM client using OpenAI SDK with base URL and API key
- Create a
messagesarray and append your query usingopenai.UserMessage - Choose a model and define chat completion parameters
- Create a
ctxand callclient.ChatCompletions.New(ctx, params) - Print the AI response
Run the Basic Version
export $(cat .env | xargs)
go run main.go
Step 3: Making it Interactive
The basic version is cool, but let's make it more interactive by adding a chat loop. This is where things get really fun because now we're building something that feels like a real conversation!
We'll enhance the CLI to wait for user input in a loop:
Create a Scanner
scanner := bufio.NewScanner(os.Stdin)
Add the Interactive Loop
- Print a prompt symbol:
fmt.Print(">") - Read input:
if !scanner.Scan() { break } - Process the input:
input := strings.TrimSpace(scanner.Text()) - Skip empty inputs:
if input == "" { continue } - Append input to
messagesarray - Send chat completion request
- Print AI response
- Loop back for the next input
Now you have a proper interactive chat experience! But we can make it even better.
Step 4: Adding Memory and Visual Appeal
Here's where our agent starts to feel more intelligent. We'll add memory so it remembers the conversation, and some colors to make it look professional.
Memory Enhancement
- Append AI response to
messagesarray - Print message count to track conversation length
- Limit
messagesarray to last 4 messages if it grows too large (this prevents token limits)
Visual Enhancement with Colors
Add colors with the fatih/color package:
go get github.com/fatih/color
Use colors to differentiate:
- User queries (maybe blue or green)
- AI responses (maybe yellow or cyan)
- System info (maybe red or magenta)
This improves readability and visual distinction in the chat, making it feel more like a professional tool.
Final Thoughts
And there you have it! You've just built a fully functional CLI AI agent in Go. What started as a simple API call has evolved into an interactive, memory-enabled chat agent with visual appeal.
The beauty of this approach is that you can now extend it further. You could add:
- File reading capabilities
- Web scraping tools
- Database connections
- Custom commands
- Integration with other APIs
The possibilities are endless when you understand that an AI agent is just a combination of an LLM brain with the tools you provide it.
Ready to Run?
go run main.go
Enjoy your interactive CLI AI agent!
Get the complete code: Want to see the full implementation? Check out the complete source code on GitHub: https://github.com/morethancoder/buildcliagentdemo/tree/main
Feel free to fork it, modify it, and make it your own. If you build something cool with it, I'd love to see what you create!
Want More AI Agent Tutorials?
This tutorial covered the basics of building an AI agent, but we've only scratched the surface! Remember those three core components I mentioned at the beginning? We focused mainly on getting the LLM brain working, but what about the other crucial parts?
Context Management: How do you give your agent long-term memory? How do you manage conversation history efficiently? How do you inject custom knowledge and data?
Advanced Tools: How do you build tools that can read files, make API calls, interact with databases, or even control other applications?
If you want more tutorials diving deep into these components, show me that you want those tutorials! Here's the deal:
🎯 Challenge: Get this YouTube video to 100 likes and I will drop another tutorial talking about context management - how to build agents with persistent memory and custom knowledge bases!
Let me know in the comments what specific topics you want me to cover next. Your engagement helps me understand what content you find most valuable.