The Architecture Behind a Stateless AI Application

Published: (December 1, 2025 at 06:39 PM EST)
3 min read
Source: Dev.to

Source: Dev.to

The project started with a risky‑looking decision: no backend database.
At the time there was no need to persist user data—getting the user’s response was the priority. Most tutorials assume you’ll store accounts, sessions, and data in PostgreSQL, MongoDB, DynamoDB, etc., but this app doesn’t need to persist anything across devices.

The Three‑Layer Split

Architecture diagram

  • Frontend – Handles all user interaction, UI state, image compression, multi‑step wizard flow, local history, and result rendering.
  • Backend – One responsibility: transform image data into diagnosis data. It receives a request, builds a prompt, calls the LLM, parses the response, and returns structured JSON. No state, sessions, or database.
  • AI Layer – Claude Vision receives the images with crafted prompts and returns detailed diagnostic information.

Each layer does exactly one thing. Mixing responsibilities (e.g., storing history in the backend or calling the LLM directly from the frontend) adds unnecessary complexity.

Data Interaction

Data flow diagram

  • History and settings never leave the user’s device.
  • The API key passes through the server but is never stored.

The Multi‑Step Wizard: Why State Machines

The scan flow can include five potential steps:

  1. Plant part selection
  2. Crop type selection
  3. Media upload
  4. Analysis mode (single vs. multiple images)
  5. Context entry

Traditional numbered steps become ambiguous because the presence of step 4 depends on runtime conditions (single vs. multiple images).

Solution: a state machine with meaningful state names (part, crop, media, mode, context, analyzing). The UI renders based on the current state, and the progress indicator is computed dynamically, keeping the user experience accurate without hard‑coded step numbers.

State machine problem illustration

Storage Architecture: Three Tiers

Storage tiers diagram

TierPurposeTypical Content
Session storageHolds consent flags that expire when the browser closes.Consent choices for health‑related data.
Local storagePersists data across sessions.Scan history, accessibility settings (font size, voice prefs), API key.
Embedded deep cacheStores the full diagnosis for each history item (all 25+ fields).Treatments, prevention tips, full result payload.

The deep cache increases storage size but enables true offline access—critical for rural users. A typical scan is 20–30 KB; with a maximum of ~50 scans the total is ~1.5 MB, well under the 5 MB browser quota. Older scans rotate out automatically.

The Single Endpoint Philosophy

The backend exposes only one API endpoint:

POST /api/v1/analyze

Single endpoint diagram

All analysis modes (single image, batch, video) are handled by a mode parameter, which adjusts prompt construction and response handling. This avoids:

  • Duplicate validation logic
  • Complex client‑side routing
  • Versioning headaches for multiple endpoints
  • Extra documentation overhead

A single, well‑documented endpoint is simpler to test and maintain.

User‑Provided API Key

API key UI

  • Cost transparency: Users see exactly what they’re paying; no hidden markup.
  • No key management: No database needed for storing or rotating keys, reducing operational complexity.
  • Scalability: Each user has their own Anthropic quota, eliminating shared rate limits.
  • Trust: Users retain control of their credentials; the developer cannot incur unexpected charges.

The trade‑off is friction—users must create an Anthropic account and generate an API key before using the app. This is acceptable for a technical audience but would need reconsideration for mass‑market adoption.

Batch vs. Single Mode: The Same Plant Problem

When multiple images are uploaded, the system must decide whether they represent different plants (analyze separately) or the same plant from different angles (analyze together).

(Further details on the implementation of this decision logic continue in the original article.)

Back to Blog

Related posts

Read more »

Day 1276 : Career Climbing

Saturday Before heading to the station, I did some coding on my current side project. Made some pretty good progress and then it was time to head out. Made i...

JWT Token Validator Challenge

Overview In 2019 Django’s session management framework contained a subtle but catastrophic vulnerability CVE‑2019‑11358. The framework failed to properly inv...