Build a ChatGPT‑Powered Code Assistant in 60 Minutes: A Practical Guide for Developers
You’ve probably heard the buzz about AI pair programmers, but most of the hype feels like a distant dream—until now. In under an hour you can have a tiny, but surprisingly useful, code assistant that lives right in your editor. No PhD in machine learning required, just a bit of curiosity and a few minutes of setup.
Why a ChatGPT code assistant matters today
Every developer has that moment when a simple typo or a missing import stalls the whole day. A quick “what’s wrong?” from a teammate can save minutes; a ChatGPT‑powered assistant can save hours. It’s not about replacing you, it’s about giving you a friendly, always‑on‑call buddy that can suggest snippets, explain errors, and even refactor a function while you sip your coffee.
What you need
Before we dive in, let’s make sure you have the basics covered.
- A recent version of VS Code (or any editor that supports extensions). I use VS Code because its marketplace is huge and the setup feels painless.
- An OpenAI API key – you can get one from the OpenAI website. The free tier gives you enough credits for a few hundred requests, which is plenty for a personal assistant.
- Node.js installed – we’ll use a tiny Node script to talk to the API. If you already have npm, you’re good.
- A bit of patience – the whole thing should take about 60 minutes, but feel free to take a short break after each step.
Step 1: Grab an OpenAI API key
Head over to https://platform.openai.com/account/api-keys and click “Create new secret key”. Copy the key; you’ll need it later. Treat it like a password—don’t push it to a public repo.
Personal note: The first time I generated a key I accidentally pasted it into a public gist. Oops! I quickly revoked it and learned the hard way to keep secrets safe.
Step 2: Set up a tiny backend
We’ll create a small Node server that forwards your editor’s requests to the OpenAI API. This keeps your key out of the client side, which is a good security habit.
mkdir chatgpt-assistant
cd chatgpt-assistant
npm init -y
npm install express axios dotenv
Create a file named .env and add:
OPENAI_API_KEY=sk-YourSecretKeyHere
Now make server.js:
require('dotenv').config()
const express = require('express')
const axios = require('axios')
const app = express()
app.use(express.json())
app.post('/chat', async (req, res) => {
const prompt = req.body.prompt
try {
const response = await axios.post(
'https://api.openai.com/v1/chat/completions',
{
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: prompt }],
temperature: 0.2
},
{
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
'Content-Type': 'application/json'
}
}
)
const answer = response.data.choices[0].message.content
res.json({ answer })
} catch (e) {
console.error(e)
res.status(500).send('Error contacting OpenAI')
}
})
app.listen(3000, () => console.log('Assistant server listening on port 3000'))
Run it with node server.js. If you see “Assistant server listening on port 3000”, you’re good to go.
Step 3: Connect the editor
VS Code lets you create custom extensions, but for a quick prototype we can use the built‑in “Tasks” feature combined with a small script.
Create a file ask.js in the same folder:
const axios = require('axios')
const fs = require('fs')
async function main() {
const code = fs.readFileSync(process.argv[2], 'utf8')
const prompt = `You are a helpful coding assistant. Explain what the following code does and suggest any improvements:\n\n${code}`
const resp = await axios.post('http://localhost:3000/chat', { prompt })
console.log(resp.data.answer)
}
main()
Now add a task to .vscode/tasks.json (create the folder if it doesn’t exist):
{
"version": "2.0.0",
"tasks": [
{
"label": "Ask ChatGPT",
"type": "shell",
"command": "node ask.js ${file}",
"problemMatcher": []
}
]
}
Open any source file, press Ctrl+Shift+B, choose “Ask ChatGPT”, and watch the terminal print a friendly explanation. You can tweak the prompt to ask for refactoring, unit tests, or even a one‑line summary.
Step 4: Polish the experience
The basic setup works, but a few tweaks make it feel more like a real assistant.
Add a shortcut
In keybindings.json add:
{
"key": "ctrl+alt+a",
"command": "workbench.action.tasks.runTask",
"args": "Ask ChatGPT"
}
Now a single keystroke summons the assistant.
Limit token usage
OpenAI charges per token (a token is roughly four characters). To keep costs low, add "max_tokens": 300 to the request payload. This caps the response length.
Handle errors gracefully
Wrap the axios.post call in a try/catch and return a friendly message like “Sorry, I couldn’t reach the AI service. Check your server.” This prevents the terminal from spitting a stack trace at you.
How to use it effectively
A code assistant shines when you give it clear, focused prompts. Here are some patterns that work well:
- Explain a snippet – “What does this function do? Why is it using
awaitinside a loop?” - Find bugs – “I’m getting a TypeError on line 12. Here’s the surrounding code.”
- Generate boilerplate – “Create a basic Express route that returns JSON.”
- Refactor – “Rewrite this function using async/await instead of callbacks.”
Remember, the model can hallucinate (make up code that looks right but doesn’t compile). Always review the suggestion before you paste it into production.
When to step back
AI is great for repetitive or low‑risk tasks, but it’s not a substitute for deep domain knowledge. If you’re working on security‑critical code, a thorough manual review is still essential. Think of the assistant as a first draft writer; you are the editor who polishes the final piece.
Wrap‑up
In just an hour you’ve built a functional ChatGPT‑powered code assistant that lives inside your editor. It can explain, suggest, and even generate code on demand. The real power comes from iterating—tweak prompts, add more context, or even plug the server into other tools like your CI pipeline.
Give it a spin on your next bug hunt. You might find that the “aha!” moment arrives a little faster, and you’ll have more time for the parts of development you truly enjoy—designing, architecting, and, of course, sipping that well‑earned coffee.
- → How AI Is Changing Everyday Apps: A Developer's Perspective
- → Build a ChatGPT‑Powered Bot in Python in Under an Hour
- → From Concept to Code: Designing a Voice‑Controlled IoT Light System
- → Automating Your Workflow with Python: 5 Real‑World Scripts
- → Building a Personal Chatbot with OpenAI's API: Step‑by‑Step Guide
- → A Practical Guide to Selecting the Best AI Coding Assistant for Your Development Stack @aidevcompanion
- → How to Build a Daily Random Quote Generator That Boosts Your Coding Productivity @quotecraft
- → How to Pick the Right AI Grammar Checker for Flawless Professional Writing @aigrammarcheck
- → How to Craft High‑Impact AI Prompts That Double Your Writing Speed @promptlab
- → Integrate an AI Coding Assistant into VS Code: A Step‑by‑Step Guide to Faster Bug Fixes @aidevcompanion