A beginner-friendly project to learn how AI agents work using LangChain. The agent automatically reads a buggy Python file, fixes it, and saves the corrected code back to disk.
Built for learning purposes — covers core concepts like tools, agents, and LLMs.
- Agent calls
read_fileto read the buggy file - LLM figures out what's wrong and fixes it
- Agent calls
write_fileto save the corrected code
read_file(filepath)— reads a file and returns its contentswrite_file(filepath, content)— overwrites the file with fixed code
import os
from dotenv import load_dotenv
load_dotenv()
from langchain_core.tools import tool
from langchain.chat_models import init_chat_model
from langchain.agents import create_agent
@tool
def read_file(filepath: str) -> str:
"""Read and return the contents of a file at the given filepath."""
with open(filepath, "r") as f:
return f.read()
@tool
def write_file(filepath: str, content: str) -> str:
"""Write content to a file at the given filepath, overwriting it completely."""
with open(filepath, "w") as f:
f.write(content)
return f"Successfully wrote to {filepath}"
tools = [read_file, write_file]
model = init_chat_model("groq:qwen/qwen3-32b", temperature=0)
agent = create_agent(
model,
tools,
system_prompt=(
"You are a helpful coding assistant. "
"When asked to fix code in a file: "
"1. Use read_file to read the file's current content. "
"2. Fix the code. "
"3. Use write_file to save the corrected code back to the same file. "
"Do not add explanations inside the file — only write valid code."
)
)
if __name__ == "__main__":
result = agent.invoke({
"messages": [{"role": "user", "content": "Fix the code in test.py so it works correctly."}]
})
print("\n agent response:", result["messages"][-1].content)uv add -r requirements.txt
# add GROQ_API_KEY to .env
uv run python app.py

