
Summary
Key points of this document
- Where are the design challenges?
- In FastAPI, we keep endpoints thin and separate document ingestion, search, answer generation, and evaluation into service layers. PostgreSQL is used not just as a storage destination, but as an auditing backbone that links chunks, embedding_version, search executions, and answer executions.
- In table design, separating documents, chunks, embedding_runs, search_runs, and answer_runs makes re-execution and comparison easier. Even if the API response only returns the answer body, internal logs should retain search top-k, exclusion reasons, and prompt template IDs.
Overview
From a chat interface perspective, a RAG API appears as a single endpoint. However, internally, document ingestion, index updates, search, prompt construction, answer generation, and log storage each have distinct responsibilities. With a FastAPI and PostgreSQL configuration, it's crucial that RAG can be designed not just as a chat API, but as a data processing pipeline capable of reproducing search history.
Where are the design challenges?
When implementing RAG with FastAPI, the initial inclination is to centralize processing at /chat. While this works in the short term, visibility deteriorates when it comes to investigating search quality, reproducing answer failures, re-indexing after document updates, or implementing permission-based searches.RAG has longer processing times than typical CRUD APIs and is affected by delays and failures of external AI APIs. Furthermore, if the basis for an answer cannot be explained later, it becomes difficult to troubleshoot issues in business use.The difficulty of a RAG API lies not in calling external AI APIs, but in the numerous intermediate states from question to answer. Without saving the state when chunks are created, embeddings are updated, search rankings are determined, and prompts are assembled, it's impossible to reproduce incorrect answers.
How to define boundaries?
We separate the ingestion API, search API, answer generation API, and evaluation API. While a chat endpoint can combine search and generation, internally, we separate the search service and generation service, logging their respective inputs and outputs.
In FastAPI, we keep endpoints thin and separate document ingestion, search, answer generation, and evaluation into service layers. PostgreSQL is used not just as a storage destination, but as an auditing backbone that links chunks, embedding_version, search executions, and answer executions.
Effective details in implementation
In PostgreSQL, we store documents, chunks, embedding metadata, question logs, search candidates, and answer logs in separate tables. In FastAPI, we pass DB sessions, AI clients, and search services via dependency injection, allowing them to be swapped during testing.
In table design, separating documents, chunks, embedding_runs, search_runs, and answer_runs makes re-execution and comparison easier. Even if the API response only returns the answer body, internal logs should retain search top-k, exclusion reasons, and prompt template IDs.
Ingestion jobs should not complete with a synchronous response; instead, manage their state as queued, processing, indexed, or failed.
Save search result scores, chunk id, and document id to allow re-executing only the generation with the same candidates later.Distinguish external AI API timeouts from search failures, separating HTTP status and internal error codes.Observe how it breaksDuring validation, we check not only API responses but also the reproducibility of search candidates, the saving of citation IDs, and how timeouts are handled. Launching PostgreSQL and the API with Docker Compose, then running representative queries and checking log consistency, makes operational investigations easier.During validation, we check not only the API's 200 response but also the consistency of search run and answer run records remaining in the DB. After running representative queries, testing whether citation IDs exist and if chunks in the prompt match citations in the response significantly eases operational investigations.Discarded options and tradeoffsSeparating responsibilities increases the number of files and tables. This might seem excessive for small prototypes. However, to continuously improve quality in production, a design that allows observing failures at each distinct stage is necessary.The more detailed the logs, the greater the storage volume and the burden of personal information management. It's necessary to separate information required for answer improvement from input content that should not be stored, and to incorporate retention periods and masking into the design.Decision criteria for the fieldIn RAG API design, it's important not only to return an answer but also to be able to trace why that answer was given. Even with a FastAPI and PostgreSQL configuration, simply separating responsibilities and logs provides a foundation for improvement.A RAG API is not sufficient just by returning good answers. By making it possible to trace why an answer was given on PostgreSQL, you can achieve both a thin FastAPI API layer and an easily improvable search generation foundation.
- Separation of Concerns for RAG API Built with FastAPI and PostgreSQL
- We will organize the design for separating ingestion, search, answer generation, and audit logs in a RAG API that uses FastAPI, PostgreSQL, and vector search.Separation of Concerns for RAG API Built with FastAPI and PostgreSQLFrom a chat interface perspective, a RAG API appears as a single endpoint. However, internally, document ingestion, index updates, search, prompt construction, answer generation, and log storage each have distinct responsibilities. With a FastAPI and PostgreSQL configuration, it's crucial that RAG can be designed not just as a chat API, but as a data processing pipeline capable of reproducing search history.
- Where are the design challenges?
When implementing RAG with FastAPI, the initial inclination is to centralize processing at /chat. While this works in the short term, visibility deteriorates when it comes to investigating search quality, reproducing answer failures, re-indexing after document updates, or implementing permission-based searches.
RAG has longer processing times than typical CRUD APIs and is affected by delays and failures of external AI APIs. Furthermore, if the basis for an answer cannot be explained later, it becomes difficult to troubleshoot issues in business use.
The difficulty of a RAG API lies not in calling external AI APIs, but in the numerous intermediate states from question to answer. Without saving the state when chunks are created, embeddings are updated, search rankings are determined, and prompts are assembled, it's impossible to reproduce incorrect answers.*How to define boundaries?*We separate the ingestion API, search API, answer generation API, and evaluation API. While a chat endpoint can combine search and generation, internally, we separate the search service and generation service, logging their respective inputs and outputs.
In FastAPI, we keep endpoints thin and separate document ingestion, search, answer generation, and evaluation into service layers. PostgreSQL is used not just as a storage destination, but as an auditing backbone that links chunks, embedding_version, search executions, and answer executions.
Effective details in implementation
In PostgreSQL, we store documents, chunks, embedding metadata, question logs, search candidates, and answer logs in separate tables. In FastAPI, we pass DB sessions, AI clients, and search services via dependency injection, allowing them to be swapped during testing.
In table design, separating documents, chunks, embedding_runs, search_runs, and answer_runs makes re-execution and comparison easier. Even if the API response only returns the answer body, internal logs should retain search top-k, exclusion reasons, and prompt template IDs.
Ingestion jobs should not complete with a synchronous response; instead, manage their state as queued, processing, indexed, or failed.
Save search result scores, chunk id, and document id to allow re-executing only the generation with the same candidates later.


