Creating a Self-Correcting Code Assistant

AI coding assistants are improving, but most still have a big limitation that they generate code once and hope it works. In real engineering, that’s not enough. Production systems need checks, debugging, retries, and self-evaluation. That’s why building a self-correcting code assistant is a practical AI project you can try now.

In this tutorial, you’ll build an AI agent that can write Python code, run it safely, spot errors, fix its own mistakes, and keep trying until the code works.

Self-Correcting Code Assistant

Building a self-correcting code assistant is straightforward but effective. Instead of generating code just once, the agent works in a loop where:

  1. The user gives a task.
  2. AI writes code.
  3. Code gets executed.
  4. Errors are captured.
  5. AI analyzes the error.
  6. AI rewrites the code.
  7. Process repeats until success.

This core idea is also used in many advanced AI agent systems in 2026.

To build this code assistant, we’ll use Python, Ollama to run local LLMs, DeepSeek-Coder, LangChain, and the Python subprocess module to run code.

All the tools are free and open source. Let’s get started.

If you want to move beyond simple LLM apps and build autonomous AI systems, I’ve broken it down in my book: Hands-On GenAI, LLMs & AI Agents.

Step 1: Install Ollama

Ollama lets you run LLMs on your own computer. Here’s how to install it.

Once you’ve installed Ollama, download a coding model. We’ll use DeepSeek Coder:

ollama pull deepseek-coder:6.7b

Step 2: Create the Project Folder

Make a new folder for your project:

mkdir self_correcting_agent
cd self_correcting_agent

Set up a virtual environment:

python -m venv venv

Activate the virtual environment:

source venv/bin/activate # for mac/linux
venv\Scripts\activate # for Windows

Step 3: Install Python Dependencies

Install the needed libraries:

pip install langchain langchain-community ollama

I’m keeping the setup simple so you can easily understand how everything works.

Step 4: Create the Prompt Templates

Make a new Python file called prompts.py:

SYSTEM_PROMPT = """
You are an expert Python engineer.

Your task is to:
1. Write clean Python code
2. Fix bugs when errors appear
3. Return ONLY executable Python code
4. Do not include markdown
5. Ensure the code runs correctly
"""

DEBUG_PROMPT = """
The following Python code failed.

CODE:
{code}

ERROR:
{error}

Fix the issue and return corrected executable Python code only.
"""

This step is very important. Many beginners overlook how crucial prompt engineering is for autonomous agents. Even small mistakes in prompts can cause endless debugging loops.

Adding the instruction “Return ONLY executable Python code” makes the system much more reliable.

Step 5: Build the Code Executor

Next, make a new Python file called executor.py:

import subprocess

def run_code(file_path):
    try:
        result = subprocess.run(
            ["python", file_path],
            capture_output=True,
            text=True,
            timeout=10
        )

        if result.returncode == 0:
            return {
                "success": True,
                "output": result.stdout
            }

        return {
            "success": False,
            "error": result.stderr
        }

    except Exception as e:
        return {
            "success": False,
            "error": str(e)
        }

This file runs the generated Python code and captures:

  • Standard output
  • Runtime errors
  • Timeout failures

In real-world systems, this part is usually separated using Docker sandboxes for security.

For learning, running code locally with subprocess is enough.

Step 6: Build the Self-Correcting Agent

Now, create the main file called agent.py:

from langchain_community.llms import Ollama
from prompts import SYSTEM_PROMPT, DEBUG_PROMPT
from executor import run_code

MAX_RETRIES = 5

llm = Ollama(model="deepseek-coder:6.7b")

user_task = """
Create a Python script that:
1. Reads a CSV file
2. Calculates average salary
3. Displays the result
"""

def generate_code(prompt):
    return llm.invoke(prompt)

code_prompt = f"""
{SYSTEM_PROMPT}

TASK:
{user_task}
"""

generated_code = generate_code(code_prompt)

for attempt in range(MAX_RETRIES):

    with open("generated_code.py", "w") as f:
        f.write(generated_code)

    result = run_code("generated_code.py")

    if result["success"]:
        print("\nFINAL WORKING CODE:\n")
        print(generated_code)

        print("\nOUTPUT:\n")
        print(result["output"])

        break

    print(f"\nAttempt {attempt + 1} failed...")
    print(result["error"])

    debug_prompt = DEBUG_PROMPT.format(
        code=generated_code,
        error=result["error"]
    )

    generated_code = generate_code(debug_prompt)

else:
    print("Agent failed after maximum retries.")

This file contains the main logic. The agent keeps generating code, running it, checking for errors, fixing them, and trying again.

This simple setup is surprisingly powerful.

Step 7: Run the Agent

Now, run the agent:

python agent.py

You’ll see output like this:

Attempt 1 failed...
FileNotFoundError: salaries.csv not found

Attempt 2 failed...
ValueError: invalid column name

FINAL WORKING CODE:
...

This repeating process is what makes the system autonomous, not just generative.

Closing Thoughts

This project teaches you several key AI engineering concepts at once, like LLM orchestration, agent workflows, feedback loops, autonomous systems, and tool usage.

The future isn’t just about chatbots giving answers. It’s about autonomous systems that can check their own work and keep improving their solutions.

I hope you enjoyed this article on building a self-correcting code assistant.

For more AI and machine learning tips, follow me on Instagram. My book, Hands-On GenAI, LLMs & AI Agents, can also help you grow your AI career.

Aman Kharwal
Aman Kharwal

AI/ML Engineer | Published Author. My aim is to decode data science for the real world in the most simple words.

Articles: 2110

Leave a Reply

Discover more from AmanXai by Aman Kharwal

Subscribe now to keep reading and get access to the full archive.

Continue reading