惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
D
DataBreaches.Net
GbyAI
GbyAI
Microsoft Security Blog
Microsoft Security Blog
博客园_首页
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
IT之家
IT之家
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
月光博客
月光博客
U
Unit 42
Hugging Face - Blog
Hugging Face - Blog
博客园 - 叶小钗
腾讯CDC
B
Blog RSS Feed
博客园 - Franky
爱范儿
爱范儿

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How I Structure a FastAPI Backend with LLM Features (From...
aichannode · 2026-04-29 · via DEV Community

How I Structure a FastAPI Backend with LLM Features (From a Real Project)

I Don’t Start With Endpoints Anymore

When I used to start backend projects, I’d jump straight into writing routes.

That worked… until the project grew.

Now, I start with something else:

“How will this project fall apart in 3 months?”

Because it will — especially if you’re using LLMs.

You’ll start seeing:

  • prompts copied across files
  • random LLM calls inside endpoints
  • parsing logic that no one wants to touch
  • “temporary” hacks that become permanent

So these days, I focus heavily on structure first, features second.

This post is how I structured a FastAPI backend with LLM integration for a real estate consultant system — and what actually held up.


FastAPI vs Express — Different Problems

Coming from Node.js + Express, I was used to this:

routes/
controllers/
services/
models/

Enter fullscreen mode Exit fullscreen mode

Flexible, simple… and easy to mess up.

Over time:

  • controllers get bloated
  • services become dumping grounds
  • logic gets duplicated

With FastAPI, the issue is different:

It gives you powerful tools, but no strong opinion on structure.

So people end up with:

  • everything inside main.py
  • business logic inside route handlers
  • LLM calls scattered everywhere

And once LLM is involved, things get chaotic fast.


The Project (Real Context)

This is from a real project:

A backend that collects user preferences, uses an LLM to interpret them, and guides real estate search.

Not just CRUD. It includes:

  • multi-step intake flow
  • LLM-based parsing
  • dynamic question generation

The Structure I Landed On

api/
core/
llm/
models/
repositories/
schemas/
utils/

Enter fullscreen mode Exit fullscreen mode


High-Level Flow

[Client]
   ↓
[API Layer]
   ↓
[Repositories + LLM]
   ↓
[Database]   [LLM Provider]

Enter fullscreen mode Exit fullscreen mode


API Layer — Keep It Boring

api/v1/endpoints/

Enter fullscreen mode Exit fullscreen mode

Responsibilities:

  • request/response
  • validation
  • calling repositories or LLM layer
@router.post("/intake")
def create_intake(...):
    return intake_repo.create(...)

Enter fullscreen mode Exit fullscreen mode


Core — The Foundation

core/

Enter fullscreen mode Exit fullscreen mode

Includes:

  • config
  • DB connection
  • dependency injection
  • external SDK wrappers

LLM Layer — Treat It as a Domain

llm/
  intake/
    prompts.py
    schema.py
    service.py
  providers/

Enter fullscreen mode Exit fullscreen mode

All LLM-related logic lives here.

llm_intake_service.parse_user_input(text)

Enter fullscreen mode Exit fullscreen mode

Why?

Because LLM is:

  • non-deterministic
  • sensitive to prompts
  • provider-dependent

Models vs Repositories

models/
repositories/

Enter fullscreen mode Exit fullscreen mode

  • models/ → DB structure
  • repositories/ → queries

Keeps data access clean and testable.


Schemas — Critical for LLM

schemas/

Enter fullscreen mode Exit fullscreen mode

LLMs:

  • hallucinate
  • return inconsistent formats

So:

  • define strict schemas
  • validate every response

Utils — Use Carefully

utils/

Enter fullscreen mode Exit fullscreen mode

Good for:

  • small helpers

Bad when:

  • it becomes a dumping ground

LLM Flow

User Input
   ↓
LLM Prompt
   ↓
LLM Response
   ↓
Schema Validation
   ↓
Structured Data

Enter fullscreen mode Exit fullscreen mode


One Key Idea

Treat LLM as its own domain, not just a tool.


Final Thoughts

This structure isn’t perfect.

But it worked in a real project.

Structure isn’t about being clean.

It’s about staying sane when things get messy.