AGENTS.md template for FastAPI
A ready AGENTS.md for a FastAPI service. It covers routers, Pydantic models and dependencies, the commands that prove an endpoint works, and the async mistakes agents make in FastAPI code.
# AGENTS.md An HTTP API built with FastAPI and Pydantic v2 on Python 3.12. ## Commands - `uv sync`: install dependencies - `uv run fastapi dev app/main.py`: start the API with reload on http://localhost:8000 - `uv run pytest`: run the tests - `uv run ruff check . && uv run ruff format --check .`: lint and format check - `uv run mypy app`: type-check ## Project structure - `app/main.py`: creates the FastAPI app and includes routers - `app/routers/`: one router per resource; thin handlers - `app/schemas/`: Pydantic request and response models - `app/services/`: business logic, independent of FastAPI - `app/db/`: models, session handling, queries - `tests/`: pytest tests using the test client ## Rules for agents - Do not call blocking I/O (sync database drivers, `requests`, `time.sleep`) inside `async def` endpoints. - This project uses Pydantic v2: `model_config`, `model_dump()`, `field_validator`. No v1 syntax. - Every endpoint declares a `response_model`. Never return ORM objects directly. - Database sessions come from a dependency, never from inside the handler. ## Conventions ### FastAPI - Pydantic v2 models for every request body and response. Let FastAPI do the validation. - Dependency injection via `Depends(...)` — don't import globals inside routes. - Async routes for I/O-bound work; sync routes when the library is sync (don't fake async). - Return typed response models via `response_model=` — don't leak ORM objects. ### 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 FastAPI. 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 FastAPI projects
These are the reasons the “Rules for agents” section says what it says.
Blocking calls inside async endpoints
An agent writes async def and then calls a synchronous database driver or requests inside it, which stalls the event loop for every other request. The template states which endpoints are async and what they may call.
Pydantic v1 syntax
Models still emit class Config, .dict() and @validator. They work with warnings or not at all on v2. Name the version.
Returning ORM objects directly
Without a response model the endpoint leaks every column, including the ones you never meant to expose.
Sessions created inside handlers
Generated code opens a database session per function instead of using a dependency, so nothing closes it on error.
Questions
Where does AGENTS.md go in a FastAPI 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.
Should the OpenAPI schema be mentioned?
Yes, if clients are generated from it. Add one line saying the schema is a public contract and that changing a response model is a breaking change.