š§ How I Built My First Self-Coding AI Agent Using Ollama

āCan an AI help me write production-ready code inside my own repo, using my structure?ā
That one question led me down a rabbit holeāand out came a self-coding AI agent that understands my codebase and builds features like a lightning-fast junior developer.
In this post, Iāll walk you through how I built it using Ollama, LangChain, and a few Python tricks. If you're tired of copy-pasting AI output that doesnāt fit your project, this oneās for you.editor:Sagar Arora GPT
š The Motivation
As a backend developer, I work with FastAPI, SQLAlchemy, and tools like Telnyx. Most of my time goes into:
Repeating boilerplate code (CRUD, routes, schemas)
Ensuring consistent project structure
Debugging AI output that doesnāt match my codebase
I wanted an AI that could:
Understand my repo
Follow my patterns
Write new modules automatically
And I didnāt want to hit OpenAIās APIs for every little thing. So I turned to Ollamaāa lightweight, local LLM runner.
š§° Tools I Used
š§ Ollama ā for running LLMs locally (LLaMA3, CodeLLaMA, Phi-3, etc.)
š Python ā to build agent logic
š LangChain ā to chain tools, context, and models
šļø My own codebase ā a working FastAPI app with clear modular structure
š§© Architecture Overview
+----------------------------+
| User Prompt (API) |
+-------------+--------------+
|
v
+----------------------------+
| Ollama Agent (LangChain)|
| - Prompt Template |
| - Code Context Loader |
| - File Writer Tool |
+-------------+--------------+
|
v
+----------------------------+
| Local Repo Manipulation |
| (Reads/writes code files) |
+----------------------------+
My Ollama agent is powered by a LangChain agent that:
Reads relevant files from the repo
Passes the code context to the model
Uses a prompt template tailored to my stack
Writes new files directly into the repo
š§ Making the AI Understand My Code
I created a context loader that:
Walks through the project directory
Picks important files (
models/*.py
,schemas/*.py
, etc.)Feeds their content as part of the prompt
To avoid token limits, I use:
Chunking (
LangChain TextSplitter
)File filtering
Optionally: embedding and retrieval (RAG)
š§¾ The Prompt That Made It Work
You are a coding assistant for a FastAPI backend project.
Use the following structure to generate a new CRUD module for: {{ entity_name }}
Expected Files:
- models/{{ entity_name }}.py
- schemas/{{ entity_name }}.py
- routes/{{ entity_name }}.py
- tests/test_{{ entity_name }}.py
Follow the existing style:
{{ code_context }}
Respond with valid Python code blocks only.
With this, the model didnāt hallucinate file names or invent new folder structures. It stuck to my existing format.
š§Ŗ A Real Example
When I sent a POST request like this:
{
"entity_name": "user"
}
My agent generated:
ā
models/
user.py
with a SQLAlchemy modelā
schemas/
user.py
with Pydantic classesā
routes/
user.py
with FastAPI endpointsā
tests/test_
user.py
with test cases
And it perfectly matched the coding style I use across the project. No more awkward copy-paste fixes.
āļø Internals: Tools & Execution
Hereās what powers the whole setup:
LangChain AgentExecutor with:
A code context retriever
A prompt template tool
A file writer tool
Ollama LLMs:
I experimented with
codellama:13b
,llama3
, andphi3
Codellama worked best for structured code tasks
Filesystem Tool:
Saves each code block as a new Python file
Adds it into the correct path in the repo
š” What I Learned
Local LLMs like CodeLLaMA can seriously reduce your cloud dependency
Adding your own tools (like file writers) gives you control and flexibility
Combining LLMs with context-aware design = production-grade AI automation
For larger projects, RAG with FAISS or Chroma will help a lot
š Whatās Next?
Hereās what Iām planning to build on top of this:
ā Git auto-commit for all agent-generated files
ā Code test runner after generation
ā YAML config to guide agent behavior (naming, folders)
ā A web UI with file preview + approval workflow
š§Ŗ Prompt optimization for different types of modules (jobs, services, handlers)
šÆ Final Thoughts
This project made me realize that LLMs arenāt just code helpersāthey can be active contributors to your development process.
If you already have a working codebase and want to save hours of repetitive dev work, give Ollama + LangChain a try. Build your own repo-native AI engineer.
It feels like magicāonly faster.
āļø Written by Sagar Arora GPT
Follow me for more experiments on AI tooling, dev automation, and self-coding agents.
Subscribe to my newsletter
Read articles from Sagar Arora directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
