How to Prepare a Codebase for an AI Coding Agent
This guide explains how to structure and package a codebase so an AI coding agent can understand it quickly, make accurate changes, and avoid common pitfalls. You’ll learn what files to include, how to document the project, and how to test the setup before handing it over.
What You’ll Need
- A codebase you can access locally (or clone from a repository)
- A text editor or IDE (VS Code, IntelliJ, etc.)
- Command-line access (Terminal, PowerShell, etc.)
- Basic familiarity with Git and your project’s build system
- An AI coding agent tool (e.g., GitHub Copilot Workspace, Cursor, Codex CLI, or similar) – the steps are tool-agnostic
Step-by-Step
- Clean up the repository structure. Remove any generated files, build artifacts, or temporary files that aren’t needed. Ensure the directory tree reflects the actual source code. If you have a monorepo, make sure each package or service is clearly separated.
- Write a comprehensive README.md. This is the first file an AI agent reads. Include:
- What the project does and its main features
- Tech stack (languages, frameworks, versions)
- How to install dependencies and run the project
- How to run tests and linting
- Any environment variables or configuration files needed
- Add a CONTRIBUTING.md file. Even if you’re the only developer, this file tells the agent how to make changes: coding style, commit message conventions, branch naming, and any pre-commit hooks.
- Create or update a .gitignore file. Exclude node_modules, dist, build, .env, and other environment-specific files. This prevents the agent from reading or modifying files it shouldn’t.
- Define the project structure in a doc. Create a short ARCHITECTURE.md or add a section in README that explains the high-level layout: where the entry points are, how modules depend on each other, and where business logic lives. Use a simple tree diagram or bullet list.
- Add inline documentation for complex parts. For functions or classes that are non-obvious, add docstrings or comments explaining the purpose, inputs, outputs, and any edge cases. Avoid over-commenting trivial code.
- Ensure the build and tests run locally. Before packaging, run the build and test commands yourself. If they fail, fix them. An agent will likely run these commands, and a broken build will waste time and confuse the agent.
- Pin dependency versions. Use a lockfile (package-lock.json, poetry.lock, requirements.txt with pinned versions, etc.). This ensures the agent installs the same versions you use, reducing environment-related issues.
- Create a task description file. If you have a specific task for the agent, write it down in a file like TASK.md or include it in the prompt. Specify the desired outcome, acceptance criteria, and any constraints (e.g., “do not change the public API”).
- Remove any secrets or credentials. Scan the codebase for API keys, passwords, or tokens. Use environment variables or a .env.example file instead. Never include real secrets in the package.
- Package the codebase. If you’re sending it as a zip file, make sure the archive excludes .git, node_modules, and other large directories. If you’re using a remote repository, push the latest commit and ensure the branch is clean.
- Provide a test plan. Include a list of commands the agent should run to verify changes, plus any manual test scenarios. This helps the agent self-check its work.
- Do a dry run with the agent. Before giving the full task, ask the agent to summarize what it understands about the project. If the summary is off, adjust your documentation.
Common Mistakes
- Skipping the README. Agents rely heavily on this file. Without it, they may guess the build commands and fail.
- Including generated files. Build artifacts and dependencies bloat the package and confuse the agent about what to edit.
- Not pinning dependencies. If versions float, the agent may install incompatible packages and produce errors.
- Over-commenting trivial code. Too many comments can distract the agent from important logic. Keep comments meaningful.
- Ignoring existing tests. If tests are failing before the agent starts, it may waste time debugging unrelated issues.
- Leaving secrets in the code. This is a security risk and can also cause the agent to inadvertently use or expose them.
Tips
- Use a consistent code style. If you have an ESLint, Prettier, or Black config, include it. Agents can follow style guides if they’re clear.
- Write a short ‘agent instructions’ file. Some tools allow you to add a file like AGENTS.md that gives specific guidance to the AI. This is separate from human docs and can include things like “always run tests after changes”.
- Keep the scope small. For the first few tasks, give the agent a single, well-defined issue rather than a broad feature. This improves success rate.
- Use Git history wisely. If you have a clean commit history, the agent can learn from it. If your history is messy, consider squashing before packaging.
- Provide example inputs/outputs. If the code processes data, include sample files so the agent can test its changes.
Troubleshooting
If the agent fails to understand the codebase or makes incorrect changes, try these steps:
- Check the agent’s initial summary. Most tools provide a plan or summary before acting. If it’s wrong, clarify your README and architecture docs.
- Verify the build commands. Make sure the commands in your README are exactly what a fresh clone would need. Test on a clean clone to be sure.
- Look at the agent’s logs. If it’s failing to install dependencies, check for network restrictions or missing system libraries. You may need to add a setup script.
- Reduce the task complexity. Break the task into smaller subtasks and run them sequentially.
- Check for ambiguous terms. If your code uses domain-specific jargon, add a glossary in the README.
- Update your dependencies. If the agent is using a newer runtime than you, it may run into compatibility issues. Pin the runtime version in a .nvmrc or .tool-versions file.
Frequently Asked Questions
Do I need to include the .git folder when packaging?
No. The .git folder is usually large and not needed. The agent can work with a clean directory. If you want the agent to see commit history, provide it separately or push to a remote branch.
What if my codebase is huge?
Focus on the parts relevant to the task. You can create a reduced package containing only the source directories and config files needed. Or, use a tool that supports file-level context selection.
Should I write tests before giving it to the agent?
Yes. Having existing tests helps the agent verify its changes. If you don’t have tests, at least provide a manual test script.
Can I use the same package for multiple tasks?
Yes, but you should update the task description file each time. Also, if your codebase changes between tasks, re-package it.
What if the agent changes files it shouldn’t?
Use Git to review changes and revert unwanted ones. You can also set file permissions or use a tool that restricts file access.
Do I need to document every function?
No. Focus on public APIs, complex logic, and anything that would be hard to infer from the code. Over-documentation can be as bad as none.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.