Contract-First as Protection Against LLM Agents: API-First Returns to Keep Frontend and Backend Aligned
Moonshot AI
The author describes the problem of project degradation when using LLM agents for development: agents often ignore the actual contracts between frontend and backend, creating dummy data and leading to runtime errors. The solution is to return to the contract-first approach, where the OpenAPI specification is the primary artifact from which server interfaces and clients are generated, making any deviation from the contract a compilation error.
The author encountered on two side projects that LLM agents often diverge in contracts when developing frontend and backend: the frontend agent types responses as Record<string, unknown>, uses loose objects or JSON nodes, duplicates DTOs with different field names, and sometimes even simulates a non-existent backend with local data and toasts. None of these errors are caught by the compiler, linter, or architectural tests because analyzers do not see HTTP requests between codebases. The solution is contract-first: the single source of truth is the OpenAPI spec (YAML), from which server interfaces (with options interfaceOnly and skipDefaultInterface) and a client are generated at build time. The controller implements the generated interface, ensuring signature matching—any deviation from the contract becomes a compilation error. The author notes that this approach is already used in gRPC and GraphQL and is returning to REST, since the contract reader is now not a human but an LLM agent, which does not recover intentions and takes the cheap path of guessing.
Source: Habr — хаб ИИ —
original
