← BACK TO WRITING

WRITING / Backend / AI Systems

Why I Keep AI Logic Out of Route Handlers

A practical boundary for keeping model orchestration testable as an AI backend grows.

BackendAI Systems
FORMAT
decision
READ
6 min
PUBLISHED
Aug 17, 2026
TOPICS
Backend · AI Systems
THE TAKEAWAYHTTP is one adapter around a use case; model orchestration belongs in the use-case layer where it can be tested directly.
01

The convenient beginning

Putting prompts and model calls in a route handler is fast for a prototype. It also binds transport, orchestration, validation, and provider behavior into one function.

  • Hard to test
  • Hard to reuse
  • Provider errors leak into HTTP concerns
02

The boundary

Routes parse requests and translate outcomes. A use-case service owns orchestration, while provider and persistence adapters sit behind interfaces.

  • Transport adapter
  • Application use case
  • Infrastructure adapters
03

Why it pays off

The same workflow can run from a job, CLI, or test without pretending to be an HTTP request.