AGENTS.md template for Python
A ready AGENTS.md for a Python project. It tells agents which environment and package manager to use, how to run the checks, and the Python habits that produce code which runs but fails review.
# AGENTS.md A Python 3.12 project managed with uv, linted with Ruff and tested with pytest. ## Commands - `uv sync`: create the virtual environment and install dependencies - `uv run pytest`: run the tests - `uv run ruff check .`: lint - `uv run ruff format .`: format - `uv run mypy src`: type-check ## Project structure - `src/<package>/`: application code - `tests/`: pytest tests, mirroring the package layout - `pyproject.toml`: dependencies and tool configuration; the only place they are declared - `.venv/`: virtual environment. Never edit or commit ## Rules for agents - Run everything through the project environment (`uv run ...`). Never use bare `pip install` or the system `python`. - Dependencies are declared in `pyproject.toml` only. Do not create a `requirements.txt`. - No bare `except` and no `except Exception: pass`. - New and changed functions have type annotations. ## Conventions ### Python - Python 3.11+ features are fair game (PEP 695 generics in 3.12, `Self`, `LiteralString`). - Type hints on every function signature. Use `mypy --strict` or `pyright` in CI. - f-strings for formatting. Never `%` or `.format()` in new code. - Prefer `pathlib.Path` over `os.path`. Use `match/case` where it clarifies branching. - Dependency management: match the repo's tool (`uv`, `poetry`, or `pip-tools`). Don't mix. ### Pytest - Fixtures over setUp/tearDown. Scope them appropriately (`function`, `module`, `session`). - Parametrize repetitive tests with `@pytest.mark.parametrize`. - Mark slow / integration tests and keep them out of the default run. ## Avoid - Bare `except:` clauses — always catch a specific exception. - Mutable default arguments (`def f(x=[])`). ## Guardrails - Never commit secrets, API keys, or credentials. If you spot one in a diff, stop and flag it. - Do not invent dependencies. If a package is not already in the manifest, confirm before adding it. - When a command fails, read the actual error. Do not retry blindly or bypass with `--force`. - Prefer editing existing files over creating new ones. - Run the tests and the linter before saying a task is done, and say what you ran.
The commands and folders are the common defaults for Python. Change them to match your project before you commit the file: an agent will run exactly what is written here.
How to use it
Save the file as AGENTS.md in the root of your repository and commit it. Then check each tool you use:
| Tool | What to do |
|---|---|
| Claude Code | Reads AGENTS.md only when no CLAUDE.md exists in or above the folder. If you have a CLAUDE.md, put @AGENTS.md on its first line. |
| Cursor | Reads AGENTS.md from the project root, in the desktop app and the terminal. In the app, rules in .cursor/rules must be .mdc files. The terminal reads only plain .md files there. |
| Codex | Reads AGENTS.md from the root down to the folder you are in, with a shared 32 KiB limit across all of them. |
| GitHub Copilot | Reads AGENTS.md on some surfaces only. Keep a .github/copilot-instructions.md as well if your team relies on Copilot. |
Details and sources: the AGENTS.md compatibility matrix.
What agents get wrong in Python projects
These are the reasons the “Rules for agents” section says what it says.
Installing into the wrong environment
Agents run bare pip install and python, which hit the system interpreter. Then "it works" in their shell and nowhere else. The Commands section makes every command go through the project environment.
A requirements.txt next to pyproject.toml
Models add a dependency by creating or editing whichever file they saw most in training. Name the single source of truth.
Bare except
except Exception: pass hides the failure the agent was asked to fix. The template forbids it.
Untyped new code
Without a rule, generated functions arrive with no annotations and mypy stays green only because nothing is checked.
Questions
Where does AGENTS.md go in a Python project?
In the root of the repository, next to your manifest file. Tools read it from the folder you start them in and, for most tools, from parent folders too.
Does Claude Code read AGENTS.md?
Only when there is no CLAUDE.md in or above the working folder. If you already have a CLAUDE.md, keep it and make its first line @AGENTS.md so both tools read the same rules.
How long should the file be?
Short enough that every line is something the agent would get wrong without it. Codex shares a 32 KiB budget across all the AGENTS.md files it loads, and long files get followed less closely by every tool.
I use Poetry or plain pip, not uv. What changes?
Only the Commands section. Replace uv run with poetry run, or with your activated virtual environment, and keep the rest.