🛠️ How I Built a Terminal Based AI Coding Agent (Like a Mini Cursor)

Krish DesaiKrish Desai
4 min read

“What if your terminal could think like a developer?”
That simple question led me to build my own AI-powered coding assistant a terminal-based agent capable of generating entire projects, writing full-stack code and responding intelligently to follow-up prompts. Think of it as my mini version of Cursor but inside your command line.

In this post, I’ll take you through the journey of building this agent: the motivations, technical architecture, learnings and how you can extend it for your own needs.


🚀 Motivation: Why I Built This

As developers, we spend a lot of time switching between code editors, terminal windows and documentation. I wanted to streamline this to build a CLI tool that feels like pair-programming with an intelligent assistant.

I was deeply inspired by modern dev tools like Cursor and by mentors like Piyush Garg and Hitesh Choudhary, who emphasize building tools, not just learning about them.

So I asked myself:
Can I build an AI agent that builds software — directly from the terminal?


⚙️ What It Does: Key Features

  • 🧠 Prompt-Based Interaction
    Talk to the agent in natural language:

    add a login page with email/password authentication
    It parses the context and updates your project intelligently.

  • 📁 Auto Project Scaffold
    Generates folder and file structure based on your tech stack preferences — React + Express, MERN, NestJS, etc.

  • 📝 Code Writing
    It writes real, production-ready code directly into files — frontend UI components, backend routes, database schemas.

  • ⚡ Real-Time Command Execution
    Runs npm install, pip install, npx prisma generate, npm run dev, and more — directly from within the agent.

  • 🔁 Iterative Prompts
    You can follow up with:

    now add Google login
    The agent will find and edit the correct files intelligently.


🧠 How It Works: Behind the Scenes

🧩 1. Natural Language Understanding

Using the OpenAI API (or LLMs like Mistral, LLaMA via Ollama), the user’s prompt is parsed using structured system instructions. Example system prompt:

You are a software engineer that builds full-stack apps. Given user instructions, generate project folders, write frontend and backend code, and execute commands when needed.

🗂️ 2. Project Context Parsing

The agent reads the existing folder structure and file content to understand the current state. This context is used in every follow-up prompt.

🧾 3. File Writing Logic

The agent determines:

  • Which files to update

  • Which lines to modify

  • Where to insert new code

It uses structured fs operations in Node.js to write code into actual .js, .ts, .jsx, .json, and .env files.

🧪 4. Command Execution

A shell wrapper executes commands like:

child_process.exec('npm install')

Outputs are captured and logged to the user like a real CLI experience.


🖼️ Sample Interaction

bashCopyEdit> initialize react + express project
✅ Project scaffold created

> add login page with email/password
🧠 Context parsed
📝 Writing Login.jsx, AuthRoutes.js, updating App.jsx...

> install dependencies
📦 Running: npm install react-router-dom express bcrypt

> now add forgot password flow
🔁 Updating AuthRoutes.js and adding ForgotPassword.jsx

⚠️ Challenges & Fixes

🔄 Context Management

Problem: Model forgets context or overwrites unrelated files.
Fix: Used a snapshot of file tree + diff and attached recent edits in every prompt.

🛠️ Code Injection Issues

Problem: LLM inserted duplicate functions or broke existing code.
Fix: Added helper functions for safe append/replace, and file-specific constraints.

🐢 Performance Bottlenecks

Problem: Long command chains slowed the CLI.
Fix: Debounced command execution and showed stepwise loading.


📚 Learnings from Prompt Engineering

I went deep into prompt formats and methods to get consistent results.

✅ System + Task + Context Prompting

Combined system message + file tree + task message.

🧠 Few-Shot Prompting

Gave the model examples like:

// Instruction:
Add a new route to /register in Express

// Response:
Create file routes/register.js, write:
router.post('/register', ...)

🧵 Chain of Thought Prompting

Got better quality by encouraging the model to “think” step-by-step:

First, check if user model exists. Then add the controller. Finally, update routes.

🧩 How Others Can Use or Extend It

  • Plug into any tech stack: React, Next.js, NestJS, Django — just modify the scaffold templates.

  • Customize the system prompt: Tailor it for your team, coding style, or even a language (like Python or Go).

  • Add voice input or autocomplete: Turn it into a full dev assistant.

  • Deploy as a binary CLI: Package it for others to install via npx or brew.


📅 What’s Next?

  • Integrating AI-powered code linting

  • Adding file diff previews before writing

  • Local model fallback via Ollama (for offline use)

  • Voice-to-code CLI interaction using Whisper


☕ Final Thoughts

Building this mini Cursor taught me something simple but powerful:

💡 Prompting is the new scripting.

You don’t need 1,000 lines of shell scripts anymore. Just an intelligent agent that understands what you want and writes what you mean.

It’s not just about automating tasks — it’s about building with AI, not just using it.


10
Subscribe to my newsletter

Read articles from Krish Desai directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Krish Desai
Krish Desai

I am a Software Engineer. I have 3 years of experience in building Software Applications with high level production ready code and tech industry.