Pattern: LLM in backend

The classic way to build LLM-powered apps is to keep all the AI logic, prompts, and orchestration in the backend. This is the default for most teams, and for good reason: it's simple, secure, and puts all the sensitive stuff where you can control it.

See also: Pattern: LLM in frontend.

When to use it

Use the LLM-in-backend pattern when:

Avoid this pattern if:

LLM-in-backend is best for teams and products that value safety, centralized control, and strict management of sensitive operations over speed and flexibility.

How it works

The frontend is just a UI layer. It collects user input and sends it to the backend. The backend owns everything AI-related: prompt templates, orchestration, OpenAI SDK calls, and any business logic that needs to interact with the model. The backend talks to OpenAI directly, so API keys and secrets never leave your server.

Frontend                 Backend
+----------------+       +---------------------+
| UI components  | ----> | Prompts             |
+----------------+       | OpenAI SDK          | ----> OpenAI
                         +---------------------+

Advantages

Disadvantages

Expanded architecture

Here's what a typical LLM-in-backend architecture looks like in practice:

Frontend                 Backend
+----------------+       +----------------------+
| UI components  | ----> | API server           |
+----------------+       |   Prompts & AI logic | --+
                         |   Business APIs      |   |
                         +----------------------+   |
                           OpenAI SDK <-------------+
                               |
                               v
                            OpenAI API

Summary

LLM-in-backend is ideal for teams that value safety, centralized control, and strict management of sensitive operations over speed and flexibility. If you need to keep everything confidential and tightly managed, this is the pattern to use.