Skip to main content
In this lesson we demonstrate context management techniques for safely refactoring APIs and guiding an AI assistant to make focused, minimal changes to a codebase. Using a small FastAPI example (a casting number lookup API), you’ll see why starting with a narrow context—one file or a few adjacent files—usually produces more reliable results than feeding the tool the entire repository. We’ll cover:
  • The dataset the API serves (CSV excerpt)
  • The existing endpoints in the route file
  • How to instruct the AI to remove write endpoints (POST/PUT/DELETE)
  • A minimal, cleaned casting.py after the change
  • Verifying the result in Swagger/OpenAPI and with curl
  • Notes on related dependencies (SQLAlchemy, Pydantic)
Resources CSV sample (excerpt)
Goal: Make the API read-only for production or third-party consumption (expose only GET endpoints: list, get-by-casting-number, and search). Rather than hand-editing many files, we’ll show how to limit the AI’s context to the relevant endpoint file and any directly related files. Existing route file (example)
  • File: app/api/endpoints/casting.py
  • This is the starting point for the AI assistant; it includes GET, POST, PUT, DELETE, and SEARCH routes.
Which endpoints do we want to remove?
  • POST /api/castings/ — create_casting
  • PUT /api/castings/ — update_casting
  • DELETE /api/castings/ — delete_casting
Summary: endpoints before vs. after Minimal, cleaned casting.py
  • Remove the write route handlers and the now-unused schema imports (CastingCreate, CastingUpdate).
  • Leaving only read/search routes keeps the OpenAPI surface minimal and easier to audit.
API docs (Swagger/OpenAPI) Below is the same screenshot used in the original workflow showing GET/POST/PUT/DELETE entries. After removing the write routes and unused imports, the Swagger UI should show only the GET endpoints (list, get-by-id, search, and the root).
A screenshot of a web-based API documentation page (Swagger/OpenAPI) titled "Casting Number Lookup API," showing colored endpoint entries (GET, POST, PUT, DELETE) for /api/castings/ and related routes. The page also shows a default root GET and a Schemas section listing casting-related objects.
Run the server and verify the OpenAPI document Example server logs after starting the FastAPI app (condensed):
Try the read endpoints with curl Get a casting by number:
Example JSON response:
Another lookup:
Response:
Database session dependency (reference) Typical SQLAlchemy setup used in this example:
Pydantic v2 note Pydantic v2 changed model configuration keys. If you see warnings about orm_mode, update your schema models to use from_attributes=True in Pydantic v2 model configs. See the Pydantic migration guide for details: https://docs.pydantic.dev/latest/ Best practices when using an AI assistant to refactor
  • Start with a narrow context: provide the single target file (e.g., app/api/endpoints/casting.py) first.
  • Be explicit about the desired change (e.g., “remove POST/PUT/DELETE endpoints and unused schema imports”).
  • If the AI needs more context, iteratively provide only the adjacent files it imports (schemas, models, and the DB dependency), not the entire repo.
  • Re-run your tests and check the Swagger/OpenAPI UI after edits to confirm only the intended endpoints are exposed.
Key takeaways
  • Start small: give the AI one or a few files that are directly relevant to the change you want.
  • Be explicit about what you want removed or changed (e.g., remove POST/PUT/DELETE).
  • Verify that unused imports are removed (e.g., schema types you no longer use).
  • Check Swagger/OpenAPI after the edit to confirm the intended surface is exposed.
  • If needed, gradually add more context (related modules, terminal output, or recent git commits) until the AI has enough information.
Start with a single, focused file as context (e.g., casting.py). If the AI’s edits are incomplete, iteratively add only the adjacent files it imports (schemas, models, or database) rather than the entire repository. Smaller, relevant context often produces more reliable edits.

Watch Video