c/agents › command
0votes

CLAUDE.md template for a Python FastAPI service

TemplateLow risk Tested
submitted by CommandVault editors to c/agents · 2 uses

Project memory for Claude Code on a Python API: commands, layout, testing and the rules that keep it safe.

Template · 7 variables
# CLAUDE.md

## Service
{{service_name}}: {{what_it_does}}. Python {{python_version}}, FastAPI, {{database}}.

## Commands
- Setup: `{{setup_command}}`
- Run: `uvicorn {{module}}:app --reload`
- Tests: `pytest -q` (add `-k name` for one test)
- Lint/format: `ruff check . && ruff format --check .`
- Types: `mypy {{package}}`

## Layout
- `{{package}}/api`: routers, one file per resource
- `{{package}}/services`: business logic (no FastAPI imports here)
- `{{package}}/db`: models and queries
- `tests/`: mirrors the package layout

## Conventions
- Pydantic models for every request and response; never return ORM objects directly.
- Dependencies via `Depends()`; no globals for sessions or clients.
- Raise `HTTPException` only in routers; services raise domain errors.
- Write a test for every bug fix that fails before the fix.

## Never
- Commit secrets or `.env` files.
- Run migrations against production.
- Disable a failing test to make CI pass.
What the team calls this service, e.g. billing-api
One line, e.g. issues invoices and handles Stripe webhooks
e.g. 3.12
e.g. PostgreSQL 16 via SQLAlchemy
Exact command to install dependencies, e.g. uv sync
Where the FastAPI app object lives, e.g. app.main (for uvicorn app.main:app)
Top-level Python package for type checks, e.g. app
0 commentsreport

Guide

What you get: Claude Code (and Codex, if you save it as AGENTS.md) starts every session knowing your service's name, how to set up, run, test, lint and type-check it, where code lives, and what it must never do. Best for Developers using Claude Code or Codex on a Python FastAPI service..

Before you start

  • A FastAPI project (or one you're starting)
  • Claude Code or Codex installed
  • Your real setup command and package/module names

Worked example (Oct 17, 2026, Claude Code 2.1.296; Codex 0.151.0 (gpt-5.6-sol))

Input

Filled with: service_name=Quillfeather Ledger API, what_it_does=reconciles bakery supplier invoices against delivery notes, python_version=3.12, database=PostgreSQL 16 via SQLAlchemy 2, setup_command=uv sync --frozen, module=quillfeather.main, package=quillfeather. Question: Without reading any files, answer from the project instructions you were given at startup: what is this project called, what exact command runs the tests, and name one rule you must follow. If you have no such instructions, say so.

Result

Claude Code: "Project: Quillfeather Ledger API... Test command: `pytest -q`. Add `-k name` to run a single test. One rule: every bug fix needs a test that fails before the fix." Codex: "Project: Quillfeather Ledger API / Test command: `pytest -q` / Rule: Write a test for every bug fix that fails before the fix."

Fresh copy of a Next.js 16 starter repo (the stack in the file does not exist; this checks that the file is loaded, not that the commands run). Claude Code: file saved as CLAUDE.md, run headless in plan mode; a second run in JSON mode reported 1 turn, so no files were read. Codex: same text saved as AGENTS.md in a separate fresh copy, read-only sandbox; its log shows no commands run. The user's global ~/.claude/CLAUDE.md was also loaded for Claude Code. Both answers were correct on the first try. After this test every input got a hint (for example what "App import path" means).

How to check it worked

  • Start a new session in the project folder and ask, without letting it read files, what the project is called and what command runs the tests. It should repeat your CLAUDE.md back.
  • In Claude Code, run /memory to see which instruction files are loaded.
  • If you also use Codex, save the same text as AGENTS.md (or have CLAUDE.md import it) and ask Codex the same question.

Common problems

  • The agent answers with generic FastAPI advice instead of your values: the file isn't being loaded. Check it's named exactly CLAUDE.md (Codex needs AGENTS.md) and sits at the repo root, then start a new session; edits don't reach a session that's already running.
  • Leftover {{placeholders}}: the agent will repeat them literally. Search the file for '{{' before saving.
  • The file describes commands that don't match the repo (for example a different test runner). The agent trusts the file, so keep it in sync with package/tooling changes.
  • Your project uses a different runner or layout than the template assumes (for example `python -m pytest`, or code under `src/`). The test command and layout lines aren't blanks, so edit them by hand.

Use this file with your AI tool

Each tool reads project instructions from a different file name. Fill in the blanks above, then save it where your tool looks.

One command
npx commandvault add claude-md-template-for-a-python-fastapi-service-6a36a740 --tool claude
Save as CLAUDE.md
# CLAUDE.md

## Service
{{service_name}}: {{what_it_does}}. Python {{python_version}}, FastAPI, {{database}}.

## Commands
- Setup: `{{setup_command}}`
- Run: `uvicorn {{module}}:app --reload`
- Tests: `pytest -q` (add `-k name` for one test)
- Lint/format: `ruff check . && ruff format --check .`
- Types: `mypy {{package}}`

## Layout
- `{{package}}/api`: routers, one file per resource
- `{{package}}/services`: business logic (no FastAPI imports here)
- `{{package}}/db`: models and queries
- `tests/`: mirrors the package layout

## Conventions
- Pydantic models for every request and response; never return ORM objects directly.
- Dependencies via `Depends()`; no globals for sessions or clients.
- Raise `HTTPException` only in routers; services raise domain errors.
- Write a test for every bug fix that fails before the fix.

## Never
- Commit secrets or `.env` files.
- Run migrations against production.
- Disable a failing test to make CI pass.

Use it: Claude Code loads it at the start of every session. Check with /context (Memory files).

From Claude Code's docs , checked Oct 17, 2026.

More from c/agents

See all of c/agents →
0 comments

Log in to join the discussion.