My App
System Architecture

Communication and Runtime Flow

Everything in ClayDiary happens inside a single HTTP request/response. There's no job queue, no background worker, no webhook callback — if a piece of work can't finish before the response goes out, it doesn't happen. That's a deliberate scope decision, not a missing feature, and it shapes how both writes and chat work.

Writing a journal entry: all-or-nothing, before the response

Adding an event to a piece does several things that would often be split across a request and a background job: build the searchable text, call the embedding API, write the row, and let a database trigger roll the change up into the piece's current state. ClayDiary does all of it inline:

POST /pieces/{id}/events
        |
Build searchable text from the piece and the new event
        |
Call the embedding API (await)
        |
Insert the event row with its vector
        |
Trigger updates the piece's current stage/location
        |
Commit, then respond 201

If the embedding call fails, the insert never happens — the request fails instead of leaving a half-indexed event behind. The cost is that the response is only as fast as the slowest step in that chain, including a network call to an external API. At ClayDiary's scale that's an acceptable wait; it's the kind of thing that would need to move off the request path if traffic grew.

Chat: same pattern, streamed response

A chat answer can take several seconds to generate, so a single blocking request would feel broken. Rather than switching to a different communication pattern — SSE, websockets, or a job-and-poll flow — ClayDiary keeps the same one-request model and makes the response itself a stream: a plain text/plain chunked body that the frontend reads token-by-token with response.body.getReader().

That choice has one direct consequence: without SSE's typed events, there's no built-in way to send "here's the answer" and "here's which pieces it came from" as two separate messages. ClayDiary solves it with the smallest protocol that works — a single record-separator byte appended after the answer text, followed by one JSON object carrying the pipeline path taken and the sources used. The frontend splits on that byte instead of parsing a second message type.

Inside that one streamed response, the turn still runs as a strict decision tree — classify the question, decide whether it needs SQL or retrieval, then generate — each step depending on the last, so nothing here runs in parallel.

Concurrency lives between requests, not within one

FastAPI, asyncpg, and PydanticAI are all async, so while one request is waiting on Postgres or an LLM provider, the event loop is free to make progress on other requests. That's where ClayDiary gets its concurrency — across users, not within a single chat turn. A single turn's steps are sequential by necessity: routing depends on classification, generation depends on retrieval.

What's deliberately out of scope

No retry queue: if the embedding API or the chat model is down, the request simply fails. No reconnection semantics: if a client disconnects mid-stream, the generation step notices on its next token and stops — the turn is never written to chat history, so a dropped connection quietly costs that answer rather than corrupting it. These are named trade-offs, made to keep the system legible at its current size, not gaps nobody noticed.

On this page