👾 AI Agents for Coding: TDD & Memory Bank Strategies

Learn two powerful tactics—Test-Driven Development and Memory Bank—to make AI coding tools smarter, faster, and less error-prone.

One of the main applications of AI agents is writing software. One could say we are almost at the point where AI agents are writing themselves. But without constantly providing context about your codebase, you spend more time fixing and correcting the AI than writing the code yourself.

I want to share two new pro tactics for leveraging AI for coding, I learnt from Jason Zhou (link to original post below under community highlights)

Building Reliable Code with AI: Test-Driven Development

Creating complex functions with AI coding tools can be hard. Often, the AI doesn't get it right the first time. You might face errors. You feed these errors back to the AI. It tries to fix the code. But sometimes this causes more errors. You become a messenger between the testing results and the AI. This can be frustrating. The AI might fix one part but break another.

A helpful approach is Test-Driven Development (TDD). This is a software process. You write tests for functions or features before writing the code itself. You define what input the function expects. You define the expected output. Then, you write just enough code to pass the test. You improve the code while making sure all tests still pass. The test tells you if it passed or not.

This works well with AI tools like Cursor. It helps you align requirements with the AI. More importantly, Cursor can run command-line tests by itself. You can use features like 'yolo mode' or auto-run. You ask it to write a test. Then write the function. Then run the test. The AI iterates on the function based on the test results until all tests pass.

Example prompt to illustrate TDD in Cursor

Example prompt to illustrate the TDD approach

Giving AI a Memory: The Memory Bank Concept

When using AI coding agents for large or complex projects, they often lack context. They don't remember what has been done. They forget the implementation choices made earlier. They might not understand the current task fully. When you ask the agent to add new features, it might not have enough context. This can lead to it messing up existing files.

A concept called Memory Bank helps solve this. It was first introduced by an open-source tool called Cline. The idea is that the AI agent can have memory. It remembers past tasks. It remembers decisions made. It knows the latest task it is working on.

How does this work? It's not magic. It involves defining specific files. These files store different kinds of project context.

  • A project brief defines core goals and requirements.

  • Product context covers user experience and how things should work.

  • Active context holds the current focus, recent changes, decisions, and patterns.

  • System patterns store architecture and key technical decisions.

  • Tech context includes the tech stack, constraints, and dependencies.

  • Progress MD tracks what works and what is left.

Flowchart illustration how the memory bank works

How the memory bank works

These files act as a central context repository. The AI coding agent is taught how and when to use these files. Before building new features, it first reads from this memory bank. It gets a quick download of the project. It understands what has been done. It learns from implementation decisions made previously. This provides enough context. The AI can then build new features on top of what exists without messing up the project.

This method works for new projects started from scratch. It also works for existing projects. The AI agent can look at your existing project files. It can understand what is going on. It can then generate these memory bank files to gain context.

Cursor’s New Features to Level Up Development

In Cursor, this memory bank concept can be adopted using the custom mode feature. Cursor silently rolled out custom modes which is currently in beta. You can enable it in Cursor Settings → Features → Chat → Custom modes.

You can paste in specific instructions that teach the AI to use and update memory bank files. There's also a more advanced project called Cursor Memory Bank. This project pushes the idea further. It uses different custom modes for structured workflows. These modes can include:

  • Van mode for initial setup and understanding the existing project.

  • Plan mode for breaking down tasks, like an engineer manager.

  • Creative mode for thinking, exploration, and debugging. It can help figure out potential root causes for bugs.

  • Build mode for actually implementing the functionality.

Flowchart illustrating how the memory bank with custom modes in Cursor works

Concept of the memory bank system in Cursor with custom modes

Using these modes and the associated memory bank files helps the AI load relevant rules dynamically. It keeps track of progress and context across chats and tasks. This provides a much better starting point for the AI when you want it to implement new features in an existing project.